skip to Main Content

I am putting this question out there because it took me the better part of two days figuring this out by putting answers together.

I was attempting to receive AWS’s MessageID that it assigns to an email so that I could later use SNS/SQS to receive email feedback. And using System.Net.SmtpClient responded with a void for successful emails. You only received exception codes on a failure.

AWS will always send a response, but to get the message on success I needed to incorporate MailKit nuget and use the MessageSent event.

The resposne looks like this…. "OK xxxxx-xxxxxx-xxxxxx-xxxxx" where x’s are the newly assigned messageID.

2

Answers


  1. Chosen as BEST ANSWER

    Below is the simplest code to send an email with the method I used to get the messageID from the successful response. I hope this saves people a lot of time!

    Simply use MailKit nuget and the following code:

    using MailKit.Net.Smtp;
    using MailKit.Security;
    using MimeKit;
    
    namespace TestMail
    {
        internal class Program
        {
            static async Task Main(string[] args)
            {
                await SendMail();
            }
    
            internal static async Task SendMail()
            {
                using (var mail = new MimeMessage())
                {
                    mail.From.Add(MailboxAddress.Parse("[email protected]"));
    
                    mail.To.Add(MailboxAddress.Parse("[email protected]"));
    
                    mail.Subject = "TEST";
    
                    mail.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = "<P>Hello!</P>" };
    
                    string goodMessage = "";
                    using (var smtp = new SmtpClient())
                    {
                        smtp.MessageSent += async (sender, args) =>
                        {
                            goodMessage = args.Response;
                        };
                        
                        await smtp.ConnectAsync("email-smtp.us-east-1.amazonaws.com", 587, SecureSocketOptions.StartTls);
    
                        await smtp.AuthenticateAsync("user", "password");
                        await smtp.SendAsync(mail);                                    
                    }
    
                    Console.WriteLine("This is the messageID: " + goodMessage.Split(' ')[1]);
    
                }            
            }
        }
    }
    

  2. I believe that starting with MailKit 3.0, the Send() and SendAsync() methods now return a string value so that you don’t actually need to listen to the MessageSent event:

    using (var smtp = new SmtpClient())
    {
        await smtp.ConnectAsync("email-smtp.us-east-1.amazonaws.com", 587, SecureSocketOptions.StartTls);
        await smtp.AuthenticateAsync("user", "password");
    
        string response = await smtp.SendAsync(mail);
        string[] tokens = response.Split(' ');
        string id = tokens[1];
    
        await smtp.DisconnectAsync(true);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search