Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing an SMTP header with an Exchange 2007 Transport Agent

Tags:

I'm currently writing an Exchange 2007 Transport Agent to replace some headers in all outgoing mails from a particular sender. I managed to replace the 'From' SMTP header successfully, but rewriting the 'Return-Path' header does not seem to work.

To make this all happen, I have written a custom SmtpReceiveAgent and subscribe to the OnEndOfData event like this:

private static void MyAgent_OnEndOfData(ReceiveMessageEventSource source, EndOfDataEventArgs e)
        {

            try
            {
                var address = e.MailItem.Message.From.SmtpAddress;
                if (address.ToLower().EndsWith("[internal email domain]"))
                {
                    // replace the From: header - WORKING FINE!
                    e.MailItem.Message.From = new EmailRecipient("[displayname]",
                                                                 "[email address]");

                    // replace the Return-Path: header - NOT WORKING!
                    var headerList = e.MailItem.Message.RootPart.Headers;
                    var header = (AddressHeader)headerList.FindFirst("Return-Path");
                    var newheader = new AddressHeader("Return-Path") { Value = "[email address" };
                    headerList.ReplaceChild(newheader, header);
                }
            }
            catch (Exception ex)
            {
               // do something useful here
            }

        }
like image 658
Thomas Vochten Avatar asked Nov 04 '09 13:11

Thomas Vochten


2 Answers

Per the RFCs, the Return-Path header is supposed to be set by the recipient's SMTP server. If a Return-Path header exists in the email, it is to be removed, and reset by the recipient's server.

Maybe what you are seeing, is the correct implementation of the RFCs.

like image 70
dave wanta Avatar answered Oct 16 '22 02:10

dave wanta


I'm not sure, but it sounds like you might want to be changing the "reply-to" header and not "return-path". "return-path" is meant to be set by the server.

like image 25
Dave Archer Avatar answered Oct 16 '22 00:10

Dave Archer