我在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_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; } } } }
我怎么解决这个问题?我需要通讯系统……
解决方法
我发现了很多错误,为什么它不起作用.
第一个是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 = "[email protected]"; 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是一样的.现在,所有的电子邮件都会消失.
非常感谢所有帮助过的用户!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。