简体   繁体   English

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

[英]How do you send mass emails from ASP.NET?

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.我设置了一个测试页面,并设法使用 System.Net.Mail 命名空间向自己发送了一封测试电子邮件。 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.有时它会中止循环并且一些电子邮件不会被发送。

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.我对 .NET 中的电子邮件领域非常陌生。

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 (ie sends the emails) and then sleeps for a period of time.然后运行一个线程,从队列中提取 x 个作业,处理它们(即发送电子邮件),然后休眠一段时间。 This will give you web app some breathing space.这将为您的 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.通常,运行工作线程的进程不会是 Web 应用程序本身。 It would be a windows service or something similar.这将是一个 Windows 服务或类似的东西。 This might not be possible if you're on shared hosting.如果您使用的是共享主机,这可能是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM