I want to create a process in my ASP.NET application that I trigger manually and will send a bunch of emails to my users. Since this process takes a while I am creating a ne
I'm guessing this has to do with the fact that the controller context is no longer valid in the new thread, but I'm not sure.
Precisely.
So, what is the best way to approach this?
Send emails from another process, not your ASP.NET MVC application.
What alternatives do I have?
One possibility is to use RazorEngine:
private void SendMessagesLongRunningProcess()
{
var users = GetUsers();
foreach (var user in users)
{
string view = Server.MapPath("~/Views/MailTemplates/SomeTemplate.cshtml");
string template = System.IO.File.ReadAllText(view);
string message = Razor.Parse(template, user);
SendEmail(user.email, message);
}
}
You might also checkout MvcMailer and send the email asynchronously (take a look at the Send Email Asynchronously
section of the step-by-step-guide).