none
2010 Transport Agent to compare email senders to email addresses in a text file. RRS feed

  • Question

  • I am trying to create an Exchange Transport Agent which will replace email Subject line for a certain group of email senders. The senders email addresses will be stored in a text file. The recipient of the email will be a SharePoint document library and it will create a folder based on the Subject of the email message. Below is the code but it doesn't work. Can you take a look and tell me what is wrong? Thanks.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Exchange.Data.Transport.Routing;
    using Microsoft.Exchange.Data.Transport;
    using System.Globalization;
    using System.IO;
    using Microsoft.Exchange.Data.Transport.Email;
    
    namespace MyRoutingAgent
    {
        public sealed class MyAgentFactory : RoutingAgentFactory
        {
            public override RoutingAgent CreateAgent(SmtpServer server)
            {
                RoutingAgent myAgent = new ProposalLeadsClass();
                return myAgent;
            }
        }
    
        public class MyRoutingAgent : RoutingAgent
        {
            public const string dir = @"C:\ProgramFiles\Microsoft\ExchangeServer\TransportRoles\Pickup";
    
            public const string path1 = dir + "emailaddresses1.txt";
     
    
            public MyRoutingAgent()
            {
                //subscribe to different events 
                base.OnSubmittedMessage += new SubmittedMessageEventHandler(SRoutingAgent_OnSubmittedMessage);
            }
    
            void SRoutingAgent_OnSubmittedMessage(SubmittedMessageEventSource source, QueuedMessageEventArgs e)
            {
                try
                {
                    IEnumerator<EnvelopeRecipient> wrEnumerator = e.MailItem.Recipients.GetEnumerator();
                    while (wrEnumerator.MoveNext())
                        if (wrEnumerator.Current.Address.ToString() == "ShPtLibrary@contoso.com")
                        {
                            //compare senders email addresses with the emails in the text file 
                            //and if there is a match replace email's Subject line
                            List<RoutingAddress> emails = EmailAddressesDB.GetEmailAddresses(dir, path1);
                            if (emails.Contains(e.MailItem.FromAddress))
                                e.MailItem.Message.Subject = "Email Group 1";
    
                        }
                }
                catch //(Exception except)
                {
                }
            }
        }
    
        public class EmailAddressesDB
        {
            public static List<RoutingAddress> GetEmailAddresses(string dir, string path)
            {           
                StreamReader textIn;
                List<RoutingAddress> emails;
                string line;           
                try
                {
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
                    
                    textIn = new StreamReader(new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
                    emails = new List<RoutingAddress>();
    
                    while ((line = textIn.ReadLine()) != null)
                    {
                        RoutingAddress emailAdd = new RoutingAddress(line);
                        emails.Add(emailAdd);
                    }      
                }
                finally
                {
                    textIn = null;
                    textIn.Close();
                }
                return emails;
            }
        }
    }


    sean zhang signature


    Thursday, April 26, 2012 7:54 PM