13

The .Net SmtpClient's Send method returns void. It only throws two exceptions, SmtpException and a FailureToSendToRecipientsException (or something like that).

When using SES, for successful email delivery SES sends back a 200 OK message with the message id. This message id needs to be tracked.

How do I do this using the C# SMTP api?

Edit: The SMTP protocol mentions various response codes sent by the SMTP server. I am looking for a SMTP library that exposes the "final" response code to the caller. I am already aware of the SES HTTP API. I am not looking to use that for the moment.

Arafat Nalkhande
  • 8,663
  • 7
  • 35
  • 57
Amith George
  • 5,702
  • 2
  • 32
  • 50
  • +1 good question, someone has asked something similar but not duplicate: http://stackoverflow.com/questions/7273949/smtpclient-get-result-from-server-on-send – Jeremy Thompson May 24 '13 at 06:41
  • @JeremyThompson, thanks. I find it funny that of the three different SMTP libs I found, none return the response code. Anyway, I did see that question. The accepted answer mentions `From your code, you can only confirm that it got to the SMTP server you're using to send`. And currently the only way to do that is the absence of an Exception. SES could very well return a throttled or max emails sent response code. This might not create an Exception (atleast I couldn't find documentation saying that it would). – Amith George May 24 '13 at 07:02
  • @AmithGeorge were you able to get this done through SMTP ? I am facing the exact same issue after having written all my code using SMTP :( If it doesn't work, will need to use the SES api. Thanks. – HopeKing Jan 11 '18 at 15:01

1 Answers1

3

Have you tried the Amazon SES (Simple Email Service) C# Wrapper?

It has an SendEmail method that returns a Class with the MessageId:

public AmazonSentEmailResult SendEmail(string toEmail, string senderEmailAddress, string replyToEmailAddress, string subject, string body)
{
       List<string> toAddressList = new List<string>();
       toAddressList.Add(toEmail);
       return SendEmail(this.AWSAccessKey, this.AWSSecretKey, toAddressList, new List<string>(), new List<string>(), senderEmailAddress, replyToEmailAddress, subject, body);
}

public class AmazonSentEmailResult
{
    public Exception ErrorException { get; set; }
    public string MessageId { get; set; }
    public bool HasError { get; set; }

    public AmazonSentEmailResult()
    {
        this.HasError = false;
        this.ErrorException = null;
        this.MessageId = string.Empty;
    }
}

I dont think you can get the MessageId with System.Net.Mail.SmtpClient you will need to use Amazon.SimpleEmail.AmazonSimpleEmailServiceClient as per the Amazon SES sample: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-net.html

Jeremy Thompson
  • 52,213
  • 20
  • 153
  • 256
  • I was aware of the HTTP API offered by SES and the SDK available for C#. I am interested in using SMTP for SES. That way I can dynamically switch between using my own SMTP server, the client's SMTP server or SES SMTP server. – Amith George May 24 '13 at 06:24
  • I understand, the thing is only one of those SMTP servers will return the MessageId. I'd be leaning towards making an interface and using the SES implementation for the SES Server and System.Net.Mail.SmtpClient for the others. – Jeremy Thompson May 24 '13 at 06:27
  • `Only one of those will the MessageId` - True. But all of the SMTP servers will return a response code. The response code from SES will contain the MessageId. That is the only difference. There must be an SMTP client that exposes the last response code returned by the server. – Amith George May 24 '13 at 06:30
  • 1
    Does it handle attachments presently? – sobelito Aug 28 '13 at 19:39