ASP.NET MVC Email

2019-03-08 04:48发布

Is their a solution to generate an email template using an ASP.NET MVC View without having to jump through hoops.

Let me elaborate jumping through hoops.

var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
var oldContext = HttpContext.Current;
HttpContext.Current = fakeContext;
var html = new HtmlHelper(new ViewContext(fakeControllerContext,
  new FakeView(), viewDataDictionary, new TempDataDictionary()),
  new ViewPage());
html.RenderPartial(viewName, viewData, viewDataDictionary);
HttpContext.Current = oldContext;

The above code is using the current HttpContext to fake a new Context and render the page with RenderPartial, we shouldn't have to do this.

Another very detailed solution using ControllerContext and .Render: (IEmailTemplateService, Headers/Postback WorkAround) but pretty much doing the same thing with a lot more code.

I on the other hand, am looking for something that would just render a View without having to POST/GET and generates me a simple string that I can send off through my Email code. Something that doesn't run into errors such as posting headers twice or faking some piece of data.

EX:

//code which does not fire Render, RenderPartial... etc
var email = emailFramework.Create(viewData, view); 

See my solution bellow or follow this link:

My Solution using spark: (12/30/2009) ASP.NET MVC Email Template Solution

7条回答
The star\"
2楼-- · 2019-03-08 05:15

If you want simple text replacements, .NET has something for that:

        ListDictionary replacements = new ListDictionary();

        // Replace hard coded values with objects values
        replacements.Add("{USERNAME}", "NewUser");            
        replacements.Add("{SITE_URL}", "http://yourwebsite.com");
        replacements.Add("{SITE_NAME}", "My site's name");

        string FromEmail= "from@yourwebsite.com";
        string ToEmail = "newuser@gmail.com";

        //Create MailDefinition
        MailDefinition md = new MailDefinition();

        //specify the location of template
        md.BodyFileName = "~/Templates/Email/Welcome.txt";
        md.IsBodyHtml = true;
        md.From = FromEmail;
        md.Subject = "Welcome to youwebsite.com ";

        System.Web.UI.Control ctrl = new System.Web.UI.Control { ID = "IDontKnowWhyThisIsRequiredButItWorks" };

        MailMessage message = md.CreateMailMessage(ToEmail , replacements, ctrl);

        //Send the message
        SmtpClient client = new SmtpClient();

        client.Send(message);

And the Welcome.txt file

    Welcome - {SITE_NAME}<br />
    <br />
    Thank you for registering at {SITE_NAME}<br />
    <br />
    Your account is activated and ready to go! <br />
    To login, visit <a href="{SITE_URL}">{SITE_NAME}</a> and use the following credentials:
    <br />
    username: <b>{USERNAME}</b><br />
    password: use the password you registered with
    <br />
    <br />

    - {SITE_NAME} Team

Again, this is only good for simple string replacements. If you plan emailing more data, you would need to format it properly then replace it.

查看更多
登录 后发表回答