We have a dovecot/postfix mail server which we use for automation purposes through IMAP and SMTP protocols.
There are situations that the system processes the email message but there is something wrong with the message and the system will exclude the message from the pipeline.
As the email messages are coming in from different sources, the system needs to inform the sender about the failure, and again for automation purposes, the other party expects a Message Delivery Status message with status fail.
We use MailKit for handling the messages and I had been trying to figure out how I can send such a message using MailKit in C#, but I have not been successful.
A simplified version of the code I have is as following:
// 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", "[email protected]")},
To = { new MailboxAddress("User", "[email protected]")},
Subject = "Delivery Status Notification (Failure)",
InReplyTo = "message id of the received message"
};
smtpClient.Send(mimeMessage);
Update:
With the suggested modification by @jstedfast now the code generates the following message:
From: Webmaster <[email protected]>
Date: Sat, 06 Feb 2021 23:50:56 +0100
Subject: Delivery Status Notification (Failure)
Message-Id: <[email protected]>
To: First Last <[email protected]>
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;[email protected]
Action: failed
Status: 550 5.7.1
--=-HnKieASbitf0LQ3XjF16Mw==--
What happens after sending the message is that, it is received and displayed as a regular email message in the original sender's mailbox.
Update 2:
If it helps, I use Postfix
as the SMTP server and it seems to me that, it might be Postfix
that modifies the mime message and converts it to a regular text mime message.
question from:
https://stackoverflow.com/questions/66061920/how-to-send-a-message-delivery-status-with-fail-status-using-mailkit 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…