ASP.Net C# - 发送电子邮件时发现错误

时间:2014-08-05 10:51:43

标签: c# asp.net email error-handling try-catch

所以我有一个功能来发送一个电子邮件,我在一些验证后在按钮点击事件中运行。但是,如何在发送电子邮件时发现任何错误?我熟悉try-catch-finally方法,那么底部的代码是否足够?

protected void sendEmail(string activationCode, string username, string emailAddress)
{
    SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
    var mail = new MailMessage();
    mail.From = new MailAddress("EMAIL");
    mail.To.Add(emailAddress);
    mail.Subject = "Please activate your account.";
    mail.IsBodyHtml = true;
    string htmlBody;
    htmlBody = "Dear " + username + "<br /><br />";
    htmlBody += "Thank you for registering an account.  Please activate your account by visiting the URL below:<br /><br />";
    htmlBody += "http://localhost:57167/signin.aspx?activate=" + activationCode + "<br /><br />";
    htmlBody += "Thank you.";
    mail.Body = htmlBody;
    SmtpServer.Port = 587;
    SmtpServer.UseDefaultCredentials = false;
    SmtpServer.Credentials = new System.Net.NetworkCredential("EMAIL", "PASSWORD");
    SmtpServer.EnableSsl = true;
    SmtpServer.Send(mail);
}

这样的事情好吗?

protected void sendEmail(string activationCode, string username, string emailAddress)
{
    try
    {
    SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
    var mail = new MailMessage();
    mail.From = new MailAddress("EMAIL");
    mail.To.Add(emailAddress);
    mail.Subject = "Please activate your account.";
    mail.IsBodyHtml = true;
    string htmlBody;
    htmlBody = "Dear " + username + "<br /><br />";
    htmlBody += "Thank you for registering an account.  Please activate your account by visiting the URL below:<br /><br />";
    htmlBody += "http://localhost:57167/signin.aspx?activate=" + activationCode + "<br /><br />";
    htmlBody += "Thank you.";
    mail.Body = htmlBody;
    SmtpServer.Port = 587;
    SmtpServer.UseDefaultCredentials = false;
    SmtpServer.Credentials = new System.Net.NetworkCredential("EMAIL", "PASSWORD");
    SmtpServer.EnableSsl = true;
    SmtpServer.Send(mail);
    }
    catch
    {
        lblError.Text = "error message";
    }
}
    }

4 个答案:

答案 0 :(得分:2)

您应该通过访问 {status> 属性 SmtpException Class 来处理异常。

同时将尝试块仅放在您认为可能发生异常的位置。

        try
        {
            SmtpServer.Send(mail);
        }
        catch (SmtpFailedRecipientsException ex)
        {

        }

答案 1 :(得分:1)

是的,这或多或少就足够了(取决于你想要做的事情)。

最好的方法是将当前方法保留原样(第一个列表),然后再使用另一个方法:

public void dispatchEmail(string activationCode, string username, string emailAddress)
{
  try
  {
      this.sendEmail(activationCode, username, emailAddress);
  }
  catch (Exception e) // Note: This is considered bad practice, might want to check for specific exceptions
  {
   lblError.Text = "error message: " + e.ToString();
  }
}

答案 2 :(得分:0)

我改变了一些事情。请参阅以下代码:

using(SmtpClient SmtpServer = new SmtpClient("smtp.live.com"))
{
    var mail = new MailMessage();
    mail.From = new MailAddress("EMAIL");
    mail.To.Add(emailAddress);
    mail.Subject = "Please activate your account.";
    mail.IsBodyHtml = true;
    string htmlBody;
    htmlBody = "Dear " + username + "<br /><br />";
    htmlBody += "Thank you for registering an account.  Please activate your account by visiting the URL below:<br /><br />";
    htmlBody += "http://localhost:57167/signin.aspx?activate=" + activationCode + "<br /><br />";
    htmlBody += "Thank you.";
    mail.Body = htmlBody;
    SmtpServer.Port = 587;
    SmtpServer.UseDefaultCredentials = false;
    SmtpServer.Credentials = new System.Net.NetworkCredential("EMAIL", "PASSWORD");
    SmtpServer.EnableSsl = true;
    try
    {
        SmtpServer.Send(mail);
    }
    catch(Exception ex)
    {
        lblError.Text = ex.Message;
    }
}
  1. 使用语句正确处理SmtpClient,无论是否失败。
  2. 将分配移到try-catch之外。
  3. 即使我反对捕捉一般例外(你应该知道期望什么样的例外);使用ex.Message显示相关的异常信息。

答案 3 :(得分:0)

zaitsman的回答是我一般会做的。最重要的是,如果要求您进行非阻塞调用以发送电子邮件(以便应用程序的其余部分不等待电子邮件发送并且可以继续进行其余工作),您应该使用TPL并对电子邮件发件人进行非阻止调用。像这样的东西 -

 Task emailTask = Task.Factory.StartNew(() => dispatchEmail();