加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:00:06 所属栏目:百科 来源:网络整理
导读:我在mvc-helper类中发送简报(大约5000人)时遇到了问题. public static void SendNewsletter(string fromAccount,string subject,string eMailText){ var userEMailAddresses = new Liststring(); using (var dbContext = new dbEntities()) { userEMailAddre
我在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 eMailText)
{
    var fromEmailAddress = new EMailModel(fromAccount);

    var body = string.Empty;
    using (var sr = new StreamReader(HostingEnvironment.MapPath("App_DataTemplates") + "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上 – >服务器 – > 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 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_DataTemplates") + "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是一样的.现在,所有的电子邮件都会消失.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读