FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

MessageId improvements for MailKitSender

Open belidzs opened this issue 3 years ago • 0 comments

This PR contains two changes regarding MailKitSender:

  • Use local MessageId as SendResponse.MessageId
  • Use remotely generated MessageId when using Amazon SES as SendResponse.MessageId

Explanation

Returning local MessageId as part of SendResponse

MailKit generates a local Message-Id for each message which is then passed to the remote SMTP service. Currently this field is not returned as part of SendResponse. This PR changes that.

Amazon SES

While the header Message-Id should remain unchanged after its value is first set, Amazon's SES service overwrites it with its own value.

Knowing the value of this field is useful if someone wants to track whether a particular message has been accepted, bounced, complained about, etc.

This PR makes use of the SmtpClient.MessageSent event which fires after the message has been accepted. By using the hostname of the SMTP service and leveraging the Response property from the MessageSentEventArgs class we can determine the final value of Message-Id.

Since it cannot be guaranteed that the MessageSent event will fire before the ISender.Send or ISender.SendAsync method is returned, I've used an instance of ManualResetEventSlim to block the thread on Wait() until it is signaled. This signal is sent at the end of the MessageSent event (if the client is configured to use SES) or right away if SES is not used.

References

If you provide a Message-ID header, Amazon SES overrides the header with its own value.

Source: https://docs.aws.amazon.com/ses/latest/dg/header-fields.html

If the request to Amazon SES succeeds, Amazon SES returns a success response to the sender. This message includes the message ID, a string of characters that uniquely identifies the request. You can use the message ID to identify the sent email or to track problems encountered during sending.

Source: https://docs.aws.amazon.com/ses/latest/dg/send-email-concepts-process.html

belidzs avatar Apr 24 '22 17:04 belidzs