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;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s