sending multiple emails with mvcmailer

邮差的信 提交于 2019-12-05 17:22:51

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.

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("sohan39@example.com");
mailMessage.To.Add("peter23@example.com");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!