你如何从ASP.NET发送大量电子邮件?

时间:2021-10-17 18:13:12

I built a website for a client and they would like a custom newsletter tool. Building the tool was easy, but I'm not sure how to send the email.

我为客户建立了一个网站,他们想要一个自定义的时事通讯工具。构建工具很简单,但我不确定如何发送电子邮件。

I set up a test page and managed to send a test email to myself using the System.Net.Mail namespace. I tried applying this code to a loop on the newsletter page, but it's turning out to be quite a difficult task. The email sending loop locks up the whole site for about an hour while it sends its emails. Sometimes it will abort the loop midway and some of the emails won't get sent.

我设置了一个测试页面,并设法使用System.Net.Mail命名空间向我自己发送测试电子邮件。我尝试将此代码应用于简报页面上的循环,但结果却是一项相当困难的任务。电子邮件发送循环在发送电子邮件时将整个站点锁定大约一个小时。有时它会中途中止循环,一些电子邮件将不会被发送。

I tried starting the loop on another thread.

我尝试在另一个线程上启动循环。

protected void btnSendNewsletter_Click(object sender, EventArgs e)
{
    Thread t = new System.Threading.Thread(new ThreadStart(SendEmails));
    t.Start();
}

but this still makes the site go slow and also has a habit of aborting part way through. What is the common method for sending mass emails? I'm sure I'm not doing it right.

但这仍然使网站变得缓慢,并且还有一种中途流产的习惯。发送群发电子邮件的常用方法是什么?我敢肯定我做得不对。

I am very new to the email arena in .NET so any help is much appreciated.

我对.NET中的电子邮件领域非常陌生,因此非常感谢任何帮助。

1 个解决方案

#1


6  

For this kind of task you're better to add a bunch of jobs to a queue. Then have a thread running which pulls x number of jobs from the queue, processes them (i.e. sends the emails) and then sleeps for a period of time. This will give you web app some breathing space.

对于这种任务,您最好将一堆作业添加到队列中。然后运行一个线程,从队列中提取x个作业,处理它们(即发送电子邮件),然后休眠一段时间。这将为您的web应用程序提供一些喘息空间。

If you're using a database you can create an Email Queue table to store the jobs. I prefer to use this kind of storage over memory incase the app recycles for some reason or an exception is thrown...atleast you can then pick up from where you left off.

如果您正在使用数据库,则可以创建电子邮件队列表来存储作业。我喜欢在内存中使用这种存储,因为某些原因导致应用程序回收或抛出异常......至少你可以从你离开的地方接收。

Generally, the process running the worker thread wouldn't be the web app itself. It would be a windows service or something similar. This might not be possible if you're on shared hosting.

通常,运行工作线程的进程不是Web应用程序本身。它将是一个Windows服务或类似的东西。如果您在共享主机上,这可能是不可能的。

#1


6  

For this kind of task you're better to add a bunch of jobs to a queue. Then have a thread running which pulls x number of jobs from the queue, processes them (i.e. sends the emails) and then sleeps for a period of time. This will give you web app some breathing space.

对于这种任务,您最好将一堆作业添加到队列中。然后运行一个线程,从队列中提取x个作业,处理它们(即发送电子邮件),然后休眠一段时间。这将为您的web应用程序提供一些喘息空间。

If you're using a database you can create an Email Queue table to store the jobs. I prefer to use this kind of storage over memory incase the app recycles for some reason or an exception is thrown...atleast you can then pick up from where you left off.

如果您正在使用数据库,则可以创建电子邮件队列表来存储作业。我喜欢在内存中使用这种存储,因为某些原因导致应用程序回收或抛出异常......至少你可以从你离开的地方接收。

Generally, the process running the worker thread wouldn't be the web app itself. It would be a windows service or something similar. This might not be possible if you're on shared hosting.

通常,运行工作线程的进程不是Web应用程序本身。它将是一个Windows服务或类似的东西。如果您在共享主机上,这可能是不可能的。