Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MessageId improvements for MailKitSender #324

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

belidzs
Copy link
Contributor

@belidzs belidzs commented Apr 24, 2022

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

Use local MessageId as SendResponse.MessageId
Use remotely generated MessageId when using Amazon SES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant