使用asp.net发送电子邮件而不会崩溃应用程序

时间:2012-06-29 15:32:34

标签: c# asp.net

我正在开发一个用于发送电子邮件的asp.net应用程序。我想确保如果出现问题,那么它会记录错误并转移到下一条记录。我正在使用SmtpClient类发送电子邮件。但是现在,如果某个电子邮件地址错误,它就会崩溃。如何强制它移动到下一个记录?

我的代码是这样的:

 var EmailsQueue = context.WC_EmailToolQueue.Where(t => t.EmailDate == null).ToList();

            foreach (var email in EmailsQueue)
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(email.WC_EmailToolTemplates.SenderEmail);
                message.To.Add(new MailAddress(email.Email));
                message.Body = body;
                message.IsBodyHtml = true;
                message.Subject = subject;

                using (SmtpClient smtp = new SmtpClient
                {
                    Host = email.WC_EmailToolTemplates.Host, 
                    Port = email.WC_EmailToolTemplates.Port,  
                    Credentials = new NetworkCredential(email.WC_EmailToolTemplates.SMTPUser, email.WC_EmailToolTemplates.SMTPPass), 
                    EnableSsl = email.WC_EmailToolTemplates.EnableSSL 
                })
                   try
                {
                    smtp.Send(message);
                }
                catch (Exception ex) {

                }
                }

此致 Asif Hameed

3 个答案:

答案 0 :(得分:2)

使用Try / Catch捕捉异常。

try
{
    // Send Email code
}
catch (Exception ex) //Or specific exceptions
{
    Log(ex.ToString());
}

答案 1 :(得分:0)

您可以使用try catch块。如果地址格式无效,Add函数将抛出错误。如果需要,您也可以记录

   try
    {
         message.To.Add(new MailAddress(email.Email));
    }
    catch (FormatException ex)
    {
        //If you want you may log it here
    }

编辑:根据OP的问题编辑。

以下代码将跳过向有问题的项目发送电子邮件(地址错误等)。但是执行循环中的其余项目

foreach (var email in EmailsQueue)
{
    MailMessage message = new MailMessage();
    try
    {
        message.From = new MailAddress(email.WC_EmailToolTemplates.SenderEmail);
        message.To.Add(new MailAddress(email.Email));            
        message.Body = body;
        message.IsBodyHtml = true;
        message.Subject = subject;

        using (SmtpClient smtp = new SmtpClient
        {
            Host = email.WC_EmailToolTemplates.Host,
            Port = email.WC_EmailToolTemplates.Port,
            Credentials = new NetworkCredential(email.WC_EmailToolTemplates.SMTPUser, email.WC_EmailToolTemplates.SMTPPass),
            EnableSsl = email.WC_EmailToolTemplates.EnableSSL
        })
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        //error in sending email to one item in collection
        //Log it ig you want .Foreach will continue with remaining items    
    }
}

答案 2 :(得分:0)

只需将一些日志记录放入已有内容的catch块中。然后foreach循环将继续到下一条记录:

var EmailsQueue = context.WC_EmailToolQueue.Where(t => t.EmailDate == null).ToList();

    foreach (var email in EmailsQueue)
    {
        MailMessage message = new MailMessage();
        message.From = new MailAddress(email.WC_EmailToolTemplates.SenderEmail);
        message.To.Add(new MailAddress(email.Email));
        message.Body = body;
        message.IsBodyHtml = true;
        message.Subject = subject;

        using (SmtpClient smtp = new SmtpClient
        {
            Host = email.WC_EmailToolTemplates.Host,
            Port = email.WC_EmailToolTemplates.Port,
            Credentials = new NetworkCredential(email.WC_EmailToolTemplates.SMTPUser, email.WC_EmailToolTemplates.SMTPPass),
            EnableSsl = email.WC_EmailToolTemplates.EnableSSL
        })
            try
            {
                smtp.Send(message);
            }
            catch (Exception ex)
            {
                //log the error here <--------
            }
    }