【发布时间】:2021-05-09 17:10:00
【问题描述】:
我们有一个 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 消息。
【问题讨论】: