In SharePoint custom development, you may need to read user emails and display webpart,
This article is a summary of how to use the EWS managed API to operate on exchange mailboxes.
1. Understand the EWS managed API
EWS managed API is an application developed by Microsoft for exchange development on clients.ProgramInterface, and. NET Framework
The same as the class library, which provides simple and intuitive unified access to exchange resources.
2. How to Use the EWS managed API in A SharePoint Project
For more information about how to add such libraries to our SharePoint project, see add ewsmanagedapi reference in my previous blog Sharepoint.
I will not talk about it here.
Iii. Specific applications in SharePoint Projects
The following content describes the specific application based on the project I recently met.
3.1 instantiate an exchangeservice object and obtain the content in the inbox.
Servicepointmanager. servercertificatevalidationcallback = New Remotecertificatevalidationcallback (checkvalidationresult); exchangeservice Service = New Exchangeservice (exchangeversion. exchange2010 ); // For exchange 2010, switch to 2010 Icredentials creds = New Networkcredential (username, password, " Domain Name " ); Service. Credentials = New Webcredentials (creds); service. url = New Uri ( " Https: // server address/EWS/exchange. asmx " ); Service. preauthenticate = True ;
// Complete instantiation Itemview View = New Itemview ( 10 ); View. orderby. Add (itemschema. datetimereceived, Microsoft. Exchange. WebServices. Data. sortdirection. Descending ); Microsoft. Exchange. WebServices. Data. emailmessage em = Null ; Finditemsresults <Item> findmailresults = Service. finditems (wellknownfoldername. inbox, view ); Foreach (Item M In Findmailresults) {em = (Microsoft. Exchange. WebServices. Data. emailmessage) m;
// Action on em mail}
Servicepointmanager. servercertificatevalidationcallbackSet the callback used to verify the server certificate, and then instantiateExchangeserviceObject, create a Web authentication credential creds,
And grant This credentialService. credentials. The URL attribute can be directly used.The service address in the Exchange Server. You can also useExchangeserviceInThe autodiscoverurl method provides only oneExchange ServerAddress
You can,Exchangeservice automatically finds the corresponding service based on the server address.
Itemview is used to set the mail view, including the size and sorting rules. After the email is retrieved from inbox, the email is retrieved cyclically and operated accordingly.
3.2 read the content in the task folder
The instantiation is the same as the preceding one. The difference is that the accessed folder is different.
Finditemsresults <item> findresults = service. finditems (wellknownfoldername. Tasks, view );//Read the task folderMicrosoft. Exchange. WebServices. Data. Task T =Null;Foreach(ItemInFindresults. Items) {T=(Microsoft. Exchange. WebServices. Data. Task) (item );If(!T. iscomplete ){
// Specific operationCode
}}
The above content is the code to be applied immediately. The accessed files are not needed depending on the data to be obtained.
When Microsoft. Exchange. WebServices. Data is used, it is found that this enumeration contains many types. Based on different requirements, data query can be completed by accessing different types.
3.3 get owaid
Sometimes we need to jump to the OWA mail page after the user clicks the corresponding link. When splicing a URL, we will encounter an issue where the owaid cannot be obtained (the ID string in the URL). The corresponding code is attached below.
PublicString getoutlookowaid (emailmessage message, exchangeservice Ser) {alternateid ewsid=NewAlternateid (idformat. ewsid, message. Id. tostring (),"The user's email address is ** @ exchange.com"); Alternateidbase owaid=Ser. convertid (ewsid, idformat. owaid );Return(Alternateid) owaid). uniqueid ;}
This is basically the process of reading data from a mailbox. The next blog will write a Simulation of Single-point logon Based on the SharePoint secure Store service. For more information, see.