My MVC Mailer instance is failing when I send from the server. I'm getting the error Value cannot be null.Parameter name: httpContext
when I attempt to send an email.
my code looks like this:
public static void SendApproval(TimeOffRequest request)
{
MailService mailer = new MailService();
mailer.ViewData.Model = request;
MvcMailMessage mailMessage = new MvcMailMessage
{
Subject = $"PTO request approved for {request.StartTime} to {request.EndTime}",
From = new MailAddress("calendar@company.com"),
};
mailMessage.To.Add(new MailAddress(request.Requester.Email));
if (mailMessage.To.Count == 0)
return;
mailer.PopulateBody(mailMessage, "RequestApproved");
mailMessage.Send();
}
It's specifically failing on the line mailer.PopulateBody(mailMessage, "RequestApproved");
It is being called from the post of a controller endpoint. Why is the context missing in this scenario? I attempted to manually pass the context and that failed as well.
Heres the top of the stack trace:
System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext) +505
Mvc.Mailer.MailerBase.CreateControllerContext() +98
Mvc.Mailer.MailerBase.ViewExists(String viewName, String masterName) +127