rgl / MailBounceDetector

Detects whether a MailKit email Message is a bounce message
MIT License
28 stars 12 forks source link

Support "Original-Envelope-Id" #15

Open rklec opened 1 month ago

rklec commented 1 month ago

The maintainer of MailKit actually suggested an approach here, where you'd overwrite the SmtpClient in a subclass:

public class DSNSmtpClient : SmtpClient
{
    public DSNSmtpClient ()
    {
    }

    /// <summary>
    /// Get the envelope identifier to be used with delivery status notifications.
    /// </summary>
    /// <remarks>
    /// <para>The envelope identifier, if non-empty, is useful in determining which message
    /// a delivery status notification was issued for.</para>
    /// <para>The envelope identifier should be unique and may be up to 100 characters in
    /// length, but must consist only of printable ASCII characters and no white space.</para>
    /// <para>For more information, see rfc3461, section 4.4.</para>
    /// </remarks>
    /// <returns>The envelope identifier.</returns>
    /// <param name="message">The message.</param>
    protected override string GetEnvelopeId (MimeMessage message)
    {
        // Since you will want to be able to map whatever identifier you return here to the
        // message, the obvious identifier to use is probably the Message-Id value.
        return message.MessageId;
    }

    /// <summary>
    /// Get the types of delivery status notification desired for the specified recipient mailbox.
    /// </summary>
    /// <remarks>
    /// Gets the types of delivery status notification desired for the specified recipient mailbox.
    /// </remarks>
    /// <returns>The desired delivery status notification type.</returns>
    /// <param name="message">The message being sent.</param>
    /// <param name="mailbox">The mailbox.</param>
    protected override DeliveryStatusNotification? GetDeliveryStatusNotifications (MimeMessage message, MailboxAddress mailbox)
    {
        // In this example, we only want to be notified of failures to deliver to a mailbox.
        // If you also want to be notified of delays or successful deliveries, simply bitwise-or
        // whatever combination of flags you want to be notified about.
        return DeliveryStatusNotification.Failure;
    }
}

This allows one to parse and retrieve the MimeMessage here then with a definitive message ID that you also can request / should get in the DSN? Maybe support for this could be added - so that you can also access that data?

(I know you can of course manually parse it again, but well... if you have such a great library as this one here, already, why not adding it? It can be null or so after all, if it is not set.)

Edit: Foudn teh RFC part, it's https://www.rfc-editor.org/rfc/rfc3464#section-2.2.1 apparently.