[HttpPost]
public ActionResult Index(MailModel objModelMail, HttpPostedFileBase fileUploader)
{
try
{
if (objModelMail.To!=null)
{
Thread email = new Thread(delegate()
{
string from = "muthujuna@gmail.com";
using (MailMessage mail = new MailMessage())
{
string[] ToAddress = { objModelMail.To };
mail.From = new MailAddress(from);
mail.Subject = objModelMail.Subject;
mail.Body = objModelMail.Body;
foreach (string address in ToAddress)
{
mail.To.Add(address);
}
if (fileUploader != null)
{
string fileName = Path.GetFileName(fileUploader.FileName);
mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
}
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential networkCredential = new NetworkCredential(from, "XXXXXX");
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Timeout = int.MaxValue;
smtp.Port = 587;
smtp.Send(mail);
}
});
email.IsBackground = true;
email.Start();
var To = objModelMail.To;
ParsingTemplate(To);
ViewBag.Success = "hi, your Mail Send SuccessFully";
}
else
{
ViewBag.Error = "Error occured while processing your request";
}
}
catch (Exception e)
{
ViewBag.Error = ("Could not send the e-mail - error: " + e.Message);
}
return View("Index", objModelMail);
}
private void ParsingTemplate(string To)
{
string templateFilePath = System.Web.HttpContext.Current.Server.MapPath("~/EmailTemplate/EmailTemplateTest.html");
var templateAsString = System.IO.File.ReadAllText(templateFilePath);
var myViewModel = new TestEmailTemplateViewModel
{
UserFullName = "Mohamed Nady",
UserName = "NADY",
SiteUrl = "http://www.mohnady.com",
RegDate = DateTime.Now.ToString()
};
i AM GETTING ERROR THIS LINE:
string body = RazorEngine.Engine.Razor.RunCompile(templateAsString, "templateKey", typeof(TestEmailTemplateViewModel), myViewModel);
SendEmail("muthujuna@gmail.com", To, "testEmail", body);
}
private void SendEmail(string from, string To, string subject, string body)
{
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress(To));
mail.From = new MailAddress(from);
mail.Subject = subject;
string Body = body;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential
("muthujuna@gmail.com", "XXXXX");
smtp.EnableSsl = true;
smtp.Send(mail);
}
What I have tried:
hi,
I am Getting Error This line ,
string body = RazorEngine.Engine.Razor.RunCompile(templateAsString, "templateKey", typeof(TestEmailTemplateViewModel), myViewModel);
Actually i am trying to send the mail with one EmailTemplate but one Error has occured what my problem is
1.first mail has send when i enter ToAddress that muthujuna@gmail.com
2.if suppose i enter someother To Address Means am getting an Error which i put it Top
"The specified string is not in the form required for an e-mail"
Please help me if konow this