Tag Archives: email

Rendering a View to a String – Email Templates in MVC4

Hey everyone,

This is just a short post showing how to render views as strings. I’ve used this as a quick way to generate email templates. The logic to render a view as a string is as follows:

public static string RenderRazorViewToString(Controller controller, string viewName, string content)
        {
            controller.ViewBag.Content = content;

            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
                var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);

                return sw.GetStringBuilder().ToString();
            }
        }

The Email Template: Views/Emails/DefaultTemplate

@{
    Layout = null;
}



    

    Easy Carts





    
    
@ViewBag.Content

In your controller:

public String TestEmail(String content)
        {
            Email email = new Email() {
                To = "lls@live.com.au",
                From = Email.Fields.From.NoReply,
                Body = content,
                Subject = "Test",
                TemplateName = @"~ViewsEmailsDefaultTemplate.cshtml",
                UseTemplate = true
            };
      
            email.SendEmail(this);
            
            db.Emails.Add(email);
            db.SaveChanges();

            return email.Body.ToString();
        }

If you looking to implement this with your emails, note that it builds on top of the email class we created back in this post: http://www.whatibroke.com/?p=963

If you’re just going to be using it directly within the controller, using the original code from over StackOverflow will probably be your best bet: http://stackoverflow.com/a/2759898/522859

Advertisements

Sending Emails – MVC4 on GoDaddy

Hey everyone,

Just a really basic model that can be used to send emails on GoDaddy in MVC4.

The model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.ComponentModel.DataAnnotations;

namespace LL.Models
{
    public class Email
    {
        [Key]
        public int EmailId { get; set; }

        public string From { get; set; }
        public string To { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public DateTime Sent { get; set; }
        public string Status { get; set; }
        public string Log { get; set; }
        
        
        public void SendEmail()
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(this.To);
            mail.From = new MailAddress(this.From);
            mail.Subject = this.Subject;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.Normal;
            
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "relay-hosting.secureserver.net";
            smtp.UseDefaultCredentials = true;
            smtp.Port = 25;
            smtp.EnableSsl = false;  

            try
            {
                smtp.Send(mail);
                this.Status = Fields.Status.Success;
            }
            catch(Exception e)
            {
                this.Log = e.Message;
            }

            this.Sent = DateTime.Now;
        }

        [NotMapped]
        public class Fields
        {
            public class From
            {
                public const String NoReply = "noreply@yoursite.com.au";
                public const String Support = "support@yoursite.com.au";
            }

            public class Status
            {
                public const String Success = "Success";
                public const String Error = "Error";
            }
        }

    }
}

Using it in a controller:

using LearnerLessons.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LL.Controllers
{
    public class TestController : Controller
    {
        private LLDBContext db = new LLDBContext();
        
        public String TestEmail(String content)
        {
            Email email = new Email() { To = "lls@live.com.au", From = Email.Fields.From.NoReply, Body = content, Subject = "Test" };
            email.SendEmail();

            db.Emails.Add(email);
            db.SaveChanges();

            return email.ToString();
        }
    }
}

Update:
For a quick tutorial on how to use Razor views as email templates, check out the following post: http://www.whatibroke.com/?p=983