问题
I want to use MvcMailer in my application. I created a new class library project called MyMailer. Now I want to use it in my MVC app.
// mailer
public class MyMailer : MailerBase, IEdoctorMailer
{
public MyMailer()
{
MasterName = "_Layout";
}
public virtual MvcMailMessage Invitation()
{
//ViewBag.Data = someObject;
var msg = Populate(x =>
{
x.Subject = Labels.Invitation;
x.ViewName = "Invitation";
x.From = new MailAddress("xxx@gmail.com");
x.To.Add("yyy@gmail.com");
});
return msg;
}
}
//mvc controller
IMyMailer mailer = new MyMailer();
var inv = mailer.Invitation();
inv.Send(new DefaultSmtpClient()); // see below
public class DefaultSmtpClient : SmtpClient, ISmtpClient
{
public DefaultSmtpClient() : base("smtp.gmail.com", 587)
{
EnableSsl = true;
UseDefaultCredentials = false;
Credentials = new NetworkCredential("login", "pass");
}
public void SendAsync(MailMessage mailMessage)
{
throw new NotImplementedException();
}
}
In MyMailer project in Views/MyMailer/Invitation.cshtml
there is a file with some text in it.
When I send the email it actually arrives. But this Invitation view is not included (there's no body at all). I assume it's because the Send method is executed form the mvc project, but that's just my guess.
I even put a breakpoint in Views/MyMailer/_Layut.cshtml --> RenderBody()
, but it never stepped into it.
What should I do to include the view?
回答1:
Since you're MvcMailer views are in a class library, you'll have to tell the MVC project where and how to find them by overriding the ViewEngine or VirtualPathProvider.
来源:https://stackoverflow.com/questions/16366656/mcvmailer-doesnt-see-views