sharepoint讀取未讀郵件數

來源:互聯網
上載者:User

首先添加owa的web servers,地址一般為:https://mail.***.com/EWS/Services.wsdl

其次,在owa上需要建立一個帳戶具有類比其它帳號讀取郵件的許可權,

開啟Exchange 2010 伺服器在命令列中輸入

  New-ManagementRoleAssignment -Name:impersonationAssignmentName -Role:ApplicationImpersonation -User:serviceAccount

 2007裡面需要將owa設定成window登入,同時在mail伺服器上iis中虛擬目錄ews的目錄安全性裡面取消啟用匿名登入就好了

 

serviceAccount:就是我們要類比其他人的賬戶 

具體代碼:

 

 

 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                 ///綁定exchange伺服器 這裡你需要添加web引用,https://mail.***.cn/EWS/Exchange.asmx 
16                 ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
17                 ///下面這句可以解決:"基礎串連已經關閉: 未能為 SSL/TLS 安全通道建立信任關係"
18                 System.Net.ServicePointManager.ServerCertificateValidationCallback =
19                 delegate(Object obj, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors errors)
20                 {
21                     return true;
22                 };
23                 // 建立信任連接
24                 //exchangeServer.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
25                 //exchangeServer.Credentials = System.Net.CredentialCache.DefaultCredentials;
26                 //exchangeServer.UseDefaultCredentials = 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 exExchangeImpersonation = new ExchangeImpersonationType();
36                     ConnectingSIDType csConnectingSid = new ConnectingSIDType();
37                     csConnectingSid.PrimarySmtpAddress = userEmail;
38                     exExchangeImpersonation.ConnectingSID = csConnectingSid;
39                     exchangeServer.ExchangeImpersonation = exExchangeImpersonation;
40                 }
41                 else
42                 {
43                     exchangeServer.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
44                 }
45   
46                 exchangeServer.Url = url;
47 
48                 // 定義郵件的收件匣
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 isEqualTo = new IsEqualToType();
62                 PathToUnindexedFieldType pathToFieldType = new PathToUnindexedFieldType();
63                 pathToFieldType.FieldURI = UnindexedFieldURIType.messageIsRead;
64                 FieldURIOrConstantType constantType = new FieldURIOrConstantType();
65                 ConstantValueType constantValueType = new ConstantValueType();
66                 constantValueType.Value = "0";
67                 constantType.Item = constantValueType;
68                 isEqualTo.Item = pathToFieldType;
69                 isEqualTo.FieldURIOrConstant = constantType;
70                 restriction.Item = isEqualTo;
71                 findItemRequest.Restriction = restriction;
72                 //SPSecurity.RunWithElevatedPrivileges(delegate()
73                 //{
74                 // 擷取郵件
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         }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.