使用 mvcmailer 发送多封电子邮件
我希望使用 MVCMailer 使用 asp 发送电子邮件。 Net MVC 3 与剃刀。 ScottHa 也提到
它看起来相当简单,但是我对如何我感到困惑将批量发送电子邮件,例如向用户列表发送新闻通讯。
我要围绕这个创建一个循环吗?
public virtual MailMessage Welcome()
{
var mailMessage = new MailMessage{Subject = "Welcome to MvcMailer"};
mailMessage.To.Add("[email protected]");
ViewBag.Name = "Sohan";
PopulateBody(mailMessage, viewName: "Welcome");
return mailMessage;
}
有人可以解释一下吗? 谢谢
Im looking to use MVCMailer to send emails using asp.net mvc 3 with razor. Also mentioned by ScottHa
It looks fairly straight forward, however i'm confused as to how I would send batch emails eg like a newsletter to a list of users.
do i create a loop around this?
public virtual MailMessage Welcome()
{
var mailMessage = new MailMessage{Subject = "Welcome to MvcMailer"};
mailMessage.To.Add("[email protected]");
ViewBag.Name = "Sohan";
PopulateBody(mailMessage, viewName: "Welcome");
return mailMessage;
}
can someone explain?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,因为每封电子邮件都是个性化的,所以除了循环之外我看不到任何其他方式。因此,只需将您的方法更改为类似:
然后在循环内调用该方法并同时发送它。
重要提示
您应该将 web.config 设置为使用拾取目录而不是 SMTP 服务器。然后让 IIS 从拾取目录发送电子邮件。
推理 - 因为您可能会多次调用
SmtpClient.Send(MailMessage mailmessage)
- 如果您每次都必须连接到 SMTP 服务器来发送电子邮件,这可能会变得相当昂贵。这样做的一个很好的副作用是,如果 SMTP 服务器因任何原因关闭或无法访问,您还会获得一些冗余。
Unfortunately because each email message is personalized, I can't see any other way other than looping. So just change your method to something like:
And then call that method inside your loop and send it at the same time.
Important Note
You should setup your web.config to use a pickup directory rather than a SMTP server. Then get IIS to send the email from the pickup directory.
Reasoning - Because you could potentially be calling
SmtpClient.Send(MailMessage mailmessage)
any number of times - this could become rather expensive if you have to connect to a SMTP server each time to send the email.A nice side effect of this is you also get some redundancy if the SMTP server is down or unreachable for any reason.
如果您希望每封电子邮件都有不同的内容,则需要使用循环创建单独的
MailMessage
对象。如果您想要相同的内容,那么您可以添加多个收件人:If you want different content for each email, you'll need to create individual
MailMessage
objects using a loop. If you want the same contents, then you can just add multiple recipients: