桃李不言,下自成蹊;软件工程师,非某语言的程序员

Exchange 2007 自定义传输规则

需求:公司域名从aep.com 修改为casoo.com 原域名仍可接收邮件,但是希望发到原域名的邮件 自动回复发送者告知:邮件域名已变更

 

原有的传输规则无法实现上述功能,那就只有自定义了。

首先copy 两个dll到开发服务器上

Microsoft.Exchange.Data.Transport.dll ,Microsoft.Exchange.Data.Common.dll (C:\Program Files\Microsoft\Exchange Server\Public directory) 

你可以编写两种传输规则:SmtpReceiveAgent,RoutingAgent

这里使用 后者 开始编码了,新建一个类库项目

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Exchange.Data.Transport;
using Microsoft.Exchange.Data.Transport.Routing;
using System.Diagnostics;

namespace AutoReplayForCustomerDomainRule
{
    public sealed class AutoReplayForCustomerDomainRoutingAgentFactory : RoutingAgentFactory
    {
        public override RoutingAgent CreateAgent(SmtpServer server)
        {
            return new AutoReplayForCustomerDomainRoutingAgent();
        }
    }

    public class AutoReplayForCustomerDomainRoutingAgent : RoutingAgent
    {
        public AutoReplayForCustomerDomainRoutingAgent()
        {
            base.OnSubmittedMessage += new SubmittedMessageEventHandler(AutoReplayForCustomerDomainRoutingAgent_OnSubmittedMessage);
            //base.OnResolvedMessage += new ResolvedMessageEventHandler(AutoReplayForCustomerDomainRoutingAgent_OnResolvedMessage);
        }

        void AutoReplayForCustomerDomainRoutingAgent_OnSubmittedMessage(SubmittedMessageEventSource source, QueuedMessageEventArgs e)
        {
            if (e.MailItem.FromAddress.IsValid)
            {
                string rpdomain = string.Format("{0}@{1}", e.MailItem.FromAddress.LocalPart, e.MailItem.FromAddress.DomainPart);
                bool isNeedReply = false;
                //e.MailItem.Message.Subject += rpdomain;
                //e.MailItem.Message.Subject += "|" + AppConfig.AutoReplyDomain;
                foreach (EnvelopeRecipient ep in e.MailItem.Recipients)
                {
                    if (ep.Address.IsValid && ep.Address.DomainPart.ToLower() ==AppConfig.AutoReplyDomain)
                    {
                        isNeedReply = true;
                        break;
                    }
                }

                if (isNeedReply)
                { 
                    //                  
                    EWSService.Create().SendMail(AppConfig.AutoReplySubject, AppConfig.AutoReplyMessage, new string[] { rpdomain });
                }
                
            }
        }             
      
    }
}

 

部署:

打开Exchange命令行

执行下面的命令:

net stop msexchangetransport # to stop the exchange transport service

install-transportagent -name "autoreply_v1" -assemblypath c:\Transrule\AutoReplayForCustomerDomainRule.dll -transportagentfactory AutoReplayForCustomerDomainRule.AutoReplayForCustomerDomainRoutingAgentFactory
enable-transportagent -identity "autoreply_v1"

get-transportagent -identity "autoreply_v1"

net start msexchangetransport

posted @ 2011-04-17 12:02  假正经哥哥  阅读(1888)  评论(1编辑  收藏  举报