SharePoint read unread mails

Source: Internet
Author: User

Add the Web servers of OWA. The address is https: // mail. ***. com/EWS/services. WSDL.

Second, you need to create an account on OWA with the permission to simulate other accounts to read emails,

Open the exchange 2010 server and enter

New-managementroleassignment-Name: impersonationassignmentname-Role: applicationimpersonation-user: serviceaccount

In 2007, you need to set OWA to window logon, and disable anonymous logon in the Directory Security of the virtual directory EWS in IIS on the mail server.

 

Serviceaccount: the account of another user.

Code:

 

 

1 /// <summary>
2 // get unread emial number
3 /// </Summary>
4 /// <returns> </returns>
5 protected int getunreadmailcount ()
6 {
7
8 try
9 {
10 string connectionstring = "https: // mail. ***. cn | spmanager | sccdu @ SPS | ***. cn ";
11
12 INT unreadcount = 0;
13
14
15 // bind the Exchange Server. here you need to add a web reference, https: // mail. ***. CN/EWS/exchange. asmx
16 exchangeservicebinding exchangeserver = new exchangeservicebinding ();
17 // The following sentence can be solved: "The basic connection has been closed: the trust relationship cannot be established for the SSL/TLS security channel"
18 system. net. servicepointmanager. servercertificatevalidationcallback =
19 delegate (Object OBJ, system. Security. cryptography. x509certificates. x509certificate certificate, system. Security. cryptography. x509certificates. x509chain chain, system. net. Security. assumerrors)
20 {
21 return true;
22 };
23 // establish a trusted connection
24 // exchangeserver. Credentials = system. net. credentialcache. defaultnetworkcredentials;
25 // exchangeserver. Credentials = system. net. credentialcache. defaultcredentials;
26 // exchangeserver. usedefadefacredentials = true;
27 string url = string. Format ("{0}/EWS/exchange. asmx", connectionstring );
28 If (connectionstring. indexof ("|")>-1)
29 {
30 string [] STRs = connectionstring. Split ('| ');
31 url = string. Format ("{0}/EWS/exchange. asmx", STRs [0]);
32 system. net. networkcredential NC = new system. net. networkcredential (STRs [1], STRs [2], STRs [3]);
33 string useremail = spcontext. Current. Web. currentuser. Email;
34 exchangeserver. Credentials = NC;
35 exchangeimpersonationtype exexexchangeimpersonation = new exchangeimpersonationtype ();
36 connectingsidtype csconnectingsid = new connectingsidtype ();
37 csconnectingsid. primarysmtpaddress = useremail;
38 exexchangeimpersonation. connectingsid = csconnectingsid;
39 exchangeserver. exchangeimpersonation = exexexchangeimpersonation;
40}
41 else
42 {
43 exchangeserver. Credentials = system. net. credentialcache. defaultnetworkcredentials;
44}
45
46 exchangeserver. url = URL;
47
48 // define the inbox of the email
49 distinguishedfolderidtype [] folderidarray = new distinguishedfolderidtype [1];
50 folderidarray [0] = new distinguishedfolderidtype ();
51 folderidarray [0]. ID = distinguishedfolderidnametype. inbox;
52
53 finditemtype finditemrequest = new finditemtype ();
54 finditemrequest. traversal = itemquerytraversaltype. Shallow;
55 itemresponseshapetype itemproperties = new itemresponseshapetype ();
56 //
57 itemproperties. baseshape = defaultshapenamestype. idonly;
58 finditemrequest. itemshape = itemproperties;
59 finditemrequest. parentfolderids = folderidarray;
60 restrictiontype restriction = new restrictiontype ();
61 isequaltotype isto to = new isequaltotype ();
62 pathtounindexedfieldtype pathtofieldtype = new pathtounindexedfieldtype ();
63 pathtofieldtype. fielduri = unindexedfielduritype. messageisread;
64 fielduriorconstanttype constanttype = new fielduriorconstanttype ();
65 constantvaluetype = new constantvaluetype ();
66 constantvaluetype. value = "0 ";
67 constanttype. Item = constantvaluetype;
68 isequalto. Item = pathtofieldtype;
69 iseries to. fielduriorconstant = constanttype;
70 restriction. Item = isdue;
71 finditemrequest. Restriction = restriction;
72 // spsecurity. runwithelevatedprivileges (delegate ()
73 //{
74 // get the email
75 finditemresponsetype firt = exchangeserver. finditem (finditemrequest );
76
77 finditemresponsemessagetype folder = (finditemresponsemessagetype) firt. responsemessages. items [0];
78 arrayofrealitemstype foldercontents = new arrayofrealitemstype ();
79 foldercontents = (arrayofrealitemstype) folder. rootfolder. item;
80 itemtype [] items = foldercontents. items;
81 unreadcount = items = NULL? 0: items. length;
82 //});
83 return unreadcount;
84}
85 catch (exception ex)
86 {
87 return 0;
88}
89
90}

 

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.