-->

c# programmatically reading emails from the Exchan

2019-02-01 14:53发布

问题:

When you search on web you will find very easy answers for "How to read emails programmatically"... Al the websites are explaining most of the same like this page. http://omegacoder.com/?p=454

// depends from Exchange server version      
        service.Credentials = new NetworkCredential("MDR", "password", "zzz");
        service.AutodiscoverUrl("mdr@zzz.be");
        object o = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
        foreach (Item item in findResults.Items)
        {
            Console.WriteLine(item.Subject);
        }

it fails when it executes the autodiscoverURL line. The error says "The Autodiscover service couldn't be located."

So I googled further and found this site from Microsoft https://www.testexchangeconnectivity.com/#&&/wEXAQUBcwUBME93h2+JjI0+MV2gTqcRL0g43z9m Here you can test your mail server.... When I pass the parameters I get the error below....

But I still don't understand what the problem is? Do I need to add a record to DNS ? Can someone help?

Attempting to test potential Autodiscover URL https://autodiscover.zzz.be/AutoDiscover/AutoDiscover.xml
 Testing of this potential Autodiscover URL failed.
 Test Steps
 Attempting to resolve the host name autodiscover.ncb.be in DNS.
 The host name resolved successfully.
 Additional Details
 IP addresses returned: 213.246.192.205

Testing TCP port 443 on host autodiscover.ncb.be to ensure it's listening and open.
 The specified port is either blocked, not listening, or not producing the expected response.
  Tell me more about this issue and how to resolve it
 Additional Details
 A network error occurred while communicating with the remote host.
Exception details:
Message: No connection could be made because the target machine actively refused it 213.246.192.205:443
Type: System.Net.Sockets.SocketException
Stack trace:
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
at Microsoft.Exchange.Tools.ExRca.Tests.TcpPortTest.PerformTestReally()




Attempting to contact the Autodiscover service using the HTTP redirect method.
 The attempt to contact Autodiscover using the HTTP Redirect method failed.
 Test Steps
 Attempting to resolve the host name autodiscover.zzz.be in DNS.
 The host name resolved successfully.
 Additional Details
 IP addresses returned: 213.246.192.205

Testing TCP port 80 on host autodiscover.zzz.be to ensure it's listening and open.
 The port was opened successfully.
ExRCA is checking the host autodiscover.zzz.be for an HTTP redirect to the Autodiscover service.
 ExRCA failed to get an HTTP redirect response for Autodiscover.
 Additional Details
 A Web exception occurred because an HTTP 404 - NotFound response was received from IIS7.



Attempting to contact the Autodiscover service using the DNS SRV redirect method.
 ExRCA failed to contact the Autodiscover service using the DNS SRV redirect method.
 Test Steps
 Attempting to locate SRV record _autodiscover._tcp.ncb.be in DNS.
 The Autodiscover SRV record wasn't found in DNS.
  Tell me more about this issue and how to resolve it

回答1:

You don't necessarily need to use the autodiscovery if you already know the address of your exchange server. Try the following instead (for more info, look here:

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

Replace "hostname" with the hostname for your exchange server.



回答2:

I hope you should have the solution by this time now. This is just to help anyone bumped on this post. I found the solution on one of the technet article, I twick to suite me, and is working fine for me.

Just replace the line in your code with following:

service.AutodiscoverUrl("user@yourdomain.com",
delegate
{
     return true;
});

I had some other issues but not related to this bit though.

Happy Coding,

Sanjay.



回答3:

I had the same issue with AutoDiscover. It's not necessary, you can specify your URL like

    Uri myUri = new Uri("https://Hostname/ews/exchange.asmx");
    userData.AutodiscoverUrl = myUri;
    service.Url = myUri;

As the hostname you can put the Server IP address like 192.168.100.10

Alternatively, to find what your Exchange server hostname is (in in fact the whole url to use) if you are using Outlook, go to your computer start bar, where the Date and time is showing, you will find the Outlook icon, hold Ctrl + right click on the outlook icon and click “Test Email Auto Configuration”

Check the "Use AutoDiscover" checkbox. Enter an email address hosted on that Exchange Server along with its password and you will recieve a bunch of url's. Use the 1 that says "Availability Service URL"



回答4:

Consider that the credentials being passed need to have permission to the given exchange mailbox / server. In my case using a different set of credentials that are properly permissioned works but not for a service account which I'm trying to get to work. Once I discover what exactly the account needs to be permissioned for I will update it here.

Update: My issue was the service account was from a domain different than the domain on which the exchange 2007 instance is running, even though there is a trust relationship between the two. I found this is a documented known issue in Exchange 2007 in how it looks up accounts in its forest. In the end had to create an identical service account (name/pass) on the domain on which the exchange server is sitting and specify username as {exchange_domain}{service_account_name}. The windows service that calls EWS runs as {original_domain}{service_account_name}.

For reference, the exception was: Microsoft.Exchange.WebServices.Data.ServiceResponseException: Failed to get valid Active Directory information for the calling account. Confirm that it is a valid Active Directory account.