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

发布于 2024-11-05 01:26:54 字数 503 浏览 0 评论 0原文

我为客户建立了一个网站,他们想要一个定制的新闻通讯工具。构建该工具很容易,但我不确定如何发送电子邮件。

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

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

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

但这仍然使网站运行缓慢,并且有中途中止的习惯。群发邮件的常用方法是什么?我确信我做得不对。

我对 .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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2024-11-12 01:26:54

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

如果您使用数据库,则可以创建电子邮件队列表来存储作业。我更喜欢使用这种存储而不是内存,以防应用程序因某种原因回收或引发异常......至少您可以从上次中断的地方继续。

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文