none
Exchange custom Transport Agent code doesn't work. How do I check the To feild? RRS feed

  • Question

  • I am trying to create a custom Transport agent. This piece of code doesn't work for me:

    if (e.MailItem.Message.To.Equals("timesheet@contoso.com"))
     e.MailItem.Message.Subject = "Timesheet";

    Can you tell what is wrong? I have tried to replace it with:

    if (e.MailItem.Message.To.ToString() == "timesheet@contoso.com")
    e.MailItem.Message.Subject = "Timesheet";

    And it still doesn't work. Thanks.


    sean zhang signature

    Monday, April 16, 2012 9:10 PM

Answers

  • Sean,

    Yes, it's the same for 1 recipient as 100.

    The other option is to look at the raw headers but that too requires an enumerator.

    I'm typing this from memory (untested, so forgive any syntax errors) but the code would look something like this ...

    System.Collections.Generic.IEnumerator<EnvelopeRecipient> seansEnumerator = e.MailItem.Recipients.GetEnumerator(); while (seansEnumerator.MoveNext()) { if (seansEnumerator.Current.Address.ToString() == "someaddress@somedomain.com") { // Do something

    break; } }

    Regards,


    Scott Quinn | C# developer & messaging specialist (for hire). Contact me at http://au.linkedin.com/in/scottquinn


    • Edited by Scott Quinn Tuesday, April 17, 2012 1:33 PM Fix a typo
    • Marked as answer by SeanZhang2012 Wednesday, April 25, 2012 1:03 PM
    Tuesday, April 17, 2012 1:29 PM

All replies

  • I forgot to mention that timesheet@contoso.com is a distribution group. And it forwards email messages to timesheet@companyweb which is a document library at SharePoint site. Can the code above work for a disrtibution group?

    sean zhang signature

    Monday, April 16, 2012 11:31 PM
  • Hi Sean,

    No, you can't do it that way.

    Use an enumerator on e.MailItem.Recipients (which is an EnvelopeRecipientCollection) and check the current value of the enumerator's "Address" for the address you're after.

    Regards,


    Scott Quinn | C# developer & messaging specialist (for hire). Contact me at http://au.linkedin.com/in/scottquinn

    Tuesday, April 17, 2012 12:43 PM
  • Thank you for your reply, Scott. There is only one recipient in the distribution group which is a mail contact for the SharePoint document library. Do I have to use foreach loop? Can you tell me what would be the exact code? Thanks.

    sean zhang signature

    Tuesday, April 17, 2012 1:02 PM
  • Sean,

    Yes, it's the same for 1 recipient as 100.

    The other option is to look at the raw headers but that too requires an enumerator.

    I'm typing this from memory (untested, so forgive any syntax errors) but the code would look something like this ...

    System.Collections.Generic.IEnumerator<EnvelopeRecipient> seansEnumerator = e.MailItem.Recipients.GetEnumerator(); while (seansEnumerator.MoveNext()) { if (seansEnumerator.Current.Address.ToString() == "someaddress@somedomain.com") { // Do something

    break; } }

    Regards,


    Scott Quinn | C# developer & messaging specialist (for hire). Contact me at http://au.linkedin.com/in/scottquinn


    • Edited by Scott Quinn Tuesday, April 17, 2012 1:33 PM Fix a typo
    • Marked as answer by SeanZhang2012 Wednesday, April 25, 2012 1:03 PM
    Tuesday, April 17, 2012 1:29 PM
  • Thank you Scott, I am goint to try that. Another question, should I use base.OnSubmittedMessage or base.OnResolvedMessage to change email message Subject line?

     


    sean zhang signature

    Tuesday, April 17, 2012 2:52 PM
  • Sean,

    For the subject I don't recall that it matters.

    Generally as messages pass though the categoriser on each event more of the message properties become read-only so for many (but not all) properties earlier is better.

    Regards,


    Scott Quinn | C# developer & messaging specialist (for hire). Contact me at http://au.linkedin.com/in/scottquinn

    Tuesday, April 17, 2012 3:16 PM
  • Hi Scott,

    Below is complete code of my Transport Agent. I have tried that and it seems to be working fine, thanks to you. But I got another questions. It looks like for every email message which comes to the Exchange server the agent is going to create IEnumerator object. Is it going our server slow processing all the messages? 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;
    
    namespace MyTimesheetAgent
    {
        public sealed class MyAgentFactory : RoutingAgentFactory
        {
            public override RoutingAgent CreateAgent(SmtpServer server)
            {
                RoutingAgent myAgent = new myRoutingAgent();
                return myAgent;
            }
        }
    
        public class myRoutingAgent : RoutingAgent
        {
            public myRoutingAgent()
            {
                //subscribe to different events 
                base.OnSubmittedMessage += new SubmittedMessageEventHandler(SRoutingAgent_OnSubmittedMessage);
            }
            void SRoutingAgent_OnSubmittedMessage(SubmittedMessageEventSource source, QueuedMessageEventArgs e)
            {
                try
                {
                    IEnumerator<EnvelopeRecipient> tsEnumerator = e.MailItem.Recipients.GetEnumerator();
                    while(tsEnumerator.MoveNext())
                        if(tsEnumerator.Current.Address.ToString() == "timesheet@contoso.com")
                        {
                            e.MailItem.Message.Subject = DateTime.Now.ToString("MMMM yyyy");
                        }
                }
                catch //(Exception except)
                {
                }
            }
        }
    }


    sean zhang signature

    Tuesday, April 17, 2012 4:00 PM
  • Sean,

    No - not noticeably, although you could break out of the loop once you're happy you've found the address you're looking for OR haven't found the address you were looking for.

    And you can do a lot more processing than that in an event handler before you start to slow things down :-)

    Cheers,


    Scott Quinn | C# developer & messaging specialist (for hire). Contact me at http://au.linkedin.com/in/scottquinn


    • Edited by Scott Quinn Tuesday, April 17, 2012 4:19 PM Clarification
    Tuesday, April 17, 2012 4:15 PM
  • One more question: I am getting these error messages in the Exchange server Event Viewer -  7 messages have reached or exceeded the configured poison threshold of 2. After the Microsoft Exchange Transport service restarted, these messages were moved to the poison message queue.

    The recepients of the email messages are MS SharePoint document libraries. The email attachements are saved to the libraries, but what happens to the email messages. Should I write some code to discard those messages? Thank you.


    sean zhang signature

    Tuesday, April 17, 2012 8:00 PM
  • Sean,

    Poison messages are what happens when an agent is throwing an (unhandled) exception.

    If this has just started it's likely there is (or was) a bug. So if the messages are old (from a previous version of your transport agent) I wouldn't worry, however if you're getting new poison message messages you probably have a bug there.... somewhere, although other things can cause this too like building your agent against the wrong version of the exchange dlls.

    Regards,


    Scott Quinn | C# developer & messaging specialist (for hire). Contact me at http://au.linkedin.com/in/scottquinn

    Tuesday, April 17, 2012 9:32 PM