Use MVC razor to generate well-formed HTML body as message content

Source: Internet
Author: User

PS: Instantiate message Class MailMessage has a property isbodyhtml indicates whether the message body is in HTML format.

I want to use the Model binding/rendering feature of Razor view to generate HTML Body Content for the e-mail I send from the ASP.

Is there a way to render a view to a string instead of returning it as a actionresult of a GET request?

To show that I am looking for something that can do the following things ...

     PublicActionResult SendEmail (intID) {Emaildetailsviewmodel emaildetails=Emaildetailsviewmodel ().        Createemaildetails (ID); //This is the WHERE I need help ...//I want to pass my ViewModel (emaildetails) to my View (Emailbodyrazorview) and instead of rending that to the respons E stream I want to capture the output and pass it to an email client.        stringHtmlemailbody = View ("Emailbodyrazorview", Emaildetails).        ToString (); //Once I has the htmlemail body I ' m good to go. I ' ve got a utilityt that would send the email for me.Myemailutility.smtpsendemail ("[email protected]","Email Subject", Htmlemailbody); //Redirect Another Action that would return a page to the user confirming the e-mail was sent.        returnRedirecttoaction ("confirmationemailwassent"); }

If you only need to render the view as a string, try the following:

 Public stringToHtml (stringViewtorender, Viewdatadictionary viewData, ControllerContext controllercontext) {    varresult = ViewEngines.Engines.FindView (ControllerContext, Viewtorender,NULL);    StringWriter output; using(output =NewStringWriter ()) {        varViewContext =NewViewContext (controllercontext, result.        View, ViewData, controllerContext.Controller.TempData, Output); Result.        View.render (viewcontext, Output); Result. Viewengine.releaseview (ControllerContext, result.    View); }    returnoutput. ToString ();}

You need the name of the incoming view and the ViewData and ControllerContext in the controller operation.

You can send e-mail postal using views: https://github.com/andrewdavey/postal

Reference Address: https://cloud.tencent.com/developer/ask/61899

Use MVC razor to generate well-formed HTML body as message content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.