惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
月光博客
月光博客
博客园 - Franky
Martin Fowler
Martin Fowler
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
The Cloudflare Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享
J
Java Code Geeks
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
MongoDB | Blog
MongoDB | Blog
腾讯CDC
博客园_首页
博客园 - 司徒正美
D
DataBreaches.Net
I
InfoQ
GbyAI
GbyAI
IT之家
IT之家
罗磊的独立博客

博客园 - 假正经哥哥

自定义PlantUML和C4 Model样式 - 假正经哥哥 - 博客园 [dotnet core]使用Peach简化Socket网络通讯协议开发 发布xxl-job executor dotnet core 执行器的实现 Service Discovery And Health Checks In ASP.NET Core With Consul [译]Serilog Tutorial 使用DotNetty编写跨平台网络通信程序 基于DotNet Core的RPC框架(一) DotBPE.RPC快速开始 - 假正经哥哥 使用CSharp编写Google Protobuf插件 使用VsCode编写和调试.NET Core项目 C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件 xEasyApp之后端的介绍 如果利用xjplugin编写基于web的应用系统 Xgcalendar 新增Php demo jquery xgcalendar ,tree,contextmenu,tabpanel 插件遭遇盗版 基于jQuery打造TabPanel 安装Office2010后Vs2008打开网页文件假死的问题 Python 学习笔记(半ZZ半自己写) Python 学习记录(2) 杯具的GQL Python 学习记录(1)对象命名导致的问题
Exchange 2007 自定义传输规则
假正经哥哥 · 2011-04-17 · via 博客园 - 假正经哥哥

需求:公司域名从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