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

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

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

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

我了解到 @jstedfast 在这里很活跃。为此我需要你的帮助。我没有得到任何指示来做到这一点。 提前致谢。

最佳答案

您需要做的第一件事是像文档中的示例那样子类化 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 header 将如下所示:

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,以确定您需要查找哪些可能的 header 和值。

关于c# - 使用 mimekit/mailkit 库获取电子邮件的传递状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45027910/

相关文章:

c# - 使用 XmlSerializer 时如何添加换行符

c# - 选择静态选择列表的值

c# - 为什么依赖注入(inject)注入(inject)最后注册的对象?

c# - Asp.NET MVC 模型绑定(bind) - 使用绑定(bind)参数属性为简单类型分配前缀

C# 单元测试模拟方法不起作用(为什么?)

node.js - 从 Gmail API 获取电子邮件的直接 URL(列出消息)

c# - 单个 ASPX 页面 - 无代码隐藏 - 页面指令

c# - 是否可以从 Controller 方法以编程方式调用 Razor 编译器?

node.js - 使用 Nodejs 的 Gmail API 推送通知

javascript - 邮件附件媒体类型错误 Gmail API