Skip to content
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
59 changes: 11 additions & 48 deletions src/Proffer.Email.InMemory/InMemoryEmailProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,23 @@ public InMemoryEmailProvider(IInMemoryEmailRepository inMemoryEmailRepository)
/// <summary>
/// Sends an email.
/// </summary>
/// <param name="from">The sender email address.</param>
/// <param name="recipients">The email recipients.</param>
/// <param name="subject">The subject.</param>
/// <param name="bodyText">The body as plain text.</param>
/// <param name="bodyHtml">The body as HTML.</param>
/// <param name="email">All informations about the email.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// </returns>
public Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipients, string subject, string bodyText, string bodyHtml)
=> this.SendEmailAsync(from, recipients, subject, bodyText, bodyHtml, Enumerable.Empty<IEmailAttachment>());

/// <summary>
/// Sends an email.
/// </summary>
/// <param name="from">The sender email address.</param>
/// <param name="recipients">The email recipients.</param>
/// <param name="subject">The subject.</param>
/// <param name="bodyText">The body as plain text.</param>
/// <param name="bodyHtml">The body as HTML.</param>
/// <param name="attachments">The file attachments.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// </returns>
public Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipients, string subject, string bodyText, string bodyHtml, IEnumerable<IEmailAttachment> attachments)
=> this.SendEmailAsync(from, recipients, Enumerable.Empty<IEmailAddress>(), Enumerable.Empty<IEmailAddress>(), subject, bodyText, bodyHtml, Enumerable.Empty<IEmailAttachment>());

/// <summary>
/// Sends an email.
/// </summary>
/// <param name="from">The sender email address.</param>
/// <param name="recipients">The email recipients.</param>
/// <param name="ccRecipients">The CC email recipients.</param>
/// <param name="bccRecipients">The BCC email recipients.</param>
/// <param name="subject">The subject.</param>
/// <param name="bodyText">The body as plain text.</param>
/// <param name="bodyHtml">The body as HTML.</param>
/// <param name="attachments">The file attachments.</param>
/// <param name="replyTo">The reply-to email address.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// </returns>
public Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipients, IEnumerable<IEmailAddress> ccRecipients, IEnumerable<IEmailAddress> bccRecipients, string subject, string bodyText, string bodyHtml, IEnumerable<IEmailAttachment> attachments, IEmailAddress replyTo = null)
public Task SendEmailAsync(IEmail email)
{
this.inMemoryEmailRepository.Save(new InMemoryEmail
{
Subject = subject,
MessageText = bodyText,
MessageHtml = bodyHtml,
To = recipients.ToArray(),
Cc = ccRecipients.ToArray(),
Bcc = bccRecipients.ToArray(),
From = from,
ReplyTo = replyTo,
Attachments = attachments
Subject = email.Subject,
MessageText = email.BodyText,
MessageHtml = email.BodyHtml,
To = email.Recipients.ToArray(),
Cc = email.CcRecipients.ToArray(),
Bcc = email.BccRecipients.ToArray(),
From = email.From,
ReplyTo = email.ReplyTo,
Attachments = email.Attachments
});

return Task.FromResult(0);
Expand Down
73 changes: 18 additions & 55 deletions src/Proffer.Email.SendGrid/SendGridEmailProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,14 @@ public SendGridEmailProvider(IEmailProviderOptions options)
/// <summary>
/// Sends an email.
/// </summary>
/// <param name="from">The sender email address.</param>
/// <param name="recipients">The email recipients.</param>
/// <param name="subject">The subject.</param>
/// <param name="bodyText">The body as plain text.</param>
/// <param name="bodyHtml">The body as HTML.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// </returns>
public Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipients, string subject, string bodyText, string bodyHtml)
=> this.SendEmailAsync(from, recipients, subject, bodyText, bodyHtml, Enumerable.Empty<IEmailAttachment>());

/// <summary>
/// Sends an email.
/// </summary>
/// <param name="from">The sender email address.</param>
/// <param name="recipients">The email recipients.</param>
/// <param name="subject">The subject.</param>
/// <param name="bodyText">The body as plain text.</param>
/// <param name="bodyHtml">The body as HTML.</param>
/// <param name="attachments">The file attachments.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// </returns>
public Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipients, string subject, string bodyText, string bodyHtml, IEnumerable<IEmailAttachment> attachments)
=> this.SendEmailAsync(from, recipients, Enumerable.Empty<IEmailAddress>(), Enumerable.Empty<IEmailAddress>(), subject, bodyText, bodyHtml, Enumerable.Empty<IEmailAttachment>());

/// <summary>
/// Sends an email.
/// </summary>
/// <param name="from">The sender email address.</param>
/// <param name="recipients">The email recipients.</param>
/// <param name="ccRecipients">The CC email recipients.</param>
/// <param name="bccRecipients">The BCC email recipients.</param>
/// <param name="subject">The subject.</param>
/// <param name="bodyText">The body as plain text.</param>
/// <param name="bodyHtml">The body as HTML.</param>
/// <param name="attachments">The file attachments.</param>
/// <param name="replyTo">The reply-to email address.</param>
/// <param name="email">All informations about the email.</param>
/// <exception cref="ArgumentException">Each email address should be unique between to, cc, and bcc recipients. We found duplicates.</exception>
/// <exception cref="Exception">Cannot Send Email: {response.StatusCode}</exception>
public async Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipients, IEnumerable<IEmailAddress> ccRecipients, IEnumerable<IEmailAddress> bccRecipients, string subject, string bodyText, string bodyHtml, IEnumerable<IEmailAttachment> attachments, IEmailAddress replyTo = null)
public async Task SendEmailAsync(IEmail email)
{
var allRecipients = new List<IEmailAddress>(recipients);
allRecipients.AddRange(ccRecipients);
allRecipients.AddRange(bccRecipients);
var allRecipients = new List<IEmailAddress>(email.Recipients);
allRecipients.AddRange(email.CcRecipients);
allRecipients.AddRange(email.BccRecipients);

if (allRecipients.GroupBy(r => r.Email).Count() < allRecipients.Count)
{
Expand All @@ -88,43 +51,43 @@ public async Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress>

SendGridMessage message;

if (recipients.Count() == 1)
if (email.Recipients.Count() == 1)
{
message = MailHelper.CreateSingleEmail(from.ToSendGridEmail(), recipients.First().ToSendGridEmail(), subject, bodyText, bodyHtml);
message = MailHelper.CreateSingleEmail(email.From.ToSendGridEmail(), email.Recipients.First().ToSendGridEmail(), email.Subject, email.BodyText, email.BodyHtml);
}
else
{
message = MailHelper.CreateSingleEmailToMultipleRecipients(
from.ToSendGridEmail(),
recipients.Select(email => email.ToSendGridEmail()).ToList(),
subject,
bodyText,
bodyHtml);
email.From.ToSendGridEmail(),
email.Recipients.Select(email => email.ToSendGridEmail()).ToList(),
email.Subject,
email.BodyText,
email.BodyHtml);
}

foreach (IEmailAddress ccRecipient in ccRecipients)
foreach (IEmailAddress ccRecipient in email.CcRecipients)
{
message.AddCc(ccRecipient.Email, ccRecipient.DisplayName);
}

foreach (IEmailAddress bccRecipient in bccRecipients)
foreach (IEmailAddress bccRecipient in email.BccRecipients)
{
message.AddBcc(bccRecipient.Email, bccRecipient.DisplayName);
}

if (attachments.Any())
if (email.Attachments.Any())
{
message.AddAttachments(attachments.Select(a => new Attachment
message.AddAttachments(email.Attachments.Select(a => new Attachment
{
Filename = a.FileName,
Type = a.ContentType,
Content = Convert.ToBase64String(a.Data)
}).ToList());
}

if (replyTo != null)
if (email.ReplyTo != null)
{
message.ReplyTo = replyTo.ToSendGridEmail();
message.ReplyTo = email.ReplyTo.ToSendGridEmail();
}

Response response = await client.SendEmailAsync(message);
Expand Down
51 changes: 11 additions & 40 deletions src/Proffer.Email.Smtp/SmtpEmailProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,6 @@ public SmtpEmailProvider(IServiceProvider serviceProvider, IEmailProviderOptions
options.Parameters.TryGetValue("Password", out this.password);
}

/// <summary>
/// Sends an email.
/// </summary>
/// <param name="from">The sender email address.</param>
/// <param name="recipients">The email recipients.</param>
/// <param name="subject">The subject.</param>
/// <param name="bodyText">The body as plain text.</param>
/// <param name="bodyHtml">The body as HTML.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// </returns>
public Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipients, string subject, string bodyText, string bodyHtml)
=> this.SendEmailAsync(from, recipients, subject, bodyText, bodyHtml, Enumerable.Empty<IEmailAttachment>());

/// <summary>
/// Sends an email.
/// </summary>
/// <param name="from">The sender email address.</param>
/// <param name="recipients">The email recipients.</param>
/// <param name="subject">The subject.</param>
/// <param name="bodyText">The body as plain text.</param>
/// <param name="bodyHtml">The body as HTML.</param>
/// <param name="attachments">The file attachments.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// </returns>
public Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipients, string subject, string bodyText, string bodyHtml, IEnumerable<IEmailAttachment> attachments)
=> this.SendEmailAsync(from, recipients, Enumerable.Empty<IEmailAddress>(), Enumerable.Empty<IEmailAddress>(), subject, bodyText, bodyHtml, Enumerable.Empty<IEmailAttachment>());

/// <summary>
/// Sends an email.
/// </summary>
Expand All @@ -92,40 +63,40 @@ public Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipi
/// <param name="bodyHtml">The body as HTML.</param>
/// <param name="attachments">The file attachments.</param>
/// <param name="replyTo">The reply-to email address.</param>
public async Task SendEmailAsync(IEmailAddress from, IEnumerable<IEmailAddress> recipients, IEnumerable<IEmailAddress> ccRecipients, IEnumerable<IEmailAddress> bccRecipients, string subject, string bodyText, string bodyHtml, IEnumerable<IEmailAttachment> attachments, IEmailAddress replyTo = null)
public async Task SendEmailAsync(IEmail email)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(from.DisplayName, from.Email));
message.From.Add(new MailboxAddress(email.From.DisplayName, email.From.Email));

if (replyTo != null)
if (email.ReplyTo != null)
{
message.ReplyTo.Add(new MailboxAddress(replyTo.DisplayName, replyTo.Email));
message.ReplyTo.Add(new MailboxAddress(email.ReplyTo.DisplayName, email.ReplyTo.Email));
}

foreach (IEmailAddress recipient in recipients)
foreach (IEmailAddress recipient in email.Recipients)
{
message.To.Add(new MailboxAddress(recipient.DisplayName, recipient.Email));
}

foreach (IEmailAddress recipient in ccRecipients)
foreach (IEmailAddress recipient in email.CcRecipients)
{
message.Cc.Add(new MailboxAddress(recipient.DisplayName, recipient.Email));
}

foreach (IEmailAddress recipient in bccRecipients)
foreach (IEmailAddress recipient in email.BccRecipients)
{
message.Bcc.Add(new MailboxAddress(recipient.DisplayName, recipient.Email));
}

message.Subject = subject;
message.Subject = email.Subject;

var builder = new BodyBuilder
{
TextBody = bodyText,
HtmlBody = bodyHtml
TextBody = email.BodyText,
HtmlBody = email.BodyHtml
};

foreach (IEmailAttachment attachment in attachments)
foreach (IEmailAttachment attachment in email.Attachments)
{
builder.Attachments.Add(attachment.FileName, attachment.Data, new ContentType(attachment.MediaType, attachment.MediaSubtype));
}
Expand Down
51 changes: 51 additions & 0 deletions src/Proffer.Email/Email.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Generic;

namespace Proffer.Email
{
/// <summary>
/// Contains the features that an email should have.
/// </summary>
public class Email : IEmail
{
/// <summary>
/// The sender email address.
/// </summary>
public IEmailAddress From { get; set; }
/// <summary>
/// >The email recipients.
/// </summary>
public IEnumerable<IEmailAddress> Recipients { get; set; }
/// <summary>
/// The CC email recipients.
/// </summary>
public IEnumerable<IEmailAddress> CcRecipients { get; set; }
/// <summary>
/// The BCC email recipients.
/// </summary>
public IEnumerable<IEmailAddress> BccRecipients { get; set; }
/// <summary>
/// The subject.
/// </summary>
public string Subject { get; set; }
/// <summary>
/// The body as plain text.
/// </summary>
public string BodyText { get; set; }
/// <summary>
/// The body as HTML.
/// </summary>
public string BodyHtml { get; set; }
/// <summary>
/// The file attachments.
/// </summary>
public IEnumerable<IEmailAttachment> Attachments { get; set; }
/// <summary>
/// The reply-to email address.
/// </summary>
public IEmailAddress ReplyTo { get; set; }
/// <summary>
/// For template emails.
/// </summary>
public IDictionary<EmailTemplateType, string> TemplateDictionary { get; set; }
}
}
51 changes: 51 additions & 0 deletions src/Proffer.Email/IEmail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Generic;

namespace Proffer.Email
{
/// <summary>
/// Contains the features that an email should have.
/// </summary>
public interface IEmail
{
/// <summary>
/// The sender email address.
/// </summary>
IEmailAddress From { get; set; }
/// <summary>
/// >The email recipients.
/// </summary>
IEnumerable<IEmailAddress> Recipients { get; set; }
/// <summary>
/// The CC email recipients.
/// </summary>
IEnumerable<IEmailAddress> CcRecipients { get; set; }
/// <summary>
/// The BCC email recipients.
/// </summary>
IEnumerable<IEmailAddress> BccRecipients { get; set; }
/// <summary>
/// The subject.
/// </summary>
string Subject { get; set; }
/// <summary>
/// The body as plain text.
/// </summary>
string BodyText { get; set; }
/// <summary>
/// The body as HTML.
/// </summary>
string BodyHtml { get; set; }
/// <summary>
/// The file attachments.
/// </summary>
IEnumerable<IEmailAttachment> Attachments { get; set; }
/// <summary>
/// The reply-to email address.
/// </summary>
IEmailAddress ReplyTo { get; set; }
/// <summary>
/// For template emails.
/// </summary>
IDictionary<EmailTemplateType, string> TemplateDictionary { get; set; }
}
}
Loading