Search code examples
c#asp.netamazon-ses

Set 'Sender Name' in Amazon SES in C#


I'm using Amazon SES in an ASP.net (Core) 6 App to send emails from my own domain - works well.

I want to change the 'From Name' so rather than seeing the email address users see 'Name' in their email client (eg 'John Smith' or 'Company Name').

This is the code that Amazon provide that sends the email:

var sender = "Name [email protected]";
var emailMessage = BuildEmailHeaders(sender, to, cc, bcc, subject);

var emailBody = BuildEmailBody(body, isHtmlBody);
emailMessage.Body = emailBody.ToMessageBody();
return SendEmailAsync(emailMessage);

With just email in the sender variable, it works fine.

Above I've tried adding a name first with a space as recommended by this article: https://kitefaster.com/2017/04/19/set-name-senderfromsource-amazon-ses/

but it returns a 500:

I've also tried this:

var sender = "Name<[email protected]>";

But it also returns 500

How can I change the sender name?

Thanks


Solution

  • The answer was in another part of the example code provided by Amazon.

    See "John Smith" below - it was just blank in the original example files.

        private static MimeMessage BuildEmailHeaders(string from, IEnumerable<string> to, IReadOnlyCollection<string> cc, IReadOnlyCollection<string> bcc, string subject)
        {
            var message = new MimeMessage();
            //message.From.Add(new MailboxAddress(string.Empty, from));
            message.From.Add(new MailboxAddress("John Smith", from));
    
            foreach (var recipient in to)
            {
                message.To.Add(new MailboxAddress(string.Empty, recipient));
            }
            if (cc != null && cc.Any())
            {
                foreach (var recipient in cc)
                {
                    message.Cc.Add(new MailboxAddress(string.Empty, recipient));
                }
            }
            if (bcc != null && bcc.Any())
            {
                foreach (var recipient in bcc)
                {
                    message.Bcc.Add(new MailboxAddress(string.Empty, recipient));
                }
            }
            message.Subject = subject;
            return message;
        }