none
[E2010] [EWS] [MAPI] [C#] Open corresponding email in Outlook with ExchangeUniqueIDKey RRS feed

  • Question

  • Hi at all,

    I want to open a email from a Outllook add-in.

    The add-in has a list of items from the EWS.

    How can I open the corresponding email in outllook? 

    More information:

    The items contained in the list are items from the current users Inbox. It is sure that they aresome where in current outlook.

    Wednesday, April 13, 2011 8:45 AM

Answers

  • Hi Glen,

     

    after a lot of searching I found the reason.

    The Mailbox I tried to use, was set up as a IMAP mailbox. Not as a Exchange-Mailbox.

    With an Exchange-Mailbox GetItemFromId() is working, but only without StoreID... Pehaps I figure out what's the problem there.

     

    Do you have an idea how this can work if someone uses Exchange as IMAP Mailbox?

    Do I nee a self defined Property than?

     

    Thanks a lot for your help, CachedMode was not the reason but the last kick I needed ;-)

    • Marked as answer by prog_1 Thursday, April 21, 2011 6:44 AM
    Wednesday, April 20, 2011 4:16 PM

All replies

  • You can use Outlook EntryID or the EWSId and use a convertid operation to convert the ID so you can use it in either Outlook or EWS have a look at this old thread which has code to do this http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/e6f94da5-91d1-4b46-a7ee-1a8753f47747

    Cheers
    Glen

    Thursday, April 14, 2011 7:27 AM
  • I alredy tried it, but I have problems getting the messageitem afterwards.

    If I convert it I get a number like:

    BE3943B239C1314986FCA7AF429DF297CDF4A935AD3943498FE86CED9FDC9B1DCDF4A935AD3943498FE86CED9FDC9B62868C

     

    But the code does not work:

                Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();

                Microsoft.Office.Interop.Outlook.NameSpace session = application.Session;

                Microsoft.Office.Interop.Outlook.MailItem originalMailItem = session.GetItemFromID(

                 "00000000BE3943B239C1314986FCA7AF429D0F290700CDF4A935AD3943498FE86CED9FDC90B000000000001D0000CDF4A935AD3943498FE86CED9FDC90B000000062868A0000",           null) as Microsoft.Office.Interop.Outlook.MailItem;

     

    If I get the ID with mapi or redemption it looks like this:

    000000008F47279BCE5DCA4E83BF36FD78244693242A2000

     

    Dramatically shorter.

    With that number the code works well.

    Am I missing another conversion?

     

    Thanks in advance.

    Thursday, April 14, 2011 3:44 PM
  • What does the code where the original Id is coming from look like it looks like your using the wrong ID it maybe also that you need to search based on that ID rather then just trying to open in directly.

    Cheers
    Glen

    Friday, April 15, 2011 12:46 PM
  • Hi Glenn,

    tried two approaches and both have the same result:

     

    public static String GetOutlookEntryId(Item message)

           {

               AlternateId ewsId = new AlternateId(IdFormat.EwsId, message.Id.UniqueId.ToString(),"email@email.net");

               AlternateIdBase entryId = service.ConvertId(ewsId, IdFormat.EntryId);

               return Base64StringToHexString(((AlternateId)entryId).UniqueId);

           }

     

    and

     

    private static string getEntryId(string itemKey)

           {

               Item item = Item.Bind(service, itemKey);

               string entryId = string.Empty;

     

               ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(4095, MapiPropertyType.Binary);

               PropertySet propertySet = new PropertySet();

               propertySet.Add(extendedPropertyDefinition);

     

               item.Load(propertySet);

     

     

               foreach (var extendedProperty in item.ExtendedProperties)

               {

                   if (extendedProperty.PropertyDefinition == extendedPropertyDefinition)

                   {

                       string hex = BitConverter.ToString((byte[])extendedProperty.Value);

                       entryId = hex.Replace("-", "");

                   }

               }

     

               return entryId;

           }

     

    Do not know how to go on, hope you can help.

     

    Kind regards

    Fabian

    Friday, April 15, 2011 1:38 PM
  • Some additional input:

     

    To get the working id i am working with the item in a MAPI-Row.

    Than I am using HrArrayToString(object InputArray) from Redemption.IMAPIUtils

    The HrArray Method should not be the problem, because if I use it with the Array from EWS-Extendenprop I get the same like I get with

    string hex = BitConverter.ToString((byte[])extendedProperty.Value);

     

    Tried it with 

    ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(0x0FFF, MapiPropertyType.Binary);

    But long Id that I get could not get opened...


    Hope someone can help me.



    Monday, April 18, 2011 2:39 PM
  • Even the most simple example does not work:

     

    public ExchangeService service { get; set; }

            public string itemIKnow = "itemEWSIdYouKnow";

     

            void MainWindow_Loaded(object sender, RoutedEventArgs e)

            {

                System.Net.ServicePointManager.ServerCertificateValidationCallback =

                delegate(

                    Object obj,

                    X509Certificate certificate,

                    X509Chain chain,

                    SslPolicyErrors errors)

                {

                    return true;

                };

     

                service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);

                service.Credentials = new WebCredentials("user", "password", "domain");

                service.Url = new Uri("https://xxx/EWS/Exchange.asmx");

     

                Item item = Item.Bind(service, itemIKnow);

     

                string entryId = string.Empty;

                string storeId = string.Empty;

     

                ExtendedPropertyDefinition entryIdExtendedProperty = new ExtendedPropertyDefinition(4095, MapiPropertyType.Binary);

                ExtendedPropertyDefinition storeIdExtendedProperty = new ExtendedPropertyDefinition(4091, MapiPropertyType.Binary);

     

                PropertySet propertySet = new PropertySet();

                propertySet.Add(entryIdExtendedProperty);

                propertySet.Add(storeIdExtendedProperty);

     

                item.Load(propertySet);

     

                foreach (var extendedProperty in item.ExtendedProperties)

                {

                    if (extendedProperty.PropertyDefinition == entryIdExtendedProperty)

                    {

                        string hex = BitConverter.ToString((byte[])extendedProperty.Value);

                        entryId = hex.Replace("-", "");

                    }

                    else

                    {

                        string hex = BitConverter.ToString((byte[])extendedProperty.Value);

                        storeId = hex.Replace("-", "");

                    }

                }

     

                Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();

                Microsoft.Office.Interop.Outlook.NameSpace session = application.Session;

     

                var mapiNamespace = application.GetNamespace("MAPI");

                foreach (var store in mapiNamespace.Stores)

                {

                    Microsoft.Office.Interop.Outlook.Store myStore = store as Microsoft.Office.Interop.Outlook.Store;

                    MessageBox.Show(myStore.StoreID);

                    // Notice: The StoreId from Exchanges is not contained in this list.

                }

     

                Microsoft.Office.Interop.Outlook.MailItem originalMailItem = session.GetItemFromID(

                    entryId,

                    storeId) as Microsoft.Office.Interop.Outlook.MailItem;

     

                MessageBox.Show(originalMailItem.EntryID);

            }

    Tuesday, April 19, 2011 9:17 AM
  • In your convertID functions you should use IdFormat.HexEntryId

    What if you just pull the Id out with OutlookSpy or mfcmapi what you doing should work the Id's are pretty straight forward could it be you dont have rights to the objects in question.

    Cheers
    Glen

    Tuesday, April 19, 2011 9:59 AM
  • If I use the values from Outlookspy than there is no problem getting the item:

     

    storeId = "0000000038A1BB1005E5101AA1BB08002B2A56C200007073747072782E646C6C00000000000000004E495441F9BFB80100AA0037D96E0000000043003A005C00550073006500720073005C00700072006F00670031005C0041007000700044006100740061005C004C006F00630061006C005C004D006900630072006F0073006F00660074005C004F00750074006C006F006F006B005C0068006F0065006700650072004000610078006F006E00690063002E006E00650074002E007000730074000000";

                entryId = "000000008F47279BCE5DCA4E83BF36FD78244693C42E2000";

                Microsoft.Office.Interop.Outlook.MailItem originalMailItem = session.GetItemFromID(

                    entryId,

                    storeId) as Microsoft.Office.Interop.Outlook.MailItem;

     

                MessageBox.Show(originalMailItem.EntryID);

     

    The entryId is shorter than from ews and the storeId is longer than from ews.

     

    If I took the ids from ews the app does stop at getitemfromid for ever.

    Do not find code examples somewhere else...

    Tuesday, April 19, 2011 11:44 AM
  • EntryId returned from ConvertId:      AAAAAL45Q7I5wTFJhvynr0KdDykHAM30qTWtOUNJj+hs7Z/ckLAAAAAAAB0AAM30qTWtOUNJj+hs7Z/ckLAAAABjJ0QAAA==

    StoreId returnde from Convert Id:    RgAAAAC+OUOyOcExSYb8p69CnQ8pBwDN9Kk1rTlDSY/obO2f3JCwAAAAAAAdAADN9Kk1rTlDSY/obO2f3JCwAAAAYydEAAAA

     

    Than Base64StringToHexString(), perhaps this is wrong...

    Tuesday, April 19, 2011 12:58 PM
  • Is Outlook running in Cache Mode or Online mode ? If its running in cache mode you might want to try switching to online that short Id doesn't look correct to me but if you are running in cache mode that might explain it.

    Cheers
    Glen

    Wednesday, April 20, 2011 10:56 AM
  • Hi Glen,

     

    sorry but there is no change between Cached Mode and Online mode. I wonder why the shorter entryId from Outlookspy ist working.

    Therefore ther must something wrong with the exchange result...

    Can I somehow proof my Base64StringToHexString?

     

    Kind regards

    Fabian

    Wednesday, April 20, 2011 1:00 PM
  • Hi Glen,

     

    after a lot of searching I found the reason.

    The Mailbox I tried to use, was set up as a IMAP mailbox. Not as a Exchange-Mailbox.

    With an Exchange-Mailbox GetItemFromId() is working, but only without StoreID... Pehaps I figure out what's the problem there.

     

    Do you have an idea how this can work if someone uses Exchange as IMAP Mailbox?

    Do I nee a self defined Property than?

     

    Thanks a lot for your help, CachedMode was not the reason but the last kick I needed ;-)

    • Marked as answer by prog_1 Thursday, April 21, 2011 6:44 AM
    Wednesday, April 20, 2011 4:16 PM
  • If your dealing with IMAP that means the Outlook client is accessing the messages from its local PST file which is why the ID are different from those in the Exchange Mailbox which is what you get with EWS. There are big advantages to using MAPI to access exchange if you are stuck with IMAP then the ID I would use would be the Internet Message ID PR_INTERNET_MESSAGE_ID which sould be the same in both stores. You wont be able to bind to that ID directly you'll need to search for it in the folder.

    Cheers
    Glen

    Thursday, April 21, 2011 3:09 AM
  • Hi Glen,

    I will definitly try the PR_INTERNET_MESSAGE_ID.

    Does the Inter_Message_ID change if the item is moved?


    Kind regards 

    Fabian

     
    Thursday, April 21, 2011 6:46 AM
  • As far a i know it shouldn't and it should match what's stored in the message tracking logs on the server its not however guaranteed always to be unique eg it will be the same on copies of a message ect.

    Cheers
    Glen

    Saturday, April 23, 2011 3:49 AM