I am using ASP.NET MVC 3 with MVCMailer, I tried to send e-mails using SendAsync, but actually it still take longer.
So I am trying to use Task.Factory like the code bellow:
var task1 = Task.Factory.StartNew(
state =>
{
var mail = new UserMailer();
var msg = mail.Welcome("My Name", "myemail@gmail.com");
msg.SendAsync();
});
task1.Wait();
The problem is, MVCMailer needs HttpContext, but inside this task I got HttpContext Null.
How can I send Async e-mails?
You dont need tasks. SendAsync is asynchronous and use another thread self. Tasks dont accelerate you mailing.
UPDATE: When i resolve same problem i use task and synchronous send. It seems that SendAsync was not so asynchrous. It is sample of my code (it is not want HttpContext):
Task.Factory.StartNew
will create a new thread.If you want to access HttpContext which is in the main thread you have to do this:
There a long debate if it's better to use TPL or QueueUserWorkItem.
Someone has tried to address the issue.
This is the QueueUserWorkItem version:
A small addition to this. Here is an extension method to help some.
Use it like follows from your controller methods.