简体   繁体   中英

Get the to email address using EWS for exchange 2013

I'm trying to get the actual 'To:' email address that an email is sent to using EWS 2013. So I can run different methods based on the email address that a mail is sent to. My issue is similar to this question Exchange Web Services (EWS) API "To" header for alias in that the alias I'm sending to is replaced to the primary email address for the mailbox.

I have successfully manage to retrieve the To: address in the transport headers using above article but only if the email originates outside of the exchange organisation. If the email is sent by another user in the domain only the primary email address is visible in the headers. How can I get the actual email address a message was sent to..??

ExtendedPropertyDefinition PR_TRANSPORT_MESSAGE_HEADERS = new ExtendedPropertyDefinition(0x007D, MapiPropertyType.String);
                    PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties) { PR_TRANSPORT_MESSAGE_HEADERS, ItemSchema.MimeContent };
                    Item NewItem = Item.Bind(Service, itemEvent.ItemId, propSet);
                    if (NewItem is EmailMessage)
                    {
                        NewItem.Load(propSet);
                        Object valHeaders = null;
                        string emailaddress = "";
                        if (NewItem.TryGetProperty(PR_TRANSPORT_MESSAGE_HEADERS, out valHeaders))
                        {
                            //Console.WriteLine((String)valHeaders);
                            Regex regex = new Regex(@"for.*<(.+)>|To:.*<(.+)");
                            Match match = regex.Match(valHeaders.ToString());
                            if (match.Groups[0].Value.ToString().Contains("for"))
                            {
                                emailaddress = match.Groups[1].Value;
                            }
                            if (match.Groups[0].Value.ToString().Contains("To:"))
                            {
                                emailaddress = match.Groups[2].Value;
                            }                                
                        } 

                        if ("this@domain.com" == emailaddress)
                        {
                           dothis();
                        }
                        if ("that@domain.com" == emailaddress)
                        {
                            dothat();
                        }
                    }

solution is to create a distribution group and add mailbox to this and do not add the alias directly under the mailbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM