使用ASP.NET MVC发送新闻稿时出现IIS错误

时间:2016-05-26 12:59:29

标签: c# asp.net asp.net-mvc iis

我在mvc-helper课程中发送简报(大约5000人)时遇到了问题。

public static void SendMail(string fromAccount, string toAccount, string subject, string eMailText)
{
    var fromEmailAddress = new EMailModel(fromAccount);

    var body = string.Empty;
    using (var sr = new StreamReader(HostingEnvironment.MapPath("\\App_Data\\Templates\\") + "EMailTemplate.txt"))
    {
        body = sr.ReadToEnd();
    }

    body = body.Replace("%%Subject%%", subject);
    body = body.Replace("%%Body%%", eMailText);
    body = body.Replace("%%Year%%", DateTime.Now.Year.ToString());

    using (MailMessage mail = new MailMessage(fromEmailAddress.EMailAddress, toAccount))
    {
        mail.Subject = subject;
        mail.Body = WebUtility.HtmlDecode(body);
        mail.IsBodyHtml = true;

        using (SmtpClient smtp = new SmtpClient(Statics.SmtpClient, Statics.SmtpPort))
        {
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential(fromEmailAddress.EMailAddress, fromEmailAddress.Password);
            smtp.EnableSsl = Statics.SslEnabled;
            try
            {
                smtp.Send(mail);
                Thread.CurrentThread.Join(1000);
            }
            catch (Exception ex) {
                throw ex;
            }
        }
    }
}

这是我的功能,将在控制器中调用。下一个代码块是发送功能。

{{1}}

大约1300封电子邮件后,我的IIS收到以下错误:

ErrorImage

我该如何解决这个问题?我需要通讯系统......

1 个答案:

答案 0 :(得分:0)

我发现了很多错误,为什么它不起作用。

第一个是MailEnable中的安全设置。在MailEnableAdmin上 - >服务器 - > Localhost - >服务器和连接器 - >右键单击SMTP和设置 - > Security-Tab上有一个关于收据限制的复选标记(1000)。我已取消选中它并重新启动服务器。

此后,我的用户列表中存在无效的电子邮件地址。然后我创建了一个电子邮件检查功能:

    private static bool IsValidEMail(string eMailAddress)
    {
        if (Regex.IsMatch(eMailAddress, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase) && new EmailAddressAttribute().IsValid(eMailAddress))
            return true;

        return false;
    }

我已将发送功能更新为

    public static void SendMail(string fromAccount, string toAccount, string subject, string eMailText)
    {
        if (!IsValidEMail(toAccount))
        {
            var invalidEMailAddress = toAccount;

            toAccount = "administrator@YOUR-DOMAIN.com";
            subject = "E-Mail-Address is invalid";
            eMailText = String.Format("Dear Administrator,<br><br>the following E-Mail-Address is invalid:<br><br>{0}", invalidEMailAddress);
        }

        var fromEmailAddress = new EMailModel(fromAccount);

        var body = string.Empty;
        using (var sr = new StreamReader(HostingEnvironment.MapPath("\\App_Data\\Templates\\") + "EMailTemplate.txt"))
        {
            body = sr.ReadToEnd();
        }

        body = body.Replace("%%Subject%%", subject);
        body = body.Replace("%%Body%%", eMailText);
        body = body.Replace("%%Year%%", DateTime.Now.Year.ToString());

        using (MailMessage mail = new MailMessage(fromEmailAddress.EMailAddress, toAccount))
        {
            mail.Subject = subject;
            mail.Body = WebUtility.HtmlDecode(body);
            mail.IsBodyHtml = true;

            using (SmtpClient smtp = new SmtpClient(Statics.SmtpClient, Statics.SmtpPort))
            {
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(fromEmailAddress.EMailAddress, fromEmailAddress.Password);
                smtp.EnableSsl = Statics.SslEnabled;
                try
                {
                    smtp.Send(mail);
                    Thread.CurrentThread.Join(1000);
                }
                catch (Exception) { }
            }
        }
    }

和SendNewsletter-Function是一样的。现在,所有的电子邮件都会消失。

非常感谢所有帮助过的用户!