From 94afdf860d330f6b659d6186c4f808eb2332381b Mon Sep 17 00:00:00 2001 From: Hendrik Bulens Date: Thu, 5 Sep 2024 12:37:49 +0200 Subject: [PATCH] Sendgrid: CC and BCC --- src/Facteur.SendGrid/Facteur.SendGrid.csproj | 2 +- src/Facteur.SendGrid/SendGridBaseMailer.cs | 12 ++++++++++-- src/Facteur.SendGrid/SendGridPlainTextMailer.cs | 12 ++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Facteur.SendGrid/Facteur.SendGrid.csproj b/src/Facteur.SendGrid/Facteur.SendGrid.csproj index 83e396b..971650a 100644 --- a/src/Facteur.SendGrid/Facteur.SendGrid.csproj +++ b/src/Facteur.SendGrid/Facteur.SendGrid.csproj @@ -14,7 +14,7 @@ MIT Facteur;Email;Mail;SendGrid Send emails using the SendGrid endpoint - 2.1.0 + 2.1.1 Dime Software Dime Software Facteur diff --git a/src/Facteur.SendGrid/SendGridBaseMailer.cs b/src/Facteur.SendGrid/SendGridBaseMailer.cs index e2aeed0..5d091e7 100644 --- a/src/Facteur.SendGrid/SendGridBaseMailer.cs +++ b/src/Facteur.SendGrid/SendGridBaseMailer.cs @@ -33,7 +33,7 @@ protected SendGridBaseMailer(string apiKey) /// /// The subject. /// - public virtual Task SendMailAsync(EmailRequest request) + public virtual async Task SendMailAsync(EmailRequest request) { SendGridClient client = new(ApiKey); EmailAddress sendFrom = request.From.ToEmailAddress(); @@ -42,7 +42,15 @@ public virtual Task SendMailAsync(EmailRequest request) .CreateSingleEmailToMultipleRecipients(sendFrom, sendTo, request.Subject, null, request.Body) .AddAttachments(request); - return client.SendEmailAsync(message); + IEnumerable sendCc = request.Cc.Where(x => !request.To.Contains(x)); + foreach (string cc in sendCc) + message.AddCc(cc); + + IEnumerable sendBcc = request.Bcc.Where(x => !request.To.Contains(x) && !request.Cc.Contains(x)); + foreach (string bcc in sendBcc) + message.AddBcc(bcc); + + Response res = await client.SendEmailAsync(message); } } } \ No newline at end of file diff --git a/src/Facteur.SendGrid/SendGridPlainTextMailer.cs b/src/Facteur.SendGrid/SendGridPlainTextMailer.cs index e6da475..88bf2d4 100644 --- a/src/Facteur.SendGrid/SendGridPlainTextMailer.cs +++ b/src/Facteur.SendGrid/SendGridPlainTextMailer.cs @@ -30,14 +30,22 @@ public SendGridPlainTextMailer(string key, IEmailComposer composer = null) : bas /// /// The subject /// An instance of - public override Task SendMailAsync(EmailRequest request) + public override async Task SendMailAsync(EmailRequest request) { SendGridClient client = new(ApiKey); 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); - return client.SendEmailAsync(message); + IEnumerable sendCc = request.Cc.Where(x => !request.To.Contains(x)); + foreach (string cc in sendCc) + message.AddCc(cc); + + IEnumerable sendBcc = request.Bcc.Where(x => !request.To.Contains(x) && !request.Cc.Contains(x)); + foreach (string bcc in sendBcc) + message.AddBcc(bcc); + + Response res = await client.SendEmailAsync(message); } public async Task SendMailAsync(Func> compose)