从 ASP.NET 发送批量电子邮件

发布于 2024-11-10 14:38:32 字数 139 浏览 0 评论 0 原文

在我当前正在处理的博客中,每当用户发表文章评论时,我都会向对该帖子发表评论的所有其他用户发送电子邮件。在这种情况下,同步或异步发送邮件的最佳方式是什么?任何人都可以共享一个代码片段,用于使用 .NET 中的 System.Net.Mail 向用户列表发送电子邮件。

In the blog I'm working on currently whenever an user posts a comment for an article I like to send emails to all the other users commented for that post. What is the best way in sending mails in this case either synchronously or asynchronously? Can anyone share a code snippet for sending email to a list of users using System.Net.Mail in .NET.

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

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

发布评论

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

评论(6

夜声 2024-11-17 14:38:33
MailMessage message = new MailMessage();

message.From = new MailAddress("[email protected]");

message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));

message.CC.Add(new MailAddress("[email protected]"));

message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient();

client.Send(message);

您可以在此处NetworkCredential 进行身份验证,请访问“http://jalpesh.blogspot.com/2009/07/sending-email-through.html”rel="nofollow">此处
同步发送?好吧,这取决于你要发送多少。这可能很耗时。
在我必须发送大量电子邮件并且不想挂起我的网络服务器的情况下,我开发了一个自定义 Windows 服务,它定期扫描数据库、收集信息并发送电子邮件。

MailMessage message = new MailMessage();

message.From = new MailAddress("[email protected]");

message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));

message.CC.Add(new MailAddress("[email protected]"));

message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient();

client.Send(message);

You can find more info here or here if you need to use NetworkCredential for authentication.
Sending it synchronously? Well, it depends how many you have to send. It can be time consuming.
In the situation where I have to send bulk emails and I do not want to hung my web server I develop a custom windows services which scans the DB periodically, collects info and sends emails.

酒浓于脸红 2024-11-17 14:38:32

您可能想要使用单独的网络服务来发送电子邮件。您的应用程序会将正文和电子邮件列表发送到网络服务。您可以使用 gzip 或 zip 压缩。这将使 Web 服务调用非常高效(因为数据是文本,压缩率超过 70%)。

现在,在网络服务中,您可以使用多线程或异步操作来发送电子邮件。

编辑:如果您有专用服务器,则创建 Windows 服务可能是更好的选择。在sql server中创建两个表,排队电子邮件和发送电子邮件(存档)。因此,每当用户发表评论时,请更新排队电子邮件表。现在,Windows 服务可以每 30 秒唤醒一次,从排队的电子邮件中提取所有行并异步发送所有行。发送电子邮件后,从表中移动/删除该行。

You might want to use a separate webservice for sending emails. You app will send the body and email list to the webservice. You can use gzip or zip compression. This would make the webservice call very efficient(70%+ compression as the data is text).

Now in the webservice you can use multi-threading or Async operations for sending emails.

Edit: If you have a dedicated server, making a windows service might be a better option. Make a two tables in sql server, Queued Emails and Send Emails(Archive). So whenever user posts a comment, update the Queued emails table. Now the windows service can wake up every 30 seconds, extract all the rows from Queued emails and async send all of them. After an email is send, move/remove the row from the table.

染柒℉ 2024-11-17 14:38:32

看看这个

使用 System.Net.Mail 发送电子邮件

MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");

message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));

message.CC.Add(new MailAddress("[email protected]"));
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient();
client.Send(message);

Have a look at this

Sending Email with System.Net.Mail

MailMessage message = new MailMessage();
message.From = new MailAddress("[email protected]");

message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));

message.CC.Add(new MailAddress("[email protected]"));
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient();
client.Send(message);
靑春怀旧 2024-11-17 14:38:32

我用它来异步发送邮件

SmtpClient smtp = new SmtpClient();
foreach(var mail in mailsToSend)
{
   new Thread(() =>
   {
      smtp.Send(mail);
   }
   ).Start();
}

I use this to send mails asynchronously

SmtpClient smtp = new SmtpClient();
foreach(var mail in mailsToSend)
{
   new Thread(() =>
   {
      smtp.Send(mail);
   }
   ).Start();
}
_畞蕅 2024-11-17 14:38:32

如果您正在考虑使用第三方服务,有一篇不错的文章介绍 使用 .NET 的亚马逊 SES 电子邮件

这对我来说非常有效,但请注意,SendGrid 刚刚宣布了与 Amazon SES 相同的新定价,SendGrid 包含一个 SMTP 包装器,因此代码更加简单。

If you are considering a third-party service, there is a decent article on getting started with Amazon's SES Email using .NET.

This worked very well for me, but note that SendGrid just announced new pricing equal to the Amazon's SES, SendGrid includes an SMTP wrapper so the code is even simpler.

巾帼英雄 2024-11-17 14:38:32

我发布了另一个 StackOverflow 答案,解决了发送大量电子邮件的问题,可以通过 点击此处

当然,最好异步发送电子邮件,以便能够最大限度地提高在固定时间范围内可以发送的电子邮件数量。您可以自己执行此操作或使用已编写的组件来执行此操作。

我使用 Fluent.NET Mail 构建和发送单封电子邮件,我使用MassMailer.NET 发送大量电子邮件。

Fluent.NET 邮件

new Mail()
    .Html(new MessageTemplate()
       .FromText("This is my email with {content} text")
       .DataFrom(new { content = "html" })
       .BetweenBraces())
    .To(new MailBox(emailAddress))
    .From(new MailBox(emailAddress))
    .Subject("Fluent API .NET 2")
    .Timeout(5000)
    .SmtpServer("[your smtp server]")
    .SendAsync();

查看该帖子。

MassMailer.NET

查看此帖子的 有关如何发送大量电子邮件的示例

I posted another StackOverflow answer addressing this issue of sending lots emails which can find by clicking here.

It is of course better to send email asynchronously to be able to maximize on how many can be sent in a fixed time frame. You can do this yourself or use already written components to do it.

I use Fluent.NET Mail to construct and send single emails and I use MassMailer.NET to send large volumes of emails.

Fluent.NET Mail

new Mail()
    .Html(new MessageTemplate()
       .FromText("This is my email with {content} text")
       .DataFrom(new { content = "html" })
       .BetweenBraces())
    .To(new MailBox(emailAddress))
    .From(new MailBox(emailAddress))
    .Subject("Fluent API .NET 2")
    .Timeout(5000)
    .SmtpServer("[your smtp server]")
    .SendAsync();

Check out the post.

MassMailer.NET

Check out this post for an example on how to send a large number of emails.

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