locked
Exchange 2010 EWS and retrieving To and From Addresses RRS feed

  • Question

  • I'm using Microsoft EWS notification streaming to monitor a mailbox for new messages.  It is doing exactly what I need it to do except i'm having trouble pulling out the new email addresses from the message for To and From.  I'm using Microsoft's sample application they gave me here http://www.microsoft.com/en-us/download/details.aspx?id=27154 but can't figure it out.  Any help is appreciated.

    private static void OnNotificationEvent(object sender, NotificationEventArgs args)

                  {

                         // Extract the item ids for all NewMail Events in the list.

                         var newMails = from e in args.Events.OfType<ItemEvent>()

                                        where e.EventType == EventType.NewMail

                                        select e.ItemId;

                         // Note: For the sake of simplicity, error handling is ommited here.

                         // Just assume everything went fine

                         var response = _ExchangeService.BindToItems(newMails,

                                                                     new PropertySet(BasePropertySet.IdOnly, ItemSchema.DateTimeReceived,

                                                                      ItemSchema.UniqueBody, ItemSchema.Subject,

                                                                      ItemSchema.DisplayTo, ItemSchema.InternetMessageHeaders,

                                                                      ItemSchema.Body));

          var items = response.Select(itemResponse => itemResponse.Item);

          //ExtendedPropertyDefinition transportMsgHdr = new ExtendedPropertyDefinition(0x007D, MapiPropertyType.String);

                         foreach (var item in items)

                         {

            Console.Out.WriteLine("A new mail has been created. Received on {0}", item.DateTimeReceived);

            Console.Out.WriteLine("Subject: {0}", item.Subject);

            Console.Out.WriteLine("To: {0}", item.DisplayTo);

            Console.Out.WriteLine("Body: {0}", item.Body);

            Console.Out.WriteLine("ID: {0}", item.Id);

            Console.Out.WriteLine("Headers: {0}", item.InternetMessageHeaders);

                         }

           }


    Michael Duhon

    Wednesday, May 28, 2014 1:55 PM

All replies

  • You need to include the To and From properties in the property set your using in the Bind statement change

     var response = _ExchangeService.BindToItems(newMails,
    
                                                                     new PropertySet(BasePropertySet.IdOnly, ItemSchema.DateTimeReceived,
    
                                                                      ItemSchema.UniqueBody, ItemSchema.Subject,
    
                                                                      ItemSchema.DisplayTo, ItemSchema.InternetMessageHeaders,
    
                                                                      ItemSchema.Body, EmailMessageSchema.ToRecipients, EmailMessageSchema.From));

    Exchange will only return the properties that you ask it to.

    Cheers
    Glen

    Thursday, May 29, 2014 5:07 AM
  • that is exactly what I needed thanks.  Now in my test application i'm providing the username and password for the mailbox I want to monitor and works great, but is there a way to provide and administrator user name and password that has full mailbox permissions to monitor different mailboxes?

    Michael Duhon

    Thursday, May 29, 2014 5:05 PM
  • So if you want to use a Service Account you need to access the target Mailbox using delegate access in EWS. This is pretty easy to do in the code look for where the FolderId Class is being used eg

    new FolderId(WellKnownFolderName.Inbox)

    so this will mean it will use the Inbox of the current user, with this class there is an overload the allow you to specify the PrimarySMTPaddress of the Mailbox you want to access eg

    new FolderId(WellKnownFolderName.Inbox,"mailboxtoAccess@domain.com")

    and as long as you have FullAccess to that mailbox or folder it should work okay

    Cheers
    Glen

    Friday, May 30, 2014 1:37 AM