1
votes

I came across the following thread Sending Email with return-path not functioning on how to set the From and Return-Path. I am using Mailkit, and this is what I tried:

var mailMessage = new MimeMessage();
mailMessage.From.Add(new MailboxAddress("firstname lastname", "[email protected]"));
mailMessage.Sender = new MailboxAddress("firstname lastname", "[email protected]");
mailMessage.To.Add(new MailboxAddress("firstname lastname", "[email protected]"));
mailMessage.Subject = "Hello there!";
mailMessage.Body = new TextPart("plain")
{
    Text = "test email!"
};

using (var smtpClient = new SmtpClient())
{
    smtpClient.Connect("smtp.gmail.com", 587);
    smtpClient.Authenticate("[email protected]", "password");
    smtpClient.Send(mailMessage);
    smtpClient.Disconnect(true);
}

But it is not working. I am using Blazor server side. The Return-path does not change Return-Path: [email protected]. Any ideas on how to set it, I am open also for other .net frameworks.

Thank you for your help

EDIT: Big thanks to @jstedfast for his engagement and taking the time to help.

What I am trying to achieve is to detect non-delivery receipts—or bounce messages. A solution for that (what I found) is to override the "Return-Path". According to the Simple Mail Transfer Protocol Page 51 RF A message-originating SMTP system SHOULD NOT send a message that already contains a Return-path header. SMTP servers performing a relay function MUST NOT inspect the message data, and especially not to the extent needed to determine if Return-path headers are present. SMTP servers making final delivery MAY remove Return-path headers before adding their own.

I tried adding to the header (and inserting at position 0), all that did is added a third "Return-Path" but did not override the original 2, which caused to work as intended.

Another solution that I found, but I don't know to implement it :

  • The primary purpose of the Return-path is to designate the address to which messages indicating non-delivery or other mail system failures are to be sent. For this to be unambiguous, exactly one return path SHOULD be present when the message is delivered. Systems using RFC 822 syntax with non-SMTP transports SHOULD designate an unambiguous address, associated with the transport envelope, to which error reports (e.g., non-delivery messages) should be sent.*

Variable envelope return path (VERP) is used to enable automatic detection and removal of undeliverable e-mail addresses. It works by using a different return path (also called "envelope sender") for each recipient of a message.

1

1 Answers

2
votes

Now that I better understand your question (due to follow-up comments made to this answer), it sounds like you are trying to replace(?) an existing Return-Path: header in a MimeMessage and then sending it again to see if the SMTP server notices the Return-Path: header and decides not to pass it along?

If so, then in order to insert a Return-Path: header to the top of the header block, you need to do this:

mailMessage.Headers.Insert (0, "Return-Path", "value");

If you want to replace an existing Return-Path: header, you can use the Replace() method, but be warned that if the Return-Path: header doesn't already exist, it will be added to the end of the header block, not the beginning of it.

Now that you know how to set (or replace) a Return-Path: header, the rest of your questions deals with page 51 of rfc2821. I'm not sure if your expectation is that MailKit's SmtpClient.Send() method should reject the message based on the existence of a Return-Path: header, but I don't think it should. I can also confirm (as the author of MailKit) that it currently does not reject such messages. That is up to the SMTP server and/or the application that makes use of MailKit to decide.