Easy Asynch emails in MVC 3

Here’s a workaround if you’re facing issues implementing the Smtp.SendAsync() method. It doesn’t require any change in your actions or controllers (like changing them to be AsycController).

If  you got one, you can implement the below in your service layer. The original code is in Vb.Net, like the project I am working on…sorry . Btw I’ve converted with one of the several tools online.

Code Snippet
public void SendEmail(MailManagerSettings mailManagerSettings, bool isAsynch)
    object smtp = new SmtpClient {
        Host = mailManagerSettings.Host,
        Port = mailManagerSettings.Port,
        EnableSsl = mailManagerSettings.EnableSSL,
        DeliveryMethod = mailManagerSettings.DeliveryMethod,
        Credentials = new NetworkCredential(mailManagerSettings.Username, mailManagerSettings.Password)

    TriggerMailDelegate triggerHandler = new TriggerMailDelegate(smtp.Send);
    AsyncCallback sendCompleteCallback = new AsyncCallback(SendEmailCbk);

    // We send an email to each user in the ToAddress list

    foreach ( address in mailManagerSettings.ToAddress) {
        object message = new MailMessage(mailManagerSettings.Username, address) {
            Subject = mailManagerSettings.Subject,
            Body = mailManagerSettings.Body,
            IsBodyHtml = mailManagerSettings.IsBodyHtml

        foreach ( attachment in _attachments) {
            message.Attachments.Add(new Attachment(attachment));
        //Asynch send
        if ((isAsynch)) {
            triggerHandler.BeginInvoke(message, sendCompleteCallback, triggerHandler);
        } else {


public void AddAttachment(string path)

private delegate void TriggerMailDelegate(System.Net.Mail.MailMessage message);

private static void SendEmailCbk(IAsyncResult result)
    TriggerMailDelegate sendHandler = (TriggerMailDelegate)result.AsyncState;

No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *