gpt4 book ai didi

c# - 使用 ASP.NET MVC 发送时事通讯时出现 IIS 错误

转载 作者:行者123 更新时间:2023-11-30 12:56:55 25 4
gpt4 key购买 nike

我在我的 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();
}

这是我的函数,将在 Controller 中调用。下一个代码块是发送函数。

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 -> Servers -> Localhost -> Servers and Connectors -> Rightclick on SMTP and Settings -> 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 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是一样的。现在,所有电子邮件都已发送出去。

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

关于c# - 使用 ASP.NET MVC 发送时事通讯时出现 IIS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37461606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com