diff --git a/src/Facteur.MsGraph/GraphMailer.cs b/src/Facteur.MsGraph/GraphMailer.cs index 3837232..384a7c0 100644 --- a/src/Facteur.MsGraph/GraphMailer.cs +++ b/src/Facteur.MsGraph/GraphMailer.cs @@ -56,7 +56,6 @@ await graphClient.Users[Credentials.From] public async t.Task SendMailAsync(EmailRequest request) where T : class { GraphServiceClient graphClient = await ConnectClient().ConfigureAwait(false); - Message message = new() { Subject = request.Subject, diff --git a/src/Facteur.SendGrid/EmailAddressConverter.cs b/src/Facteur.SendGrid/EmailAddressConverter.cs new file mode 100644 index 0000000..29f3244 --- /dev/null +++ b/src/Facteur.SendGrid/EmailAddressConverter.cs @@ -0,0 +1,10 @@ +using SendGrid.Helpers.Mail; + +namespace Facteur.SendGrid +{ + internal static class EmailAddressConverter + { + internal static EmailAddress ToEmailAddress(this Sender sender) + => new(sender.Email, sender.Name); + } +} \ No newline at end of file diff --git a/src/Facteur.SendGrid/SendGridBaseMailer.cs b/src/Facteur.SendGrid/SendGridBaseMailer.cs index 7e9d35c..e2aeed0 100644 --- a/src/Facteur.SendGrid/SendGridBaseMailer.cs +++ b/src/Facteur.SendGrid/SendGridBaseMailer.cs @@ -36,7 +36,7 @@ protected SendGridBaseMailer(string apiKey) public virtual Task SendMailAsync(EmailRequest request) { SendGridClient client = new(ApiKey); - EmailAddress sendFrom = new(request.From); + EmailAddress sendFrom = request.From.ToEmailAddress(); List sendTo = request.To.Select(x => new EmailAddress(x)).ToList(); SendGridMessage message = MailHelper .CreateSingleEmailToMultipleRecipients(sendFrom, sendTo, request.Subject, null, request.Body) diff --git a/src/Facteur.SendGrid/SendGridPlainTextMailer.cs b/src/Facteur.SendGrid/SendGridPlainTextMailer.cs index 8e3c7b9..9271b8e 100644 --- a/src/Facteur.SendGrid/SendGridPlainTextMailer.cs +++ b/src/Facteur.SendGrid/SendGridPlainTextMailer.cs @@ -29,7 +29,7 @@ public SendGridPlainTextMailer(string key) : base(key) public override Task SendMailAsync(EmailRequest request) { SendGridClient client = new(ApiKey); - EmailAddress sendFrom = new(request.From); + EmailAddress sendFrom = request.From.ToEmailAddress(); List sendTo = request.To.Select(x => new EmailAddress(x)).ToList(); SendGridMessage message = MailHelper.CreateSingleEmailToMultipleRecipients(sendFrom, sendTo, request.Subject, request.Body, null); @@ -39,7 +39,7 @@ public override Task SendMailAsync(EmailRequest request) public Task SendMailAsync(EmailRequest request) where T : class { SendGridClient client = new(ApiKey); - EmailAddress sendFrom = new(request.From); + EmailAddress sendFrom = request.From.ToEmailAddress(); List sendTo = request.To.Select(x => new EmailAddress(x)).ToList(); SendGridMessage message = MailHelper.CreateSingleEmailToMultipleRecipients(sendFrom, sendTo, request.Subject, request.Body, null); diff --git a/src/Facteur.Smtp.Tests/SmtpTests.cs b/src/Facteur.Smtp.Tests/SmtpTests.cs index 4c2cbb2..f0b83c6 100644 --- a/src/Facteur.Smtp.Tests/SmtpTests.cs +++ b/src/Facteur.Smtp.Tests/SmtpTests.cs @@ -19,7 +19,7 @@ public async Task Smtp_SendTemplateMail_ShouldSend() EmailRequest request = composer .SetModel(new TestMailModel { Email = "guy.gadbois@facteur.com", Name = "Guy Gadbois" }) .SetSubject("Hello world") - .SetFrom("info@facteur.com") + .SetFrom("info@facteur.com", "Facteur") .SetTo("tibipi@getnada.com") .SetCc("tibipi@getnada.com") .SetBcc("tibipi@getnada.com") diff --git a/src/Facteur.Smtp/SmtpMailer.cs b/src/Facteur.Smtp/SmtpMailer.cs index fe9173d..ae16259 100644 --- a/src/Facteur.Smtp/SmtpMailer.cs +++ b/src/Facteur.Smtp/SmtpMailer.cs @@ -43,7 +43,7 @@ public async Task SendMailAsync(EmailRequest request) msg.Subject = request.Subject; msg.Body = request.Body; msg.IsBodyHtml = true; - msg.From = new MailAddress(request.From); + msg.From = request.From.ToMailAddress(); if (request.To != null && request.To.Any()) msg.To.Add(string.Join(",", request.To)); diff --git a/src/Facteur.Smtp/ToMailAddressConverter.cs b/src/Facteur.Smtp/ToMailAddressConverter.cs new file mode 100644 index 0000000..3fab345 --- /dev/null +++ b/src/Facteur.Smtp/ToMailAddressConverter.cs @@ -0,0 +1,10 @@ +using System.Net.Mail; + +namespace Facteur.Smtp +{ + internal static class ToMailAddressConverter + { + internal static MailAddress ToMailAddress(this Sender sender) + => new(sender.Email, sender.Name); + } +} \ No newline at end of file diff --git a/src/Facteur/Compose/BaseEmailComposer.cs b/src/Facteur/Compose/BaseEmailComposer.cs index c422047..132c9b2 100644 --- a/src/Facteur/Compose/BaseEmailComposer.cs +++ b/src/Facteur/Compose/BaseEmailComposer.cs @@ -23,9 +23,15 @@ public BaseEmailComposer SetBody(string body) return this; } - public BaseEmailComposer SetFrom(string from) + public BaseEmailComposer SetFrom(Sender sender) { - Request.From = from; + Request.From = sender; + return this; + } + + public BaseEmailComposer SetFrom(string email, string name = null) + { + Request.From = new Sender(email, name); return this; } @@ -61,7 +67,8 @@ public BaseEmailComposer Attach(IEnumerable attachments) public T Build() { - Guard.ThrowIfNullOrEmpty(Request.From, nameof(Request.From)); + Guard.ThrowIfNull(Request.From, nameof(Request.From)); + Guard.ThrowIfNullOrEmpty(Request.From.Email, nameof(Request.From.Email)); Guard.ThrowIfNullOrEmpty(Request.Subject, nameof(Request.Subject)); Guard.ThrowIfNullOrEmpty(Request.To, nameof(Request.To)); diff --git a/src/Facteur/Compose/EmailRequest.cs b/src/Facteur/Compose/EmailRequest.cs index 4bae4f2..4cd4173 100644 --- a/src/Facteur/Compose/EmailRequest.cs +++ b/src/Facteur/Compose/EmailRequest.cs @@ -2,13 +2,14 @@ namespace Facteur { + public class EmailRequest { public string Subject { get; set; } public string Body { get; set; } - public string From { get; set; } + public Sender From { get; set; } public IEnumerable To { get; set; } diff --git a/src/Facteur/Compose/Sender.cs b/src/Facteur/Compose/Sender.cs new file mode 100644 index 0000000..fbfc94e --- /dev/null +++ b/src/Facteur/Compose/Sender.cs @@ -0,0 +1,15 @@ +namespace Facteur +{ + public struct Sender + { + public Sender(string email, string name) + { + Name = name; + Email = email; + } + + public string Name { get; set; } + + public string Email { get; set; } + } +} \ No newline at end of file diff --git a/src/Facteur/Guard.cs b/src/Facteur/Guard.cs index f65677e..62bffa4 100644 --- a/src/Facteur/Guard.cs +++ b/src/Facteur/Guard.cs @@ -6,6 +6,12 @@ namespace Facteur { internal static class Guard { + internal static void ThrowIfNull(object argumentValue, string argumentName) + { + if (argumentValue == null) + throw new ArgumentNullException(argumentName); + } + internal static void ThrowIfNullOrEmpty(string argumentValue, string argumentName) { if (string.IsNullOrEmpty(argumentValue))