Created
March 4, 2013 14:35
-
-
Save nescalante/5082603 to your computer and use it in GitHub Desktop.
Mvc Mail Sender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Net.Mail; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
public abstract class MailerBase : ControllerBase | |
{ | |
protected override void ExecuteCore() | |
{ | |
} | |
public virtual void Send(string to, string subject = null, object model = null, string viewName = null) | |
{ | |
// get method name | |
var action = viewName ?? new StackFrame(1).GetMethod().Name; | |
this.Send(new[] { to }, subject, model, action); | |
} | |
public virtual void Send(string[] to, string subject = null, object model = null, string viewName = null) | |
{ | |
// get method name | |
var action = viewName ?? new StackFrame(1).GetMethod().Name; | |
// initialize client | |
var client = new SmtpClient(); | |
var message = this.GetMessage(options: m => { | |
m.Subject = subject; | |
// add "to" emails | |
foreach (var i in to) m.To.Add(i); | |
}, | |
model: model, | |
viewName: action | |
); | |
client.Send(message); | |
} | |
public virtual MailMessage GetMessage(Action<MailMessage> options = null, object model = null, string viewName = null) | |
{ | |
ControllerContext controllerContext; | |
var content = string.Empty; | |
// get view engine | |
var action = viewName ?? new StackFrame(1).GetMethod().Name; | |
var view = this.GetView(viewName, out controllerContext); | |
var viewData = new ViewDataDictionary(model); | |
var tempData = new TempDataDictionary(); | |
// get html string | |
using (var writer = new StringWriter()) | |
{ | |
var context = new ViewContext(controllerContext, view.View, viewData, tempData, writer); | |
view.View.Render(context, writer); | |
writer.Flush(); | |
content = writer.ToString(); | |
} | |
// render options and return message | |
return MailerBase.GetMessage(options, content); | |
} | |
private ViewEngineResult GetView(string viewName, out ControllerContext controllerContext) | |
{ | |
// get action name | |
var controller = this.GetType().Name; | |
var routeData = MailerBase.GetRouteData(controller, viewName); | |
// get controller context with current http context | |
var httpContext = HttpContext.Current.Request.RequestContext.HttpContext; | |
controllerContext = new ControllerContext(httpContext, routeData, this); | |
// create view engine and return it | |
return ViewEngines.Engines.FindView(controllerContext, viewName, null); | |
} | |
private static MailMessage GetMessage(Action<MailMessage> messageOptions, string content) | |
{ | |
// assign html to new mail message | |
var message = new MailMessage(); | |
message.IsBodyHtml = true; | |
message.Body = content; | |
if (messageOptions != null) | |
{ | |
messageOptions(message); | |
} | |
return message; | |
} | |
private static RouteData GetRouteData(string controller, string action) | |
{ | |
var routeData = new RouteData(); | |
routeData.Values.Add("controller", controller); | |
routeData.Values.Add("action", action); | |
return routeData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment