3

I am using @jstedfast Mimekit/Mailkit library, For sending mass email from my app. I want to know how to get the delivery status of each email. This is my first try to get this and after some RnD i got that we have to set or pass report-type=delivery-status some where, but i didn't get any idea where to do that form the doc where i read this. i also try by overriding DeliveryStatusNotification,but got nothing.May be i am going in wrong direction to get the notification/status.

 protected override DeliveryStatusNotification? GetDeliveryStatusNotifications(MimeMessage message, MailboxAddress mailbox)
    {}

I came to know that @jstedfast is active here. I need your help for this. I didn't get any directions to do this. Thanks in advance.

Pooja K.
  • 97
  • 1
  • 10

1 Answers1

6

The first thing you need to do is subclass SmtpClient like the example in the docs:

http://www.mimekit.net/docs/html/M_MailKit_Net_Smtp_SmtpClient_GetDeliveryStatusNotifications.htm

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 will tell the SMTP server to send you emails about the delivery status of each message that you send.

These messages will have a top-level MIME-type of multipart/report with a report-type value of delivery-status.

In other words, the Content-Type header will look like this:

Content-Type: multipart/report; report-type=delivery-status; boundary=ajkfhkzfhkjhkjadskhz

Once you parse the message with MimeMessage.Load(), you can check if the Body is a MultipartReport with the expected ReportType property value.

From there, you can locate the child part that is of type MessageDeliveryStatus (typically the second part I think).

From there, you will want to check the StatusGroups property (see http://www.mimekit.net/docs/html/P_MimeKit_MessageDeliveryStatus_StatusGroups.htm) - each HeaderList in the collection will have information for a different recipient.

You'll need to read the RFC's listed in the StatusGroups docs to figure out what possible headers and values you will need to look for.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Does this API has any way to deal with bounce mail like mail id not exists? I want to check the bounce mail. – Pooja K. Jul 12 '17 at 06:13
  • Yes. That info will be in the headers. Read the etc for details. – jstedfast Jul 12 '17 at 08:34
  • So you just have to override that method? You don't have to use it anywhere? Is it used indirectly by the Send Method? – Bedir Dec 01 '20 at 15:42
  • Correct. MailKit already calls the virtual method and makes use of the value. All you need to do is override it to return a value that isn’t null. – jstedfast Dec 01 '20 at 19:22