[英]Sending emails are not received outside the company using System.Net.Mail
[英]C# Sending a large number of emails using System.Net.Mail
我们有一个ASP.NET MVC应用程序,该应用程序每个月通过电子邮件向客户发送大量报告。 每封电子邮件都附有月结单。 目前,我们有大约70个客户,但随着时间的推移,希望会增加。 我们已经看到许多电子邮件没有发送的问题。 我们使用System.Net.Mail API。
这是我们正在使用的代码,有没有更好的方法?
foreach(string client in clients){
SmtpClient client = new SmtpClient("server.com");
BackgroundWorker emailInvoker = new BackgroundWorker();
emailInvoker.DoWork += delegate
{
// Delay to prevent flow control, try later Relay error
Thread.Sleep(TimeSpan.FromSeconds(2));
client.Send(message);
}
emailInvoker.RunWorkerAsync();
}
因为这是一个ASP.NET MVC应用程序,所以您需要了解您的应用程序池。 创建多个线程将很快耗尽您的应用程序池,IIS可能会做一些时髦的事情,以防止在发送电子邮件时事情逐渐停止。 我将看看是否可以使用线程在IIS上执行长时间运行的作业? 如果您有兴趣了解更多信息。
如果要重写该代码,我将使用foreach
和电子邮件发送创建一个线程,而不是为每个客户创建一个线程。
BackgroundWorker emailInvoker = new BackgroundWorker();
emailInvoker.DoWork += delegate
{
// get your clients here somehow
foreach(string client in clients){
SmtpClient client = new SmtpClient("server.com");
// Delay to prevent flow control, try later Relay error
Thread.Sleep(TimeSpan.FromSeconds(2));
client.Send(message);
}
}
emailInvoker.RunWorkerAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.