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

New method to allow for saving email to file which also allows using of Send() to send the email #101

Open
wants to merge 3 commits 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
25 changes: 12 additions & 13 deletions build/Postal.Mvc5.nuspec
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>Postal.Mvc5</id>
<version>1.2.0</version>
<title>Postal for MVC5</title>
<authors>Andrew Davey</authors>
<owners>Andrew Davey</owners>
<licenseUrl>http://www.opensource.org/licenses/mit-license.php</licenseUrl>
<projectUrl>https://github.com/andrewdavey/postal</projectUrl>
<iconUrl>http://aboutcode.net/postal/favicon.ico</iconUrl>
<id>Postal-Custom.Mvc5</id>
<version>1.0.1</version>
<title>Postal-Custom.Mvc5</title>
<authors>mkimmet</authors>
<owners>mkimmet</owners>
<licenseUrl>http://www.opensource.org/licenses/mit-license.php</licenseUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Generate emails using ASP.NET MVC views</description>
<copyright>Copyright Andrew Davey 2014</copyright>
<tags>Email</tags>
<frameworkAssemblies>
<description>Forked from Andrew Davey's Postal.mvc5 this adds just one extra feature to allow both sending an email and saving to file.</description>
<releaseNotes>Added SaveToFile convenience function.</releaseNotes>
<copyright>Original code Copyright Andrew Davey 2014</copyright>
<tags>Email MVC5</tags>
<frameworkAssemblies>
<frameworkAssembly assemblyName="System.Web" />
</frameworkAssemblies>
<dependencies>
<dependency id="Microsoft.AspNet.Mvc" version="5.1.2" />
<dependency id="RazorEngine" version="3.4.1" />
<dependency id="RazorEngine" version="3.4.1" />
</dependencies>
</metadata>
<files>
Expand Down
11 changes: 11 additions & 0 deletions src/Postal/Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ public Task SendAsync()
return CreateEmailService().SendAsync(this);
}

/// <summary>
/// Convenience method that saves a copy of the email to a file. Using this you can
/// save a file and then also use the Send() function to send the email.
/// </summary>
/// <param name="path">The full path to the folder where you want to save the file.
/// For example, "c:\emails" </param>
public void SaveToFile(string path)
{
CreateEmailService().SaveToFile(this, path);
}

/// <summary>
/// A function that returns an instance of <see cref="IEmailService"/>.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions src/Postal/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ public Task SendAsync(Email email)
}
}

/// <summary>
/// Saves a copy of the email to a file. Using this you can
/// save a file and then also use the Send() function to send the email.
/// </summary>
/// <param name="path">The full path to the folder where you want to save the file.
/// <param name="email">The email to send.</param>
/// For example, "c:\emails" </param>
public void SaveToFile(Email email, string path)
{
using (MailMessage mailMessage = CreateMailMessage(email))
using (var smtp = createSmtpClient())
{
var service = new EmailService();
SmtpClient client = new SmtpClient("SaveEmailSMTPClient");
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = path;
client.Send(mailMessage);
client.Dispose();
}
}

/// <summary>
/// Renders the email view and builds a <see cref="MailMessage"/>. Does not send the email.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Postal/IEmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public interface IEmailService
/// <returns>A <see cref="Task"/> that can be used to await completion of sending the email.</returns>
Task SendAsync(Email email);

/// <summary>
/// Saves a copy of the email to a file. Using this you can
/// save a file and then also use the Send() function to send the email.
/// </summary>
/// <param name="path">The full path to the folder where you want to save the file.
/// <param name="email">The email to send.</param>
/// For example, "c:\emails" </param>
void SaveToFile(Email email, string path);

/// <summary>
/// Creates a new <see cref="MailMessage"/> for the given email. You can
/// modify the message, for example adding attachments, and then send this yourself.
Expand Down