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", "another@email.com"));
mailMessage.Sender = new MailboxAddress("firstname lastname", "another@email.com");
mailMessage.To.Add(new MailboxAddress("firstname lastname", "receiver@gmail.com"));
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("sender@gmail.com", "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: sender@gmail.com. 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.