29

I am trying to send email from my MVC application, it sends fine when I use the .Send() method but takes a while to come back so I wanted to use the .SendMailAsync() function, but I am receiving the following error during execution.

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>

This is my code sample. How can I configure this to send using .SendMailAsync()

Email Wrapper Class:

using System.Net.Mail;

namespace Helpers
{
    public class Email
    {
        // constants
        private const string HtmlEmailHeader = "<html><head><title></title></head><body style='font-family:arial; font-size:14px;'>";
        private const string HtmlEmailFooter = "</body></html>";

        // properties
        public List<string> To { get; set; }
        public List<string> CC { get; set; }
        public List<string> BCC { get; set; }
        public string From { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }  

        // constructor
        public Email()
        {
            To = new List<string>();
            CC = new List<string>();
            BCC = new List<string>();
        }

        // send
        public void Send()
        {
            MailMessage message = new MailMessage();

            foreach (var x in To)
            {
                message.To.Add(x);
            }
            foreach (var x in CC)
            {
                message.CC.Add(x);
            }
            foreach (var x in BCC)
            {
                message.Bcc.Add(x);
            }

            message.Subject = Subject;
            message.Body = string.Concat(HtmlEmailHeader, Body, HtmlEmailFooter);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.From = new MailAddress(From);
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient("relay.mail.server");

            client.SendMailAsync(message);            
        }
    }
}

Controller:

public ActionResult Index()
    {

        Email email = new Email();
        email.To.Add("to@email.com");
        email.From = "from@email.com";
        email.Subject = "Subject";
        email.Body = "<p><strong>Hello</strong></p><p>This is my first Email Message</p>";
        email.Send();
    }

EDIT

Further to the actual question asked, the underlying issue was the delay created when sending emails. I looked further into the actual issue and with the help of this post:

ASP.Net MVC background threads for email creation and sending

modified my Email Wrapper class to spawn off a new thread to perform the email processing:

using System.Net.Mail;

namespace Helpers
{
    public class Email
    {
        // constants
        private const string HtmlEmailHeader = "<html><head><title></title></head><body style='font-family:arial; font-size:14px;'>";
        private const string HtmlEmailFooter = "</body></html>";

        // properties
        public List<string> To { get; set; }
        public List<string> CC { get; set; }
        public List<string> BCC { get; set; }
        public string From { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }  

        // constructor
        public Email()
        {
            To = new List<string>();
            CC = new List<string>();
            BCC = new List<string>();
        }

        // send
        public void Send()
        {
            MailMessage message = new MailMessage();

            foreach (var x in To)
            {
                message.To.Add(x);
            }
            foreach (var x in CC)
            {
                message.CC.Add(x);
            }
            foreach (var x in BCC)
            {
                message.Bcc.Add(x);
            }

            message.Subject = Subject;
            message.Body = string.Concat(HtmlEmailHeader, Body, HtmlEmailFooter);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.From = new MailAddress(From);
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient("relay.mail.server");

            new Thread(() => { client.Send(message); }).Start();        
        }
    }
}
2
  • What version of .Net and MVC are you on? Nov 21, 2013 at 19:26
  • @Stinky - .NET 4.5.1 and MVC 4
    – Ancalagon
    Nov 21, 2013 at 19:48

3 Answers 3

71

Admittedly, the error is a bit obtuse, but all it's really telling you is that you're calling an asynchronous method from a synchronous method, which isn't allowed. If you're going to use async, you have to use async all the way up the chain.

So, first you need to change your Send method definition to return a Task:

public async Task Send()

And set your async method call to await:

await client.SendMailAsync(message);

Then, do the same for your action:

public async Task<ActionResult> Index()

And:

await email.Send();

UPDATE

Async doesn't do what I think you think it does. When your action is invoked by a request, it will not return a response until all code inside the action has fully executed. Async is not a magic wand that makes the action return the response quicker. Your task (in this case, sending an email) takes as long as it takes and async or not, the action will not return a response until the task has completed.

So why use async then? Because what async does do is let go the thread from the server pool. Let's say IIS is running in a pretty standard config, you'll likely have somewhere around 1000 threads available. This is often called the "max requests", because typically 1 request == 1 thread. So, if you server comes under heavy load and you're fielding more than the "max requests", each subsequent request is queued until a thread from the pool becomes available again. If all the threads are tied up waiting on something to complete, then your server essentially deadlocks. But, when you use async, you tell IIS essentially, "I'm waiting on something. Here's my thread back, so you can use it to field another request. I'll let you know when I need it back." That allows requests in the queue to proceed.

Long and short, do always use async when you are doing anything that involves waiting, because it allows server resources to be used more efficiently, but remember that it doesn't make things happen quicker.

EDIT 12/11/14 - Updated terminology a bit to make clear that async is only useful when a thread is waiting, not just involved in some long-running task. For example, running complex financial calculations could "take a while", but would not be a good fit for async because all the work is CPU-bound. The task may be long-running, but if the thread is not in a wait-state, it can't be used for other tasks and your async method will essentially just run as sync, but with extra overhead.

10
  • Fantastic. Worked like a charm. Although initially it still had the same delay I was experiencing with using just the .Send() method, but I modified your suggestion code and removed the await from the email.Send(); call in my controller, and then the remaining items processed right after the call. Thanks so much, saved me some time!
    – Ancalagon
    Nov 21, 2013 at 19:43
  • Actually, a little quick on the response there. I am still seeing the delay and VS is complaining that I removed the await from the email.Send() method. Not sure if this approach is what I need to do, but regardless your answer did answer my question so thanks again.
    – Ancalagon
    Nov 21, 2013 at 20:02
  • Please see my update. Async is a very confusing thing for most developers and very few understand what is actually happening. Nov 21, 2013 at 20:29
  • Right on, thank you again for this. I'd upvote your answer too, but I don't have enough Rep... lol, so here is a +beer
    – Ancalagon
    Nov 21, 2013 at 21:00
  • @ChrisPratt: Perfect answer with superb explanation... but in my case i don't want to wait and i need to return my actionResult to my view. So in that case what i have done is - email.Send().ConfiGureAwait(false). But i don't see any effect!!. Could you please point me out that what i am missing?? Thanks in advance. Dec 28, 2014 at 17:24
4

This might help you out.

    public void Send(MailAddress toAddress, string subject, string body, bool priority)
    {
        Task.Factory.StartNew(() => SendEmail(toAddress, subject, body, priority), TaskCreationOptions.LongRunning);
    }

    private void SendEmail(MailAddress toAddress, string subject, string body, bool priority)
    {
        MailAddress fromAddress = new MailAddress(WebConfigurationManager.AppSettings["SmtpFromAddress"]);
        string serverName = WebConfigurationManager.AppSettings["SmtpServerName"];
        int port = Convert.ToInt32(WebConfigurationManager.AppSettings["SmtpPort"]);
        string userName = WebConfigurationManager.AppSettings["SmtpUserName"];
        string password = WebConfigurationManager.AppSettings["SmtpPassword"];

        var message = new MailMessage(fromAddress, toAddress);

        message.Subject = subject;
        message.Body = body;
        message.IsBodyHtml = true;
        message.HeadersEncoding = Encoding.UTF8;
        message.SubjectEncoding = Encoding.UTF8;
        message.BodyEncoding = Encoding.UTF8;
        if (priority) message.Priority = MailPriority.High;

        Thread.Sleep(1000);

        SmtpClient client = new SmtpClient(serverName, port);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["SmtpSsl"]);
            client.UseDefaultCredentials = false;

            NetworkCredential smtpUserInfo = new NetworkCredential(userName, password);
            client.Credentials = smtpUserInfo;

            client.Send(message);

            client.Dispose();
            message.Dispose();
    }

The Thread.Sleep is there because this will send mail through so fast that many SMTP servers will report too many emails from same IP error message. Although ASP.NET handles asynchronous send mail, it will not send more than one message at a time. It waits until callback occurs before sending another email. This approach will send messages in parallel as fast as the code can call Send().

2
  • 2
    Mr @FredChateau, with all the respect may I suggest some drawbacks of this implementation that I think are important : It is synchronous. It can block the thread for a long time. Just imagine using it in Application_Error event in ASP.NET and the SMTP server is overloaded or gone. This implementation is dangerous too. In MSDN docs it is written that SmtpClient.Send can throw a bunch of exceptions which are not handled at all in this particular implementation. Jul 26, 2014 at 15:31
  • Good solution @FredChateau, but I don't think Thread.Sleep(1000) will fix your potential problem. What if 500 emails are all sent at the same time? Won't their sleep counter start at the same instance, leading to all 500 emails being sent at the same instance 1000ms later? Unless I misunderstood.
    – ferr
    Aug 23, 2017 at 16:26
3

I think the below does what you're trying to accomplish:

Modify your controller as below:

public async Task<ActionResult> Index()
{
    Email email = new Email();
    email.SendAsync();
}

And in your Email class add the SendAsync method as below

public async Task SendAsync()
{
    await Task.Run(() => this.send());
}

The action will return before the email is sent and the request will not be blocked.

Try to see the behaviour with default mvc template with this code:

[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
    LongRunningTaskAsync();
    return View();
}

public static async Task LongRunningTaskAsync()
{
    await Task.Run(() => LongRunningTask());
}

public static void LongRunningTask()
{
    Debug.WriteLine("LongRunningTask started");
    Thread.Sleep(10000);
    Debug.WriteLine("LongRunningTask completed");
}

The login page will be displayed instantly. But output window will display "LongRunningTask completed" 10 seconds later.

2
  • The last line is entirely incorrect. The action will definitely not return until all work has been completed, including sending the email. Async makes zero difference in how fast the action can return. In fact, in many cases, it will slow it down, since there's extra overhead. Also, Task.Run will block, and you're actually spinning up a new thread, so now your action is blocking two threads instead of just one. Aug 23, 2016 at 12:48
  • Yes you're using two threads until the action returns. But the action does return before the email is sent. Give it a try.
    – Emre Kenci
    Aug 23, 2016 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.