silent
silent

Reputation: 4014

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

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

Upvotes: 1

Views: 1431

Answers (3)

tvanfosson
tvanfosson

Reputation: 532625

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.

Upvotes: 0

Jason
Jason

Reputation: 52537

piggy backing of what bruce said, do this:

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

Upvotes: 0

Bruce
Bruce

Reputation: 8430

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

Upvotes: 4

Related Questions