asp.net mvc - How to render .cshtml as Email Body using SendGrid+MVC4? -
i trying send email, normal email able send. have send template(.cshtml) in email.
please tell me how can this.
here code:
controller:
public actionresult sendemaildeviceinfodisplay(list<string> devicedataildata) { string body = ""; var emailcontoller = new emailcontroller(); string email = devicedataildata[9]; body = "<html>event name: " + devicedataildata[0] +"</html>"; var sendemail = emailcontoller.senddeviceandeventinfomail(email, body); viewbag.info = devicedataildata[8]; return partialview("sendemailconformationdisplay"); }
sendgrid method:
public string senddeviceandeventinfomail(string email, string body){ var mymessage = new sendgridmessage(); mymessage.from = new mailaddress("anita.mehta@test.com"); list<string> recipients = new list<string> { email }; mymessage.addto(recipients); mymessage.subject = "information"; mymessage.html = body; var transportweb = new web(nc); transportweb.deliver(mymessage); return "done"; }
ajax call:
$(function () { var loggedinemailid = $('#buildingsession').val(); $('#sendasemaildeviceconformationdialog').dialog({ autoopen: false, width: '27.5em', position: { my: 'top', at: 'top+150' }, opacity: 100, resizable: false, //title: 'product', modal: true, closeonescape: false, open: function (event, ui) { $(".ui-dialog-titlebar-close", ui.dialog | ui).hide(); $.ajax({ url: '/overview/sendemaildeviceinfodisplay', traditional: true, data: data, success: function (result) { $('.ui-button-text').hide(); $('#sendasemaildeviceconformationdialog').html(result); }, error: function () { } }); } });
i had similar requirement before , while possible found more issues worth went different way. while not answer question directly alternate approach problem.
the basic idea avoid complexity of using cshtml. used following basic poco object , stored in database:
public interface iemailtemplate { ... other db related fields string name { get; set; } string fromemail { get; set; } string fromdisplayname { get; set; } string subject { get; set; } string textmessage { get; set; } string htmlmessage { get; set; } emailpriority priority { get; set; } }
this allows me manage email without recompiling , re-deploying project.
when need use it, load template , build out email message. use smartformat.net data replacements using follow logic:
private string datareplacements(string input, object args) { if (input.isnulloremptyorblank()) { return string.empty; } return args != null ? smart.default.format(input, args) : input; } subject = datareplacements(template.subject, datamodel); textmessage = datareplacements(template.textmessage, datamodel); htmlmessage = datareplacements(template.htmlmessage, datamodel);
again while not answering direct question path gave me lot more flexibility without hassle.
Comments
Post a Comment