tags:

views:

299

answers:

3

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;
  }
}
+3  A: 

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

Bruce
Oh God! I feel shame on my self... Didn't touch the code for so long... lol
silent
A: 

piggy backing of what bruce said, do this:

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

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.

tvanfosson