使用 mvcmailer 发送多封电子邮件

发布于 2024-11-04 16:33:09 字数 820 浏览 1 评论 0原文

我希望使用 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 技术交流群。

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

发布评论

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

评论(2

月亮是我掰弯的 2024-11-11 16:33:09

不幸的是,因为每封电子邮件都是个性化的,所以除了循环之外我看不到任何其他方式。因此,只需将您的方法更改为类似:

public virtual MailMessage Welcome(string email, string name)
{
    var mailMessage = new MailMessage{Subject = "Welcome to MvcMailer"};

    mailMessage.To.Add(email);
    ViewBag.Name = name;
    PopulateBody(mailMessage, viewName: "Welcome");

    return mailMessage;
}

然后在循环内调用该方法并同时发送它。

重要提示

您应该将 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:

public virtual MailMessage Welcome(string email, string name)
{
    var mailMessage = new MailMessage{Subject = "Welcome to MvcMailer"};

    mailMessage.To.Add(email);
    ViewBag.Name = name;
    PopulateBody(mailMessage, viewName: "Welcome");

    return mailMessage;
}

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.

酒与心事 2024-11-11 16:33:09

如果您希望每封电子邮件都有不同的内容,则需要使用循环创建单独的 MailMessage 对象。如果您想要相同的内容,那么您可以添加多个收件人:

mailMessage.To.Add("[email protected]");
mailMessage.To.Add("[email protected]");

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:

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