代码保持在阻止电子邮件错误后尝试发送电子邮件

时间:2015-01-08 15:16:02

标签: c# smtpclient

当我将阻止的电子邮件添加为“收件人”地址时,我正在尝试获取发送电子邮件失败的代码。我遇到的问题是SmtpClient一直试图发送电子邮件。我希望代码在异常后退出。关于这里发生了什么的任何想法?如何解决这个问题?

示例电子邮件:bademail@fakegmail.com

错误 其他信息:邮箱不可用。服务器响应为:5.7.1无法中继

相关代码

    public void Send(string body)
    {
        try
        {
            string configSubject = "emailSubject";
            var emailSubject = ConfigurationManager.AppSettings[configSubject];
            var emailFrom = ConfigurationManager.AppSettings["emailFrom"];
            var emailTo = ConfigurationManager.AppSettings["emailTo"];
            var emails = emailTo.Split(',');

            var message = new MailMessage();
            message.Subject = emailSubject;
            message.Body = body;
            message.From = new MailAddress(emailFrom);
            foreach (string email in emails)
            {
                message.To.Add(new MailAddress(email));
            }
            var client = new SmtpClient();
            client.Send(message);
        }
        catch {
            throw;//error occurs on send because of the bad email 
                  //and then a loop starts with this line of code 
                  //getting hit over and over and ovar. 
        }
    }

0 个答案:

没有答案