[英]IIS error when sending newsletter using ASP.NET MVC
I've a problem with sending a newsletter (to about 5000 people) in my mvc-helper class. 我在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();
}
This is my function which will be called in a controller. 这是我的功能,将在控制器中调用。 The next Code-Block is the sending function.
下一个代码块是发送功能。
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;
}
}
}
}
After approx 1300 emails, my IIS get the following error: 大约1300封电子邮件后,我的IIS收到以下错误:
How can I solve this problem? 我怎么解决这个问题? I need the newsletter system...
我需要通讯系统......
I've found a lot of errors, why it doesn't work. 我发现了很多错误,为什么它不起作用。
The first one was a security setting in MailEnable. 第一个是MailEnable中的安全设置。 On MailEnableAdmin -> Servers -> Localhost -> Servers and Connectors -> Rightclick on SMTP and Settings -> Security-Tab there was a checkmark on a limit of receipiants (1000).
在MailEnableAdmin - >服务器 - >本地主机 - >服务器和连接器 - >右键单击SMTP和设置 - >安全选项卡上,有一个关于收据限制的复选标记(1000)。 I've unchecked it and restarted the Server.
我已取消选中它并重新启动服务器。
After this, there was a unvalid E-Mail-Address in my User-List. 在此之后,我的用户列表中存在无效的电子邮件地址。 Then I've created an E-Mail-Check-Funtion:
然后我创建了一个电子邮件检查功能:
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;
}
and I've updated my send-function to 我已将我的发送功能更新为
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) { }
}
}
}
and the SendNewsletter-Function is the same. 和SendNewsletter-Function是一样的。 Now, all E-Mails go out.
现在,所有的电子邮件都会消失。
Thanks a lot to all users who helped! 非常感谢所有帮助过的用户!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.