0

we need to search email from outlook.com and to achieve this we are using Exchange Web Service (EWS) but getting 407 error a the time of calling FindItem method of service.

Here is the code which we are working on -

List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
        searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Test"));
        //searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Body, "homecoming"));
        SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());

        ItemView view = new ItemView(50);
        // Identify the properties to return in the result set and the additional properties that are returned for each item.
        view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived);
        //Order the search results by the DateTimeReceived property. The sort direction is in descending order.

        view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);

        //Set the manner by which the search filter traverses the target folder. In the following example, the search filter performs a shallow traversal. Shallow is the default option; other traversal options are Associated and SoftDeleted.

        view.Traversal = ItemTraversal.Shallow;

        string userEmailAddress = "username@outlook.com";
        string userPassword = "OutlookPassword";
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);


        service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
        service.Credentials = new WebCredentials(userEmailAddress, userPassword);
        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);

Getting error at last line of code.

Can you please guide what need to be correct to resolve it? Thank You!

Oxygen
  • 831
  • 4
  • 17
  • 42
  • If you call service.AutodiscoverUrl(userEmailAddress) does it set the Uri to https://outlook.office365.com/EWS/Exchange.asmx? – Marcus Höglund Jun 15 '16 at 12:05
  • Marcus, I tried this by setting url as - service.AutodiscoverUrl(userEmailAddress, RedirectionUrlValidationCallback); but it gives error and after checking exception error data it says "The e-mail address cannot be found." Verified this and the email is correct one. – Oxygen Jun 15 '16 at 12:31
  • Do you get the same error when just passing the email in the autodiscover like this service.AutodiscoverUrl(userEmailAddress)? (You don't need the callback here) – Marcus Höglund Jun 15 '16 at 12:34
  • Yes, I got error as "Autodiscover blocked a potentially insecure redirection to https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml. To allow Autodiscover to follow the redirection, use the AutodiscoverUrl(string, AutodiscoverRedirectionUrlValidationCallback) overload." when i tried service.AutodiscoverUrl(userEmailAddress) – Oxygen Jun 15 '16 at 12:59
  • This is an old question here but it seems Outlook didn't use to support EWS back in 2012..http://stackoverflow.com/questions/17609863/does-outlook-com-support-ews – Marcus Höglund Jun 15 '16 at 13:04
  • Ohh.. Then what would be the best option to sear emails from outlook.com? Actually I am looking to implement a functionality which will search emails by subjects or by from email address. Is it possible doing in any other way? – Oxygen Jun 15 '16 at 13:11
  • Either you use Microsoft.Office.Interop.Outlook namespace but that requires office package. An other way is to use pop3, IMAP clients. There's plenty examples on the webb for that. Here's one on pop3 https://sourceforge.net/projects/hpop/ – Marcus Höglund Jun 15 '16 at 13:17
  • Thanks for your time and guidance Marcus. I'll look into this. – Oxygen Jun 15 '16 at 13:33
  • I'd suggest you use the new REST api's https://dev.outlook.com/ they will work across Outlook.com and Office365 (these have actually be consolidated to a single platform http://www.howto-outlook.com/howto/outlookcommigrationinfo.htm ). – Glen Scales Jun 16 '16 at 02:23

0 Answers0