andrewdavey / postal

Email sending for asp.net mvc using the view engine system to render emails.
http://aboutcode.net/postal
MIT License
536 stars 168 forks source link

ArgumentNullException - Parameter controllerContext is null #53

Closed regenrek closed 11 years ago

regenrek commented 11 years ago

Hi,

after upgrading from MVC3 to MVC4 (I think thats the time where the issue came up...) I got an error when I want to send the mail.

Here my test code:

            dynamic email = new Email("PrivateMessage");
            email.To = "example@example.com";
            email.Message = "asdf";
            email.mailFrom = i.mailFrom;
            email.BenutzerID = i.BenutzerID;
            email.mailFromAddress = "example@example.com";
            email.Subject = "Subject"
            email.mailTo = i.mailTo;
            email.Date = DateTime.Now.ToString("g");

            emailServ.Send(email);

It looks like the error is here EmailViewRenderer.cs - viewEngines.FindView(controllerContext <-- is null)

    IView CreateView(string viewName, ControllerContext controllerContext)
    {
        var result = viewEngines.FindView(controllerContext, viewName, null);
        if (result.View != null)
            return result.View;

        throw new Exception(
            "Email view not found for " + viewName + 
            ". Locations searched:" + Environment.NewLine +
            string.Join(Environment.NewLine, result.SearchedLocations)
        );
    }

![Uploading Unbenannt-1.png . . .]()

regenrek commented 11 years ago

unbenannt-1

regenrek commented 11 years ago

I could solve the problem somehow. Not very clean but it works. What I did was to extended the .Send() method with the ControllerContext.

So i ended up with this

            dynamic email = new Email("notification");

            email.<foo> = "lorem@ipsum.com";
            // and so on

            emailService.Send(email, ControllerContext);  // I use the ControllerContext from the Current Controller

            // After sending the E-Mail I'm updating the RouteData with the Current Controller 
            ControllerContext.RequestContext.RouteData.Values["controller"] = "Notification";