1

I have an MVC3 web application and i am using MvcMailer to send out email (potentially thousands at once). This is working fine if i just BCC each recipient into the same email, but if i try to loop through them sending them individual emails it is incredibly slow, which i guess is because it is connecting to the smtp server again each time?

We have our own smtp server which it is connecting to, and i am currently attempting to send the emails like this:

foreach(Person x in names)
{
    var mail = Mailer.Example_Mail()
    mail.To.Add(x.Email_Address);
    mail.Send();
}

I know from the tutorial that you can save the emails to a file instead of connecting to the smtp server and sending them immediately, and i also know from the accepted answer on this post that you can have IIS pick them up and send them, but unfortunately i have no idea how.

If some one could let me know what the best way of doing this is that would be awesome! thank you all in advance!

UPDATE

I have tried using the SendAsync() method as suggested by @Mike, which does indeed speed things up considerably, but its now losing ~40% of the emails. With no errors being thrown only about 65 of every 100 emails gets through. I tried to catch any errors as suggested here in the "Handle Events for Asynchronous Email" section, but none seem to be thrown at all. Code below:

List<object> errors = new List<object>();
var mail = Mailer.Example_Mail();
mail.To.Add("some@address.com");
for (int i = 0; i < 100; i++)
{
    var client = new SmtpClientWrapper();
    client.SendCompleted += (sender, e) =>
    {
        if (e.Error != null || e.Cancelled)
        {
            errors.Add(e);
        }
    };
    mail.SendAsync("user state object", client);
}
Community
  • 1
  • 1
Ben
  • 5,525
  • 8
  • 42
  • 66
  • Have you tried the SendAsync method? It would at least return control back to the main thread and then Exchange can handle the load. – Mike Perrenoud Jul 30 '12 at 15:54
  • I havent, but i will look into it tomorrow. Having read a bit more i think i would still like to just save them to the server and let IIS take care of it as that way i would not have to worry about the smtp connection being successful at the time of sending, how would `SendAsync` cope with the smtp server being temporarily down? how do you use `SendAsync`? Just swap it for `Send`? – Ben Jul 30 '12 at 16:10
  • If you have that much access to the IIS server then that's the way to go. I'll try and post some code later. – Mike Perrenoud Jul 30 '12 at 16:16
  • Yeah i am the sole admin (doesn't mean i know what i doing to any great extent though!). Thanks Mike, i look forward to seeing it – Ben Jul 31 '12 at 07:41

1 Answers1

0

You could attempt to use the Parallel foreach loop if you are not modifying data and have a decent amount of processing power (eg, more than one core) on your machine.

http://msdn.microsoft.com/en-us/library/dd460720.aspx

This would in essence spawn multiple threads for each item in your iteration. However, be cautious with the approach: http://msdn.microsoft.com/en-us/library/dd997392.aspx.

Basically, do some testing -> this may work well for you and speed up the process, or it may not, really depends on your setup and code configurations.

Tommy
  • 39,592
  • 10
  • 90
  • 121