使用 Exchange 2007 传输代理编辑 SMTP 标头

发布于 2024-08-10 10:08:41 字数 1267 浏览 2 评论 0原文

我目前正在编写一个 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
            }

        }

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
            }

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿光 2024-08-17 10:08:41

根据 RFC,返回路径标头应该由收件人的 SMTP 服务器设置。如果电子邮件中存在 Return-Path 标头,则收件人的服务器将删除并重置该标头。

也许您所看到的是 RFC 的正确实现。

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.

你是年少的欢喜 2024-08-17 10:08:41

我不确定,但听起来您可能想要更改“回复”标头而不是“返回路径”。 “return-path”是由服务器设置的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文