我目前正在编写一个 Exchange 2007 传输代理来替换来自特定发件人的所有外发邮件中的某些标头。我成功地替换了“From”SMTP 标头,但重写“Return-Path”标头似乎不起作用。
为了实现这一切,我编写了一个自定义 SmtpReceiveAgent 并订阅 OnEndOfData 事件,如下所示:
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
}
}