public ActionResult SendEmail(EmailViewModel requestModel) { requestModel.EmailTemplates = GetEmailTemplateValues(); if (!ModelState.IsValid) { return View(requestModel); } else { using (var client = new SmtpClient("smtp.gmail.com", 587)) { string _msgBody = string.Empty; string _mailTemplateToUse = string.Empty; client.EnableSsl = true; client.Credentials = new NetworkCredential(requestModel.EmailFrom, requestModel.EmailFromPassword); var mail = new MailMessage(); mail.From = new MailAddress(requestModel.EmailFrom); mail.To.Add(requestModel.EmailTo); mail.Subject = requestModel.EmailSubject; if (requestModel.SelectedTemplate != "1003") { string[] _parts; requestModel.EmailBody = requestModel.EmailBody.Replace(Environment.NewLine, "<br/>"); mail.IsBodyHtml = true; using (StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(SelectMailTemplate(requestModel.SelectedTemplate)))) { _msgBody = reader.ReadToEnd(); } if (requestModel.SelectedTemplate == "1001") { _parts = requestModel.EmailTo.ToString().Split('.'); _msgBody = _msgBody.Replace("{Name}", _parts[0]); _msgBody = _msgBody.Replace("{messageBody}", requestModel.EmailBody); } else if (requestModel.SelectedTemplate == "1002") { _parts = requestModel.EmailTo.ToString().Split('@'); _msgBody = _msgBody.Replace("{Name}", _parts[0]); _msgBody = _msgBody.Replace("{messageBody}", requestModel.EmailBody); } mail.Body = _msgBody; } else if(requestModel.SelectedTemplate == "1003") { mail.Body = requestModel.EmailBody; } if (requestModel.File[0] != null) { foreach (HttpPostedFileBase item in requestModel.File) { var attachment = new Attachment(item.InputStream, item.FileName); mail.Attachments.Add(attachment); } } client.Send(mail); Success(string.Format("<b>{0}</b> Message sent succcessfully!.", "SUCCESS"), true); } } return RedirectToAction("SendEmail"); }
public ActionResult SendEmail() { EmailViewModel _model = new EmailViewModel(); _model.EmailTemplates = GetEmailTemplateValues(); return View(_model); }