ASP.NET won't send email and no error message, weird! - asp.net

I tried to send an email using this class below, but no success, no error message, the page just executed very fast, any problem with this class?
public bool mailSender(string strSubject, string strFrom, string strFromName, string strTo, string strBody)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(strFrom, strFromName);
smtpClient.Host = ConfigurationManager.AppSettings["smtpServer"];
smtpClient.Port = 25;
smtpClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpUsername"], ConfigurationManager.AppSettings["smtpPassword"]);
message.From = fromAddress;
message.To.Add(strTo);
message.Subject = strSubject;
message.IsBodyHtml = false;
message.Body = strBody;
smtpClient.Send(message);
return true;
}
catch
{
return false;
}
}

Your try/catch block is deliberately throwning away any error message. Remove that and see what you get.

piggy backing of what bruce said, do this:
try
'your code here'
catch ex As Exception
Response.Write(ex.Message)
end try

One thing I have noticed, especially when running in the debugger, is that the SmtpClient doesn't seem to actually send the mail until it gets disposed. At least, I often see the messages going out when I shutdown the debugger rather than at the time the mail is actually supposed to be sent.

Related

Unable to send mail if one mail not present with ASP

I've written code for sending a newsletter, all is working fine, but there is a problem if one of the email addresses in the list, doesn't exit or the domain does not exist.
In this case the script stops immediately and the sending of the mail list is not finished.
Here is the part of the code I want to modify.
public static void SendMessage(String sender, String recipient, String message, String object)
{
try
{
MailMessage mail = new MailMessage(sender, recipient);
mail.Subject = object;
mail.IsBodyHtml = true;
mail.Body = message;
SmtpClient smtp = new SmtpClient();
smtp.Host = "my.smtp.com";
smtp.Send(mail);
}
catch (Exception e)
{ throw new Exception("AdminEmail - SendMessage >> recipient: " + recipient + " - generic error: " + e.Message); }
}
Hope somone can help me, thank you very much!
Welcome to SO.
From what I can infer from your description your are not handling the exception that is thrown by SendMessage.
Handle the exception in the caller method. Or do a dirty fix as shown below...
This is not a real fix. But will help you understand the issue...You have to determine in your calling method what to do if SendMessage throws an exception.
public static void SendMessage(String sender, String recipient, String message, String object)
{
try
{
MailMessage mail = new MailMessage(sender, recipient);
mail.Subject = object;
mail.IsBodyHtml = true;
mail.Body = message;
SmtpClient smtp = new SmtpClient();
smtp.Host = "my.smtp.com";
smtp.Send(mail);
}
catch (Exception e)
{
//Just log error and continue to process
}
}

Sending mass email in ASP.NET

This is my code to send lots of emails. I want to optimize this code to be sure that it will work and can successfully send all emails. What should I do? I know that putting interrupts between sending might be useful but how can I do this?
The main problem is avoiding classify emails as spam and decreasing number of failed sent emails.
var list = from c in context.Emails orderby c.EmailAddress select c.EmailAddress;
MailMessage mail = new MailMessage();
try
{
mail.From = new MailAddress(txtfrom.Text);
foreach (var c in list)
{
mail.To.Add(new MailAddress(c.ToString()));
}
mail.Subject = txtSub.Text;
mail.IsBodyHtml = true;
mail.Body = txtBody.Text;
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(
FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
catch (Exception)
{
//exception handling
}
I would advise you against adding all reciepients into the same mail message.
Rather use this code:
mail.From = new MailAddress(txtfrom.Text);
mail.Subject = txtSub.Text;
mail.IsBodyHtml = true;
mail.Body = txtBody.Text;
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
SmtpClient smtp = new SmtpClient();
foreach (var c in list)
{
mail.To.Clear();
mail.To.Add(new MailAddress(c.ToString()));
smtp.Send(mail);
}
With a little due diligence, this can be accomplished with a very simple console application which can be called from the Web form to dispatch the emails. By diligence, I mean to insert a pause between batches so that the mail server won't get bogged down. For example, if you were grabbing the addresses from a DB and sending them out, you could have something like:
if ((count >= 100) && (count % 100 == 0))
Thread.Sleep(30000);
-----------------------------------------
// Web form code-behind
// Pass subject and message strings as params to console app
ProcessStartInfo info = new ProcessStartInfo();
string arguments = String.Format(#"""{0}"" ""{1}""",
subjectText.Text.Replace(#"""", #""""""),
messageText.Text.Replace(#"""", #""""""));
info.FileName = MAILER_FILEPATH;
Process process = Process.Start(info.FileName, arguments);
Process.Start(info);
More info here: Calling Console Application from Web Form

error sending email using ASP.NET web app

I'm trying to send email to my site users (ASP.NET, VS2010), currently I'm using my gmail account for sending email, but I'm getting the following error:
ex = {"Failure sending mail."}
InnerException = {"The remote name could not be resolved: 'smtp#gmail.com'"}
it is my code:
MailMessage mailObj = new MailMessage("mygmailaccount#gmail.com", "myyahooaccount#yahoo.com", "test", "test2");
SmtpClient SMTPServer = new SmtpClient("smtp#gmail.com");
SMTPServer.Credentials = new System.Net.NetworkCredential("mygmailaccount", mygmailpassword);
try
{
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
string a = ex.Message;
}
what is going wrong here? should I do something in my web.config? how can I find smtp server of my own host?
smtp#gmail.com is incorrect. You probably meant smtp.gmail.com. See the following question for a complete example.
Sending email in .NET through Gmail
you did not set gmail parameters correctly:
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new NetworkCredential("myGmailAcconut#gmail.com", "password");

Sending e-mail in ASP .Net

How to send e-mail on ASP .Net using outlook address??
I've tried this code but no luck:
Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mailMessage.From = New System.Net.Mail.MailAddress("fromAddress")
mailMessage.To.Add(New System.Net.Mail.MailAddress("toAddress"))
mailMessage.Priority = Net.Mail.MailPriority.High
mailMessage.Subject = "Subject"
mailMessage.Body = "test"
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("xxx.outlook.com", portNumber)
smtpClient.Send(mailMessage) //--> got error here
But while I'm execute the code, it got this error message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
I've tried to add a line of code:
smtpClient.Credentials = New System.Net.NetworkCredential(username, password)
But still can't send the e-mail.
Anyone can help?
Try smtpClient.UseDefaultCredentials = false; before you set new Credentials
Try to set smtpClient.EnableSsl to true / false depending on your environment
I'm guessing you are using Exchange 2007 or later as backend?
Anyway, your mail server does not allow you to send mails anonymously. You'll either need to supply a username/password in your code or allow unauthenticated relaying from your webserver.
Talk to your IT guys what they prefer.
I did it using c#.This may help you.Please check.
MailMessage msg1 = new MailMessage();
msg1.From = strEFrom;
msg1.To = strETo;
msg1.Cc = strECC;
msg1.Subject = "Hi";
msg1.Priority = MailPriority.High;
msg1.BodyFormat = MailFormat.Html;
msg1.Body = strBody;
SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
SmtpMail.Send(msg1);
and in web.config file
<appSettings>
<add key="MailServer" value=""/>
</appSettings>
Try these settings check for .EnableSSL property to true/false and the smtp post number on which your mail server listen for outgoing mail.
When you do not set enable the SSL settings in outlook for gmail outlook configuration then it gives same error but the reason was found the SSL settings..
well try this may it will solve your problem..
msg.IsBodyHtml = true;
msg.Body = mailContent;
SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
if it does not solve your problem then check your mail server/ client for the problem.
public static bool SendMail(string mailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(mailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(from);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = false;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
return true;
}
catch (Exception)
{
return false;
}
}
I use this code to send mail from my gmail account.
was having the same error, moved the client.UseDefaultCredentials = false; before setting client.Credentials and everything works.

Is there any way to send the Email notification using asp.net without using credentials?

I have this code to send email notification in my page.
MailAddress to = new MailAddress("xxxxx#gmail.com");
MailAddress from = new MailAddress("xxx#gmail.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "Error Occred in the application:";
message.Body = ex.Message;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("user", "password");
Is there any other way we have without giving credentials to send the message?
There are some workarounds mentioned here
Only on servers that allow anonymous sending, which Gmail doesn't.
If you have an open mail relay that doesn't require credentials, then you don't need to supply them.
use this code spinet for that,
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailAddress SendFrom = new MailAddress(txtFrom.Text);
MailAddress SendTo = new MailAddress(txtTo.Text);
MailMessage MyMessage = new MailMessage(SendFrom, SendTo);
MyMessage.Subject = txtSubject.Text;
MyMessage.Body = txtBody.Text;
Attachment attachFile = new Attachment(txtAttachmentPath.Text);
MyMessage.Attachments.Add(attachFile);
SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
emailClient.Send(MyMessage);
litStatus.Text = "Message Sent";
}
catch (Exception ex)
{
litStatus.Text=ex.ToString();
}
}
i hope this will be helpful.

Resources