Tutorial :ASP.NET won't send email and no error message, weird!



Question:

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;          }  }  


Solution:1

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


Solution:2

piggy backing of what bruce said, do this:

try      'your code here'  catch ex As Exception      Response.Write(ex.Message)  end try  


Solution:3

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »