Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate service and viewmodel #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Postal/Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ public Email(string viewName)
ImageEmbedder = new ImageEmbedder();
}

/// <summary>
/// Creates a new Email, that will render the given view using the given model.
/// </summary>
/// <param name="viewName">The name of the view to render</param>
/// <param name="model">The view's model</param>
public Email(string viewName, object model)
{
if (viewName == null) throw new ArgumentNullException("viewName");
if (string.IsNullOrWhiteSpace(viewName)) throw new ArgumentException("View name cannot be empty.", "viewName");
if (model == null) throw new ArgumentNullException("model");

Attachments = new List<Attachment>();
ViewName = viewName;
ViewData = new ViewDataDictionary(model);
ImageEmbedder = new ImageEmbedder();
}

/// <summary>Create an Email where the ViewName is derived from the name of the class.</summary>
/// <remarks>Used when defining strongly typed Email classes.</remarks>
protected Email()
Expand Down
30 changes: 30 additions & 0 deletions src/Postal/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ public void Send(Email email)
}
}

/// <summary>
/// Sends an email using an <see cref="SmtpClient"/>.
/// </summary>
/// <param name="email">The email to send.</param>
/// <param name="viewName">The name of the view to render.</param>
public void Send(string viewName, Email email)
{
using (var mailMessage = CreateMailMessage(viewName, email))
using (var smtp = createSmtpClient())
{
smtp.Send(mailMessage);
}
}

/// <summary>
/// Send an email asynchronously, using an <see cref="SmtpClient"/>.
/// </summary>
Expand Down Expand Up @@ -117,5 +131,21 @@ public MailMessage CreateMailMessage(Email email)
var mailMessage = emailParser.Parse(rawEmailString, email);
return mailMessage;
}

/// <summary>
/// Renders the email view and builds a <see cref="MailMessage"/>. Does not send the email.
/// </summary>
/// <param name="email">The email to render.</param>
/// <param name="viewName">The name of the view to render.</param>
/// <returns>A <see cref="MailMessage"/> containing the rendered email.</returns>
public MailMessage CreateMailMessage(string viewName, Email email)
{
var rawEmailString = emailViewRenderer.Render(email, viewName);
var mailMessage = emailParser.Parse(rawEmailString, email);
return mailMessage;
}



}
}