Skip to content Skip to sidebar Skip to footer

Asp.net Mvc: How To Send An Html Email Using A Controller?

What would be the simplest way to send a customised html email using asp.net? I suppose ideally I would like to send html via email rather than returning it to the browser via a Ac

Solution 1:

This blog post has a good solution for rendering a View to a string so you can send it in email.

/// Static Method to render string - put somewhere of your choosingpublicstatic string RenderPartialToString(string controlName, object viewData)
{
     ViewDataDictionaryvd=newViewDataDictionary(viewData);
     ViewPagevp=newViewPage { ViewData = vd };
     Controlcontrol= vp.LoadControl(controlName);

     vp.Controls.Add(control);

     StringBuildersb=newStringBuilder();
     using (StringWritersw=newStringWriter(sb))
     {
         using (HtmlTextWritertw=newHtmlTextWriter(sw))
         {
             vp.RenderControl(tw);
         }
     }

     return sb.ToString();
}

Solution 2:

I think sending email in mvc is just the same as in web form, you just need to set the atribute of the mail message to html enabled then it is food to go. Like this code

MailMessagemm=newMailMessage(emmailFrom,emailTo);
mm.Subject = "Your Subject";
mm.IsBodyHtml = true;
mm.Body = body.ToString();

SmtpClientsmtp=newSmtpClient();
smtp.Send(mm);

Solution 3:

I use MVC Mailer for all my Email Needs

see the project link below for more information

https://github.com/smsohan/MvcMailer

Solution 4:

    [HttpPost]
    public ActionResult SendEmail(string Type, string name, int Id, string subject, string message, HttpPostedFileBase uploadFile)
    {


        try
        {

            if (ModelState.IsValid)
            {
                var abc = _salesInvoiceMasterService.GetallInvoices().Where(a => a.TransId == Id).FirstOrDefault();

                var xyz = _accountMasterMainService.GetAllData().Where(a => a.Id == abc.CustId).FirstOrDefault();

                var mm = xyz.Email;
                if (mm == null)
                {
                    string isCheckNull = "No";
                    return Json(isCheckNull, JsonRequestBehavior.AllowGet);

                }

                var Sendermail = _systemSettingService.GetSetting().Where(a => a.BranchId == branchId && a.CompanyId == companyId && a.FinancialId == financialYId).FirstOrDefault();
                if (Sendermail.UserName == null)
                {
                    string isCheckNull = "Not";
                    return Json(isCheckNull, JsonRequestBehavior.AllowGet);
                }
                var User = Sendermail.UserName;
                var senderEmail = new MailAddress(Sendermail.UserName.ToString(), "Manabh Software");
                var receiverEmail = new MailAddress(mm, "Receiver");

                var password = Sendermail.Password;
                if (password == null)
                {
                    string isCheckNull = "PassNot";
                    return Json(isCheckNull, JsonRequestBehavior.AllowGet);
                }
                var sub = subject;
                var body = message;
                var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = true,
                    Credentials = new NetworkCredential(senderEmail.Address, password.ToString())


                };



                using (MailMessage mail = new MailMessage(senderEmail, receiverEmail))

                {
                    mail.Subject = subject;

                    mail.Body = message;                    

                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment("D:/Users/Manabh/Downloads/SalesInvoice_" + Type + "_" + name + "_" + Id + ".pdf");

                    mail.Attachments.Add(attachment);

                    smtp.Send(mail);
                }

                return View();


            }
        }
        catch (Exception e)
        {

            string isCheckNull = "NotExist";
            return Json(isCheckNull, JsonRequestBehavior.AllowGet);

        }
        return View();
    }

Solution 5:

You need also to add below codes before sending mail:

mailMessage.IsBodyHtml = true;

Post a Comment for "Asp.net Mvc: How To Send An Html Email Using A Controller?"