4

I am using MVCMailer and trying to send mail from WebApi, but mail always arrives with an empty message.

view is in /Views/Account/CreateUser.cshtml

and this is my code

public class Account: MailerBase
{

    public MvcMailMessage CreateUser(CreateNewAccount model)
    {
        return Populate(x =>
                            {
                                x.Subject = model.Subject;
                                x.ViewName = "CreateUser";
                                x.To.Add(model.Email);
                                ViewData.Model = model;
                            });
    }

When I send an email from the classic MVC controller, email arrives properly, but send from APiControler mail body is empty.

[RESOLVE] View must be in main Views folder, NOT in an Area. In my situation /Views/Account/CreateUser.cshtml

mir89
  • 95
  • 1
  • 8

1 Answers1

1

I think I figured out a workaround (took me a few hours of digging). In my case I have:

  • a Web API project exposing the REST endpoints
  • an MvcMailer MVC project containing the email templates (views)

The views are simply not found from the Web API project because they are located in the referenced assembly (the MvcMailler project). It's a very common issue apparently. I fixed it by installing the EmbeddedResourceVirtualPathProvider Nuget package (https://github.com/mcintyre321/EmbeddedResourceVirtualPathProvider) and it worked (almost) out of the box!

To understand better what's going on:

Hope it helps :)

Community
  • 1
  • 1
Pedro
  • 3,511
  • 2
  • 26
  • 31