Last active
August 29, 2015 14:07
-
-
Save distractdiverge/6c35315a86c1c47ab911 to your computer and use it in GitHub Desktop.
ASP.NET MVC Razor Template Emails
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.Net.Mail; | |
namespace Client.Project.Repository.Services | |
{ | |
public class DefaultEmailProvider : IEmailProvider | |
{ | |
private const int DEFAULT_PORT = 25; | |
private readonly int _smptServerPort; | |
private readonly string _smtpServerHost; | |
private readonly string _toEmailAddress; | |
/// <summary> | |
/// </summary> | |
/// <param name="fromEmailAddress"> The email address to send all emails from (e.g. no-reply@domain.com) </param> | |
/// <param name="smtpServerHost"> </param> | |
/// <param name="toEmailAddress"> </param> | |
/// <param name="smtpServerPort"> </param> | |
public DefaultEmailProvider( | |
string fromEmailAddress, | |
string smtpServerHost, | |
string toEmailAddress, | |
int smtpServerPort = DEFAULT_PORT) | |
{ | |
if (string.IsNullOrEmpty(smtpServerHost)) | |
{ | |
throw new ArgumentException("smtpServerHost cannot be null or empty"); | |
} | |
if (string.IsNullOrEmpty(fromEmailAddress)) | |
{ | |
throw new ArgumentException("fromEmailAddress cannot be null or empty"); | |
} | |
if (string.IsNullOrEmpty(patientContactEmailAddress)) | |
{ | |
throw new ArgumentException("patientContactEmailAddress cannot be null or empty"); | |
} | |
if (string.IsNullOrEmpty(sponsorsContactEmailAddress)) | |
{ | |
throw new ArgumentException("sponsorsContactEmailAddress cannot be null or empty"); | |
} | |
_smtpServerHost = smtpServerHost; | |
_smptServerPort = smtpServerPort; | |
FromEmailAddress = fromEmailAddress; | |
_toEmailAddress = toEmailAddress; | |
} | |
private void Send(string toAddress, string subject, string body) | |
{ | |
var client = new SmtpClient(_smtpServerHost, _smptServerPort); | |
var message = new MailMessage(FromEmailAddress, toAddress, subject, body) {IsBodyHtml = false}; | |
client.Send(message); | |
} | |
public bool TrySend(string toAddress, string subject, string body, out string errorMessage) | |
{ | |
try | |
{ | |
Send(toAddress, subject, body); | |
} | |
catch (Exception e) | |
{ | |
errorMessage = e.Message; | |
return false; | |
} | |
errorMessage = string.Empty; | |
return true; | |
} | |
public void SendEmail(string message) | |
{ | |
Send(_toEmailAddress, "Example Email Subject", message); | |
} | |
public string FromEmailAddress { get; private set; } | |
} | |
} |
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
@{ | |
Layout = null; | |
} | |
@model ContactForm | |
Name: @Model.FirstName @Model.LastName | |
Email Address: @Model.Email | |
Question Type: @Model.QuestionTypeValues.First(t=>t.Value==@Model.SelectedQuestionType).Key.Title | |
Question: | |
@Model.Question |
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.Web.Mvc; | |
using Client.Project.Models.View.Shared; | |
using Client.Project.Repository.Services; | |
namespace Client.Project.Controllers | |
{ | |
public class DefaultEmailController : BaseController, IEmailController | |
{ | |
private readonly IEmailProvider _emailProvider; | |
public DefaultEmailController(IEmailProvider emailProvider) | |
{ | |
_emailProvider = emailProvider; | |
} | |
public void SendEmail(ControllerContext context, ContactForm model) | |
{ | |
ControllerContext = context; | |
_emailProvider.SendEmail(RenderPartialViewToString("~/Email.cshtml", model)); | |
} | |
} | |
} |
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.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using Models.WebOntology; | |
namespace ESI.SBS.Models.View.Shared { | |
public struct ContactForm | |
{ | |
public ContactForm(IEnumerable<QuestionType> questionTypeValues) | |
: this() | |
{ | |
UpdateQuestionTypeValues(questionTypeValues); | |
} | |
[DataType(DataType.Text)] | |
[Required(ErrorMessage = "First name is required.")] | |
public string FirstName { get; set; } | |
[DataType(DataType.Text)] | |
[Required(ErrorMessage = "Last name is required.")] | |
public string LastName { get; set; } | |
[DataType(DataType.EmailAddress)] | |
[Required(ErrorMessage = "Email is required.")] | |
[RegularExpression(@"(.+)@(.+)\.(.+)", ErrorMessage = "A valid email is required.")] | |
public string Email { get; set; } | |
[DataType(DataType.Text)] | |
[Required(ErrorMessage = "Question Type is required.")] | |
[Range(1, double.MaxValue, ErrorMessage = "Question Type is Required")] | |
public int SelectedQuestionType { get; set; } | |
[DataType(DataType.MultilineText)] | |
[Required(ErrorMessage = "Question is required")] | |
public string Question { get; set; } | |
public IDictionary<QuestionType, int> QuestionTypeValues { get; private set; } | |
public void UpdateQuestionTypeValues(IEnumerable<QuestionType> values) | |
{ | |
if (QuestionTypeValues == null) | |
{ | |
QuestionTypeValues = new Dictionary<QuestionType, int>(); | |
} | |
else | |
{ | |
QuestionTypeValues.Clear(); | |
} | |
values = values.ToArray(); | |
var total = values.Count(); | |
for (var i = 0; i < total; i++) | |
{ | |
QuestionTypeValues.Add(values.ElementAt(i), i); | |
} | |
} | |
} | |
} |
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
namespace Client.Project.Repository.Services | |
{ | |
public interface IEmailProvider | |
{ | |
string FromEmailAddress { get; } | |
bool TrySend(string toAddress, string subject, string body, out string errorMessage); | |
void SendEmail(string message); | |
} | |
} |
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.IO; | |
using System.Web.Mvc; | |
using ESI.SBS.Models.Domain.Core; | |
using ExpressScripts.SBS.Routing; | |
using Menu = ESI.SBS.Models.View.Shared.Menu; | |
using Link = ESI.SBS.Models.View.Shared.Link; | |
namespace ExpressScripts.SBS.Controllers | |
{ | |
[OutputCache(CacheProfile = "GlobalPageLevelCache")] | |
public abstract class BaseController : Controller | |
{ | |
protected readonly Lazy<Link> _homepageBreadcrumbLink; | |
protected BaseController() | |
{ | |
_homepageBreadcrumbLink = new Lazy<Link>(() => new Link("Home", Url.RouteExplicitUrl(SiteRoutes.Home))); | |
} | |
protected string RenderPartialViewToString(string viewName, object model) | |
{ | |
if (string.IsNullOrEmpty(viewName)) | |
viewName = ControllerContext.RouteData.GetRequiredString("action"); | |
ViewData.Model = model; | |
using (var sw = new StringWriter()) | |
{ | |
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); | |
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); | |
viewResult.View.Render(viewContext, sw); | |
return sw.GetStringBuilder().ToString(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment