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

C# 使用mimekit/mailkit库获取电子邮件的传递状态,c#,asp.net-mvc,gmail-api,mailkit,mimekit,C#,Asp.net Mvc,Gmail Api,Mailkit,Mimekit,我正在使用@jstedfast Mimekit/Mailkit库从我的应用程序发送大量电子邮件。我想知道如何获取每封电子邮件的发送状态。这是我第一次尝试得到这个,在一些RnD之后,我得到了我们必须在某个地方设置或传递报告类型=交货状态,但我不知道从我读到这篇文章的文档中应该在哪里做。 我也尝试覆盖DeliveryStatusNotification,但没有得到任何结果。可能是我获取通知/状态的方向错误 protected override DeliveryStatusNotification?

我正在使用@jstedfast Mimekit/Mailkit库从我的应用程序发送大量电子邮件。我想知道如何获取每封电子邮件的发送状态。这是我第一次尝试得到这个,在一些RnD之后,我得到了我们必须在某个地方设置或传递报告类型=交货状态,但我不知道从我读到这篇文章的文档中应该在哪里做。 我也尝试覆盖DeliveryStatusNotification,但没有得到任何结果。可能是我获取通知/状态的方向错误

 protected override DeliveryStatusNotification? GetDeliveryStatusNotifications(MimeMessage message, MailboxAddress mailbox)
    {}
我知道@jstedfast在这里是活动的。我需要你的帮助。我不知道怎么做。
提前感谢。

您需要做的第一件事是子类SmtpClient,如文档中的示例:

使用
MimeMessage.Load()
解析消息后,您可以检查
正文
是否是一个
MultipartReport
,并具有预期的
ReportType
属性值

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

从那里,您将需要检查
StatusGroups
属性(请参阅)-集合中的每个
标题列表将包含不同收件人的信息


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

此API是否有任何方法来处理邮件id不存在这样的跳转邮件?我想查一下回信。是的。该信息将出现在标题中。阅读etc获取详细信息。那么您只需重写该方法?你不必在任何地方使用它?它是否由Send方法间接使用?正确。MailKit已经调用虚拟方法并使用该值。您所需要做的就是重写它以返回一个非null的值。
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;
    }
}
Content-Type: multipart/report; report-type=delivery-status; boundary=ajkfhkzfhkjhkjadskhz