Skip to content

Instantly share code, notes, and snippets.

@eralston
Last active August 9, 2020 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eralston/d68287b6634f963c09c7 to your computer and use it in GitHub Desktop.
Save eralston/d68287b6634f963c09c7 to your computer and use it in GitHub Desktop.
A set of helpers to enable ASP.Net MVC Controllers to render their views and send them as e-mail
/// <summary>
/// Extension methods for the system MVC controller and related classes
/// </summary>
public static class ControllerExtensions
{
/// <summary>
/// Renders a view to string using the given model
/// </summary>
/// <param name="controller"></param>
/// <param name="viewName"></param>
/// <param name="model"></param>
/// <returns></returns>
public static string RenderRazorViewToString(this Controller controller, string viewName, object model)
{
controller.ViewData.Model = model;
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();
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net.Mime;
/// <summary>
/// A set of extensions that simplifies interaction with the MailMessage class
/// </summary>
public static class MailExtensions
{
/// <summary>
/// Adds a to Address to the message, optionally setting the display name
/// </summary>
/// <param name="message"></param>
/// <param name="address"></param>
/// <param name="displayName"></param>
public static void AddTo(this MailMessage message, string address, string displayName = null)
{
message.To.Add(new MailAddress(address, displayName));
}
/// <summary>
/// Sets the from address for the message, optionally setting the display name
/// </summary>
/// <param name="message"></param>
/// <param name="address"></param>
public static void SetFrom(this MailMessage message, string address, string displayName = null)
{
message.From = new MailAddress(address, displayName);
}
/// <summary>
/// Adds an attachment to the e-mail
/// </summary>
/// <param name="message"></param>
/// <param name="bytes"></param>
/// <param name="name"></param>
/// <param name="contentType"></param>
public static void AddAttachment(this MailMessage message, byte[] bytes, string name, string contentType)
{
using (MemoryStream stream = new MemoryStream(bytes))
{
message.Attachments.Add(new Attachment(stream, name, contentType));
}
}
/// <summary>
/// Sets the body of the message, optionally apply an html version
/// </summary>
/// <param name="message"></param>
/// <param name="text"></param>
/// <param name="html"></param>
public static void SetBody(this MailMessage message, string text, string html = null)
{
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
if (!string.IsNullOrEmpty(html))
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
}
/// <summary>
/// Sends the given message using the SMTP settings
/// </summary>
/// <param name="message"></param>
/// <param name="smtpName"></param>
/// <param name="username"></param>
/// <param name="password"></param>
public static void SendMailSmtp(this MailMessage message, string smtpName, string username, string password)
{
using (SmtpClient smtpClient = new SmtpClient(smtpName, Convert.ToInt32(587)))
{
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(username, password);
smtpClient.Credentials = credentials;
smtpClient.Send(message);
}
}
/// <summary>
/// Sends the given message using the SMTP settings, using the settings in the web.config
/// </summary>
/// <param name="message"></param>
/// <param name="smtpName"></param>
/// <param name="username"></param>
/// <param name="password"></param>
public static void SendMailSmtp(this MailMessage message)
{
using (SmtpClient smtpClient = new SmtpClient())
{
smtpClient.Send(message);
}
}
}
/// <summary>
/// Sends an e-mail to the target user
/// </summary>
/// <param name="targetUser"></param>
/// <param name="controller"></param>
/// <param name="subject"></param>
/// <param name="emailViewName"></param>
/// <param name="model"></param>
static void SendEmail(string toEmail, Controller controller, string subject,
string emailViewName, object model = null)
{
string body = controller.RenderRazorViewToString(emailViewName, model);
MailMessage message = new MailMessage();
message.SetFrom(FromEmailAddress);
message.SetBody(body, body);
message.AddTo(toEmail);
message.Subject = subject;
message.SendMailSmtp();
System.Diagnostics.Trace.TraceInformation("Sent e-mail to '{0}' with subject '{1}'", toEmail, subject);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment