1

I have some action. I want send mail. usually i doing this way.

public ActionResult Action(Model model)
{
    string body = 
             "<html>"+
             "<head>"+
             "</head>"+
             "<body>"+
             "</body>"+
             "</html>"

    MailMessage Message = new MailMessage();
    Message.IsBodyHtml = true;
    Message.Subject = "some subject";
    Message.Body = body;
    Message.To.Add(new MailAddress(adres));
    Message.BodyEncoding = Encoding.GetEncoding("utf-8");
    SmtpClient Smtp = new SmtpClient();
    Smtp.EnableSsl = false; 
    Smtp.Send(Message);

    return RedirectToAction("SomeAction","Controller");
}

Can any say how can i generate HTML from PartialView and send this html trough email?

something like this

public ActionResult Action(Model model)
{    
    string str = GetHtmlFromPartialView("NamePartialView",model);


    MailMessage Message = new MailMessage();
    Message.IsBodyHtml = true;
    Message.Subject = "some subject";
    Message.Body = str;
    Message.To.Add(new MailAddress(adres));
    Message.BodyEncoding = Encoding.GetEncoding("utf-8");
    SmtpClient Smtp = new SmtpClient();
    Smtp.EnableSsl = false; 
    Smtp.Send(Message);

    return View();
}
Neil Knight
  • 47,437
  • 25
  • 129
  • 188
Oleksandr Fentsyk
  • 5,256
  • 5
  • 34
  • 41

2 Answers2

2

Checkout MvcMailer. Scott Hanselman also blogged about it. You will see how much easier and fun is to send emails with it. The mail templates are defined as Razor views.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

Use the MvcMailer package.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964