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_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收到以下错误:
我怎么解决这个问题?我需要通讯系统……
解决方法 我发现了很多错误,为什么它不起作用.第一个是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_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是一样的.现在,所有的电子邮件都会消失.
非常感谢所有帮助过的用户!
总结以上是内存溢出为你收集整理的c# – 使用ASP.NET MVC发送新闻稿时出现IIS错误全部内容,希望文章能够帮你解决c# – 使用ASP.NET MVC发送新闻稿时出现IIS错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)