簡體   English   中英

使用ASP.NET MVC發送新聞稿時出現IIS錯誤

[英]IIS error when sending newsletter using ASP.NET MVC

我在mvc-helper類中發送簡報(大約5000人)時遇到了問題。

public static void SendNewsletter(string fromAccount, string subject, string eMailText)
{
    var userEMailAddresses = new List<string>();

    using (var dbContext = new dbEntities())
    {
        userEMailAddresses =
            dbContext
                .User
                    .Where(w =>
                        !w.IsDeactivated &&
                        !w.IsBlacklisted)
                    .Select(s =>
                        s.UserName)
                    .ToList();
    }

    new Thread(() =>
    {
        for (int i = 0; i < userEMailAddresses.Count; i++)
        {
            SendMail(fromAccount, userEMailAddresses[i], subject, eMailText);
        }
    }).Start();
}

這是我的功能,將在控制器中調用。 下一個代碼塊是發送功能。

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

大約1300封電子郵件后,我的IIS收到以下錯誤:

ErrorImage

我怎么解決這個問題? 我需要通訊系統......

我發現了很多錯誤,為什么它不起作用。

第一個是MailEnable中的安全設置。 在MailEnableAdmin - >服務器 - >本地主機 - >服務器和連接器 - >右鍵單擊SMTP和設置 - >安全選項卡上,有一個關於收據限制的復選標記(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是一樣的。 現在,所有的電子郵件都會消失。

非常感謝所有幫助過的用戶!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM