anleon
anleon

Reputation: 632

I need to get emails with help of EWS in C#

Here is my code:

using System;
using System.Net;
using MailListClient.MailListServiceReference;
using Microsoft.Exchange.WebServices.Autodiscover;
using Microsoft.Exchange.WebServices.Data;

namespace MailListClient
{
   class Program
   {
       static void Main(string[] args)
       {
           var service = GetBinding();

           ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

           try
           {
               FindItemsResults<Item> findResults = service.FindItems(
                   WellKnownFolderName.Inbox,
                   new ItemView(10));

               foreach (Item item in findResults.Items)
                   Console.WriteLine(item.Id);
           }
           catch (Exception ex)
           {
               Console.WriteLine("Error: " + ex.Message);
               Console.ReadLine();
           }
       }

       static ExchangeService GetBinding()
       {
           ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
           service.Credentials = new WebCredentials("[email protected]", "password");
           try
           {
               service.Url = new Uri("https://email.com/AutoDiscover/AutoDiscover.xml");
           }
           catch (AutodiscoverRemoteException ex)
           {
               Console.WriteLine("Exception thrown: " + ex.Error.Message);
           }
           return service;
       }
   }
}

But I get Exception: The request failed. The remote server returned an error: (404) Not Found. What is the problem?

Upvotes: 2

Views: 3443

Answers (3)

user6911980
user6911980

Reputation:

For EWS this is triggered by an alias being used to override SMTP headers.

Check the mail properties of an address for it's SMTP address and any aliases, the alias will have priority.

Upvotes: 0

Tim M.
Tim M.

Reputation: 54378

Your URL may be wrong. Here is the URL format I am using in code which is definitely working in a few applications (2007 and 2010):

https://[fully qualified domain name]/EWS/Exchange.asmx

The rest of your code looks correct at a cursory level.

Upvotes: 3

Dan
Dan

Reputation: 13343

Because the file you are requesting does not exist. I know that sounds purely antagonistic, but it's the simplest answer.

Follow this procedure as needed:

  1. Get out Fiddler2 and trace the request. Is it going where you think it should be going?
  2. Make sure you haven't spelt anything wrong.
  3. Refer to the docs your IT support has given you
  4. Go to your IT with the url and ask why it's not working.

Upvotes: 3

Related Questions