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