Reputation: 577
I am trying to send email, normal email I am able to send. But now I have send a template(.cshtml) in email.
Please tell me to how can I do this.
Here is my 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("[email protected]");
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 () {
}
});
}
});
}
Upvotes: 1
Views: 734
Reputation: 6841
I had a similar requirement before and while it is possible to do I found more issues then it was worth so I went a different way. While this does not answer your question directly it is an alternate approach to the problem.
The basic idea was to avoid the complexity of using cshtml. So I used the following basic POCO object and stored it in a 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 to manage the email without recompiling and re-deploying the project.
When I need to use it, I load the template and build out the email message. To help with that I use SmartFormat.NET to do data replacements using the 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 your direct question this path gave me a lot more flexibility without the hassle.
Upvotes: 1