Sending multiple emails using mvcmailer

I want to use MVCMailer to send emails using asp.net mvc 3 with a razor. Also mentioned by ScottHa

It looks pretty straight forward, however, I am confused about how I will send periodic emails, for example, as a newsletter for a list of users.

create a loop around this?

public virtual MailMessage Welcome() { var mailMessage = new MailMessage{Subject = "Welcome to MvcMailer"}; mailMessage.To.Add(" sohan39@example.com "); ViewBag.Name = "Sohan"; PopulateBody(mailMessage, viewName: "Welcome"); return mailMessage; } 

can anyone explain? thanks

+4
source share
2 answers

Unfortunately, since each email is personalized, I see no other way than a loop. 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 this method inside your loop and send it at the same time.

Important Note

You must configure your web.config to use the pickup directory, not the SMTP server. Then start IIS to send an email from the pickup directory.

Reasoning. Since you can potentially call SmtpClient.Send(MailMessage mailmessage) any number of times, this can become quite expensive if you need to connect to the SMTP server each time to send e-mail.

A good side effect of this is that you also get some redundancy if the SMTP server is unavailable or unavailable for any reason.

+2
source

If you want different content for each email, you need to create separate MailMessage objects using a loop. If you want to have the same content, you can simply add multiple recipients:

 mailMessage.To.Add(" sohan39@example.com "); mailMessage.To.Add(" peter23@example.com "); 
+1
source

All Articles