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

Email.Azure: Deconstructing email addresses that include a display name (Lombiq Technologies: OCORE-204) #16889

Merged
merged 8 commits into from
Oct 28, 2024
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net.Mail;
using Azure;
using Azure.Communication.Email;
using Microsoft.Extensions.Localization;
Expand Down Expand Up @@ -161,19 +162,19 @@ private EmailMessage FromMailMessage(MailMessage message, Dictionary<string, ILi
List<EmailAddress> toRecipients = null;
if (recipients.To.Count > 0)
{
toRecipients = [.. recipients.To.Select(r => new EmailAddress(r))];
toRecipients = [.. recipients.To.Select(ConvertEmailAddressToAzureEmailAddress)];
}

List<EmailAddress> ccRecipients = null;
if (recipients.Cc.Count > 0)
{
ccRecipients = [.. recipients.Cc.Select(r => new EmailAddress(r))];
ccRecipients = [.. recipients.Cc.Select(ConvertEmailAddressToAzureEmailAddress)];
}

List<EmailAddress> bccRecipients = null;
if (recipients.Bcc.Count > 0)
{
bccRecipients = [.. recipients.Bcc.Select(r => new EmailAddress(r))];
bccRecipients = [.. recipients.Bcc.Select(ConvertEmailAddressToAzureEmailAddress)];
}

var content = new EmailContent(message.Subject);
Expand All @@ -187,13 +188,14 @@ private EmailMessage FromMailMessage(MailMessage message, Dictionary<string, ILi
}

var emailMessage = new EmailMessage(
message.From,
// For compatibility with configuration for other providers that allow a sender with display name.
TryCreateMailAddressOrFail(message.From).EmailAddress,
new EmailRecipients(toRecipients, ccRecipients, bccRecipients),
content);

foreach (var address in message.GetReplyTo())
{
emailMessage.ReplyTo.Add(new EmailAddress(address));
emailMessage.ReplyTo.Add(ConvertEmailAddressToAzureEmailAddress(address));
}

foreach (var attachment in message.Attachments)
Expand Down Expand Up @@ -224,4 +226,21 @@ private EmailMessage FromMailMessage(MailMessage message, Dictionary<string, ILi

return emailMessage;
}

private static EmailAddress ConvertEmailAddressToAzureEmailAddress(string emailWithDisplayName)
{
var (displayName, emailAddress) = TryCreateMailAddressOrFail(emailWithDisplayName);

return new EmailAddress(emailAddress, displayName);
}

private static (string DisplayName, string EmailAddress) TryCreateMailAddressOrFail(string email)
{
if (MailAddress.TryCreate(email, out var mailAddress))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use IEmailAddressValidator instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need the deconstructed email address (i.e. the display name and the address itself separately), not just validation.

{
return (mailAddress.DisplayName, mailAddress.Address);
}

throw new ArgumentException($"The email address '{email}' is invalid.");
}
}