开发者

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("zxzxzxzxzx@email.com", "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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜