Use EWS to access Exchange emails

Source: Internet
Author: User

Today, I tried to use EWS to access exchange emails. It was very simple on the Internet, but I encountered more than N problems. However, I still succeeded in the end, and I did not want to work overtime. Record some methods and problems, and hope to help those who need to develop similar exchange functions.

The first step is to download the Exchange Web Services Managed API. This DLL encapsulates a lot of access to EWS, which is much easier than directly using the proxy class generated by EWS ,:

Http://www.microsoft.com/downloads/en/details.aspx? FamilyID = c3342fb3-fbcc-4127-becf-872c746840e1

There are many examples of code using EWS on the Internet, and there are also some on the Microsoft site. There are two articles on this in the blog Park (the next article does not provide a link ):

Http://www.cnblogs.com/diaojia/archive/2010/10/19/1855839.html

The code is relatively simple. Here I paste my test code:

Public class AccessInfo {public string UserName; public string Password; public string Domain; public string ServerUrl; public string Email;} class Program {static void Main (string [] args) {AccessInfo info = new AccessInfo () {UserName = "administrator", Password = "p @ ssw0rd", Domain = "contoso.com", ServerUrl = "https://contoso-exchange.contoso.com/ews/Exchange.asmx Email =" administrator@contoso.com "}; readMail (info); Console. read ();} static void ReadMail (AccessInfo Info) {// The ExchangeService version is 2010 ExchangeService service = new ExchangeService (ExchangeVersion. exchange2010); // The parameter is the user name, password, and domain service. credentials = new WebCredentials (Info. userName, Info. password, Info. domain); // the URL of the Exchange Server http: // xxxxxxx service. url = new Uri (Info. serverUrl); // your own email address xxx@xxx.xxx // service. autodiscoverUrl (Info. email); // create a filter with the condition that the Email is not read. searchFilter sf = new SearchFilter. iseconto (EmailMessageSchema. isRead, false); // search for Inbox, Add filter conditions, and the result is 10 FindItemsResults <Item> findResults = null; try {findResults = service. findItems (WellKnownFolderName. inbox, sf, new ItemView (10);} catch (Exception ex) {Console. writeLine (ex. message);} foreach (Item item in findResults. items) {EmailMessage email = EmailMessage. bind (service, item. id); Console. writeLine (email. subject );}}}",
 
This code is the code that is successfully tested. The following describes the problems encountered.
1. An exception occurred at service. AutodiscoverUrl (Info. Email): AutodiscoverUrl cocould not be located
In the end, this problem was not fundamentally solved. I simply commented on this line, because I think autodiscover is unnecessary since the service. Url is specified;
   
2. An exception occurs at service. FindItems (WellKnownFolderName. Inbox, sf, new ItemView (10) and an error occurs at Http404.
This problem has been depressing for a long time, because the written URL is clearly correct and can be accessed in the browser or used to generate a proxy class. When you are about to give up later, I found someone who has the same experience with me.
Post: http://www.cnblogs.com/bear-study-hard/archive/2010/10/11/184720..html. This is because SSL is disabled on the EWS site. Indeed
SSL is also disabled on my EWS site. I prefer not to receive a warning that the certificate is invalid when I access the site in a browser. Therefore, SSL is added, and http is changed to https (http written in the test at the beginning ).
 
3. When SSL is enabled, the message "failed to establish trust relationship for SSL/TLS Security Channel" is displayed in FindItems ".
After the certificate is finished, the problem arises. I have never understood anything, but I still don't know how to set a certificate for the IIS Site, and how to install the certificate on the client. Google has two articles, one of which is win2003 IIS6,
One is win2008 IIS7:
        win2003 IIS6:http://www.cnblogs.com/kimzeng/archive/2009/12/17/1626171.html
        win2008 IIS7:http://blog.csdn.net/steel_ligang/archive/2009/09/25/4593102.aspx
Here I want to despise MS, which is a disruptive operation between two generations of products. I have introduced a lot in the two articles, but both of them lack the information about how to submit the applied certificate and how to obtain the issued certificate.
Operation. The following is a brief introduction:
 
    Certificate submitted for application
Open: expose in the browser:
 

Click "apply for a certificate", select "Advanced Certificate Application", and then select "base64 encoded CMC or PKCS #10 file to submit a certificate application, or use the base64 encoded PKCS #7 file to renew the certificate application. ", Shows the most open page:

Paste all the content in the generated HASH code text file in the text box above, and click Submit. The certificate application is complete.

Then open the Certificate Authority, find "pending application", find the requested certificate in the list on the right, right-click all tasks, and issue the certificate.

For newly applied certificates, go to the Certificate Management home page (http://contoso.com/certsrv/), select view pending certificate application status, and the download link is available on the following page.

I would like to remind you that you must install IIS before installing the Certificate Service. Otherwise, there will be no certificate service page on the WEB.

Back to the problem mentioned at, after a long time, I still failed to add the Certificate in my IIS. I found an article by Microsoft later:

Http://support.microsoft.com/kb/981954/zh-cn

The key point is to create a self-signed or $ self-issued root certificate. Although this sentence does not appear in the solution described in this article, it reminds me that

It may be useful to create a self-signed certificate. The server certificate management interface in IIS7 provides the function of creating a self-signed certificate, which is not detailed in the process. After creating the certificate, remember to change the certificate bound to the EWS site to the certificate you just created. After the modification, make sure that the Service URL domain name specified in the program is consistent with the "issued to" attribute in the certificate.

Note: Do not add a Host header to the EWS site unless you are familiar with the WCF configuration file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.