如何解决如何使用 MailKit 发送带有失败状态的消息传递状态
我们有一个 dovecot/postfix 邮件服务器,我们通过 IMAP 和 SMTP 协议将其用于自动化目的。
在某些情况下,系统会处理电子邮件,但邮件有问题,系统会将邮件从管道中排除。
由于电子邮件来自不同的来源,系统需要将失败通知发件人,同样出于自动化目的,另一方希望收到状态为失败的消息传递状态消息。
我们使用 MailKit 来处理消息,我一直试图弄清楚如何在 C# 中使用 MailKit 发送这样的消息,但我没有成功。
我拥有的代码的简化版本如下:
// Prepares smtpClient
MultipartReport multipartReport = new MultipartReport("delivery-status")
{
new TextPart {Content = new MimeContent(new MemoryStream(Encoding.ASCII.GetBytes("The message contains malformed data")))},new MessageDeliveryStatus {StatusGroups = {new HeaderList {new Header("Action","Failed")}}}
};
MimeMessage message = new MimeMessage
{
Body = multipartReport,From = { new MailBoxAddress("Webmaster","webmaster@our.domain")},To = { new MailBoxAddress("User","user@origin.domain")},Subject = "Delivery Status Notification (Failure)",InReplyTo = "message id of the received message"
};
smtpClient.Send(mimeMessage);
更新: 根据@jstedfast 的建议修改,代码现在生成以下消息:
From: Webmaster <webmaster@ourdomain.com>
Date: Sat,06 Feb 2021 23:50:56 +0100
Subject: Delivery Status Notification (Failure)
Message-Id: <GII2AYZOWCU4.AXDWNKVKJ4XD3@ourdomain.com>
To: First Last <someone@gmail.com>
Return-Path: <>
MIME-Version: 1.0
Content-Type: multipart/report; boundary="=-HnKieASbitf0LQ3XjF16Mw==";
report-type=delivery-status
--=-HnKieASbitf0LQ3XjF16Mw==
Content-Type: text/plain
Attachment not allowed
--=-HnKieASbitf0LQ3XjF16Mw==
Content-Type: message/delivery-status
Reporting-MTA: dns;mail.ourdomain.com
Final-Recipient: rfc822;user@ourdomain.com
Action: Failed
Status: 550 5.7.1
--=-HnKieASbitf0LQ3XjF16Mw==--
发送邮件后发生的事情是,它被接收并显示为原始发件人邮箱中的常规电子邮件。
更新 2:
如果有帮助,我使用 Postfix
作为 SMTP 服务器,在我看来,它可能是 Postfix
修改 mime 消息并将其转换为常规文本 mime 消息。>
解决方法
不知道什么不起作用,就很难知道该建议什么。
也就是说,快速浏览 spec 会发现“Action”标头是 per-recipient-fields
的一部分,per-message-fields
是可选的第二组状态标头。
第一组标头字段是 per-message-fields =
[ original-envelope-id-field CRLF ]
reporting-mta-field CRLF
[ dsn-gateway-field CRLF ]
[ received-from-mta-field CRLF ]
[ arrival-date-field CRLF ]
*( extension-field CRLF )
,它应该包括以下字段:
per-recipient-fields
per-recipient-fields =
[ original-recipient-field CRLF ]
final-recipient-field CRLF
action-field CRLF
status-field CRLF
[ remote-mta-field CRLF ]
[ diagnostic-code-field CRLF ]
[ last-attempt-date-field CRLF ]
[ final-log-id-field CRLF ]
[ will-retry-until-field CRLF ]
*( extension-field CRLF )
定义为:
MessageDeliveryStatus
这意味着,您至少需要将构建 new MessageDeliveryStatus {
StatusGroups = {
new HeaderList {
new Header("Reporting-MTA","dns;<dns name of the MTA>")
},new HeaderList {
new Header("Final-Recipient","rfc822;<address of recipient>",new Header("Action","failed"),new Header("Status","5.something")
}
}
}
部分的方式更改为类似于以下内容:
{{1}}中选择一个状态代码
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。