简体   繁体   English

使用mimekit / mailkit库获取电子邮件的传递状态

[英]get the delivery status of email with mimekit/mailkit library

I am using @jstedfast Mimekit/Mailkit library, For sending mass email from my app. 我正在使用@jstedfast Mimekit / Mailkit库,用于从我的应用程序发送大量电子邮件。 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. 这是我的第一次尝试,经过一定的RnD后,我必须在某些地方设置或传递report-type = delivery-status,但我不知道在哪里阅读该文件,该怎么做。 。 i also try by overriding DeliveryStatusNotification,but got nothing.May be i am going in wrong direction to get the notification/status. 我也尝试通过覆盖DeliveryStatusNotification,但一无所获。可能是我朝错误的方向获取通知/状态。

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

I came to know that @jstedfast is active here. 我知道@jstedfast在这里活跃。 I need your help for this. 为此,我需要您的帮助。 I didn't get any directions to do this. 我没有任何指示来执行此操作。 Thanks in advance. 提前致谢。

The first thing you need to do is subclass SmtpClient like the example in the docs: 您需要做的第一件事是子类SmtpClient,如文档中的示例所示:

http://www.mimekit.net/docs/html/M_MailKit_Net_Smtp_SmtpClient_GetDeliveryStatusNotifications.htm 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. 这将告诉SMTP服务器向您发送有关您发送的每封邮件的传递状态的电子邮件。

These messages will have a top-level MIME-type of multipart/report with a report-type value of delivery-status . 这些消息将具有multipart/report的顶级MIME类型,其report-type值为delivery-status

In other words, the Content-Type header will look like this: 换句话说, Content-Type标头将如下所示:

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. 使用MimeMessage.Load()解析消息后,就可以检查Body是否为具有预期ReportType属性值的MultipartReport

From there, you can locate the child part that is of type MessageDeliveryStatus (typically the second part I think). 从那里,您可以找到类型为MessageDeliveryStatus的子部分(通常是我认为的第二部分)。

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. 从那里,您将要检查StatusGroups属性(请参阅http://www.mimekit.net/docs/html/P_Mimekit_MessageDeliveryStatus_StatusGroups.htm)-集合中的每个HeaderList都将具有不同收件人的信息。

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. 您需要阅读StatusGroups文档中列出的RFC,以找出需要查找的可能的标头和值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM