12

I've got some .Net code I'm switching from the System.Net.MailMessage to Amazon SES and their .Net SDK v2. Is it possible to include a display name with SES using the SDK similar to the MailMessage object?

The relevant part of the old code looks something like this:

    MailMessage message = new MailMessage();
    MailAddress toAddress = new MailAddress(_user.Email, _user.DisplayName);
    message.To.Add(toAddress);

The relevant part of the new code (so far):

        SendEmailRequest request = new SendEmailRequest()
        {
            Source = _user.Email
        };
Jake Braun
  • 1,172
  • 1
  • 13
  • 34

3 Answers3

19

With the Java SDK you can include the display name in the sender field using the format:

John Doe <john.doe@example.com>

I assume it is the same with the .NET SDK.

David Levesque
  • 22,181
  • 8
  • 67
  • 82
3

Just use the .ToString() method from the MailAddress object, and you'll get the John Doe <john.doe@example.com> string. Send this string to AWS.

Radu Ungureanu
  • 131
  • 1
  • 9
  • This also works for non-ASCII characters. Single liner example: `static readonly string senderAddress = new MailAddress("no-reply@email.com", "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes("Моя компания")) + "?=").ToString();` – Niksr Dec 02 '21 at 18:02
2

You can set this in your App Settings or WebConfig and concatenate the name and email in your method like this:

var toAddress = $"{_configuration["AWS-SES:SenderName"]} <{_configuration["AWS-SES:SenderAddress"]}>";

its found for .NET