使用mimekit / mailkit库获取电子邮件的交付状态

时间:2017-07-11 07:20:37

标签: c# asp.net-mvc gmail-api mailkit mimekit

我正在使用@jstedfast Mimekit / Mailkit库,用于从我的应用程序发送大量电子邮件。我想知道如何获取每封电子邮件的递送状态。这是我第一次尝试得到这个,在一些RnD之后,我得到了我们必须设置或传递report-type = delivery-status一些地方,但我不知道在哪里做那个形式的doc我读到这个。 我也尝试通过覆盖DeliveryStatusNotification,但什么也没得到。可能我错误的方向来获取通知/状态。

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

我知道@jstedfast在这里很活跃。我需要你的帮助。我没有得到任何指示这样做。 提前谢谢。

1 个答案:

答案 0 :(得分:4)

您需要做的第一件事就是将SmtpClient子类化为文档中的示例:

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;
    }
}

这将告诉SMTP服务器向您发送有关您发送的每封邮件的传递状态的电子邮件。

这些邮件的顶级MIME类型为multipart/reportreport-type值为delivery-status

换句话说,Content-Type标题将如下所示:

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

使用MimeMessage.Load()解析邮件后,您可以检查Body是否为具有预期MultipartReport属性值的ReportType

从那里,您可以找到MessageDeliveryStatus类型的子部件(通常是我认为的第二部分)。

从那里,您需要检查StatusGroups属性(请参阅http://www.mimekit.net/docs/html/P_MimeKit_MessageDeliveryStatus_StatusGroups.htm) - 集合中的每个HeaderList都会包含其他收件人的信息。

您需要阅读StatusGroups文档中列出的RFC,以确定您需要查找哪些标头和值。