EWS编程问题二:如何利用EWS读取Exchange里面相应用户的未读邮件数量(解决)...

本文介绍了一种方法,通过Exchange Web Service API读取指定域用户的Exchange邮箱中未读邮件的数量。文中提供了C#实现的具体代码示例,包括如何设置连接、认证及获取特定文件夹(如收件箱)中的未读邮件计数。

        最近几个项目都碰到如何读取Exchange中,登录域用户的未读邮件数量问题,查了一些文章这个问题终于解决掉。现在将源代码放在这里。首先新建一个项目,控制台应用程序,WinForm或者WebPart,网站都可以。然后我们添加Exchange的WebService引用,这里可以完成很多对Exchange邮箱的操作,可以分别读取相应文件夹里面的东西,甚至是邮件的内容等等,当然,这肯定是需要用户的用户名和密码才能完成,不然这个Exchange就不安全了,O(∩_∩)O~

        在添加Exchange的WebService时有个需要注意的问题,不然你是不能找到ExchangeServiceBinding这个类的。请参考这篇文章。剩下的代码如下:

        调用部分的代码:

 

ExpandedBlockStart.gif 代码
ServicePointManager.ServerCertificateValidationCallback  =   delegate (Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) {  return   true ; }; 

ExchangeServiceBinding service 
=   new  ExchangeServiceBinding ();

service.RequestServerVersionValue 
=   new  RequestServerVersion(); 
service.RequestServerVersionValue.Version 
=  ExchangeVersionType.Exchange2010; 
service.EnableDecompression 
=   true
service.Credentials 
=   new  NetworkCredential( " username " " password " " domain " ); 
service.Url 
=  @https: // 192.168.100.11/EWS/exchange.asmx;

int  merCurrentCount  =  GetUnreadFolderItemsCount(service,  " Inbox " );

 

 

       涉及到的两个方法:

 

ExpandedBlockStart.gif 代码
public   int  GetUnreadFolderItemsCount(ExchangeServiceBinding service,  string  folderName) 
       { 
           
int  unReadCount  =   - 1
           
//  Identify the folder properties to return. 
           FolderResponseShapeType properties  =   new  FolderResponseShapeType(); 
           PathToUnindexedFieldType ptuft 
=   new  PathToUnindexedFieldType(); 
           ptuft.FieldURI 
=  UnindexedFieldURIType.folderManagedFolderInformation; 
           PathToUnindexedFieldType[] ptufts 
=   new  PathToUnindexedFieldType[ 1 ] { ptuft }; 
           properties.AdditionalProperties 
=  ptufts; 
           properties.BaseShape 
=  DefaultShapeNamesType.AllProperties; 

           
//  Form the get folder request. 
           BaseFolderIdType p_folder  =  FindFolderID(service, folderName); 

           GetFolderType request 
=   new  GetFolderType(); 
           request.FolderIds 
=   new  BaseFolderIdType[ 1 ] { p_folder }; 
           request.FolderShape 
=  properties; 

           
//  Send the request and get the response. 
           GetFolderResponseType response  =  service.GetFolder(request); 

           ArrayOfResponseMessagesType aormt 
=  response.ResponseMessages; 
           ResponseMessageType[] rmta 
=  aormt.Items; 
           
foreach  (ResponseMessageType rmt  in  rmta) 
           { 
               
if  (rmt.ResponseClass  ==  ResponseClassType.Error) 
               { 
                   
throw   new  Exception(rmt.MessageText); 
               } 
               
else  
               { 
                   FolderInfoResponseMessageType firmt; 
                   firmt 
=  (rmt  as  FolderInfoResponseMessageType); 
                   BaseFolderType[] folders 
=  firmt.Folders; 

                   
foreach  (BaseFolderType rfolder  in  folders) 
                   { 
                       
if  (rfolder  is  FolderType) 
                       { 
                           FolderType myFolder; 
                           myFolder 
=  (rfolder  as  FolderType); 
                           
if  (myFolder.UnreadCountSpecified) 
                           { 
                               unReadCount 
=  myFolder.UnreadCount; 
                           } 
                       } 
                   } 
               } 
           } 
           
return  unReadCount; 
       } 

       
public   static  FolderIdType FindFolderID(ExchangeServiceBinding service, String folderName) 
       { 
           DistinguishedFolderIdType objSearchRootFolder 
=   new  DistinguishedFolderIdType(); 
           objSearchRootFolder.Id 
=  DistinguishedFolderIdNameType.msgfolderroot; 

           FindFolderType requestFindFolder 
=   new  FindFolderType(); 
           requestFindFolder.Traversal 
=  FolderQueryTraversalType.Deep; 
           requestFindFolder.ParentFolderIds 
=   new  DistinguishedFolderIdType[] { objSearchRootFolder }; 
           requestFindFolder.FolderShape 
=   new  FolderResponseShapeType(); 
           requestFindFolder.FolderShape.BaseShape 
=  DefaultShapeNamesType.IdOnly; 

           
// Search filter definition 
           requestFindFolder.Restriction  =   new  RestrictionType(); 

           
#region  Contains expression 

           ContainsExpressionType objContainsExpression 
=   new  ContainsExpressionType(); 
           objContainsExpression.ContainmentMode 
=  ContainmentModeType.FullString; 
           objContainsExpression.ContainmentModeSpecified 
=   true
           objContainsExpression.ContainmentComparison 
=  ContainmentComparisonType.Exact; 
           objContainsExpression.ContainmentComparisonSpecified 
=   true

           PathToUnindexedFieldType objFieldFolderName 
=   new  PathToUnindexedFieldType(); 
           objFieldFolderName.FieldURI 
=  UnindexedFieldURIType.folderDisplayName; 
           objContainsExpression.Item 
=  objFieldFolderName; 

           objContainsExpression.Constant 
=   new  ConstantValueType(); 
           objContainsExpression.Constant.Value 
=  folderName; 

           
#endregion  Contains expression 

           requestFindFolder.Restriction.Item 
=  objContainsExpression; 

           FindFolderResponseType objFindFolderResponse 
=  
               service.FindFolder(requestFindFolder); 

           
if  (objFindFolderResponse.ResponseMessages.Items.Length  ==   0
               
return   null

           
foreach  (ResponseMessageType responseMsg  in  
               objFindFolderResponse.ResponseMessages.Items) 
           { 
               
if  (responseMsg.ResponseClass  ==  ResponseClassType.Success) 
               { 
                   FindFolderResponseMessageType objFindResponse 
=  
                       responseMsg 
as  FindFolderResponseMessageType; 
                   
foreach  ( 
                       BaseFolderType objFolderType 
in  objFindResponse.RootFolder.Folders) 
                   { 
                       
return  objFolderType.FolderId; 
                   } 
               } 
           } 
           
return   null
       }

 

 

 

       参考文章:Get the Unread count of the Inbox of Exchange 2010/2007, using web services

当你尝试使用JavaMail API读取回执邮件时,如果遇到`javax.mail.NoSuchProviderException: invalid provider`错误,这通常意味着JavaMail找不到支持特定邮箱服务提供商的协议适配器。JavaMail库需要适配器来连接到各种电子邮件服务器,比如POP3、IMAP4、SMTP等。 解决这个问题的步骤通常是: 1. **检查配置**:确保你在创建`Session`对象时正确地指定了邮件服务器的提供者。例如,如果你的邮件账户是通过Gmail的IMAP,你应该使用`imap.googlemail.com`作为主机名,并指定合适的`Authenticator`。 ```java Properties props = System.getProperties(); props.put("mail.imap.host", "imap.gmail.com"); props.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.imap.socketFactory.fallback", false); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your_username", "your_password"); } }); ``` 2. **添加缺失的provider**:检查是否有相应的JavaMail provider JAR文件已包含在项目中。对于一些第三方邮件服务,如Office 365,你可能需要下载并添加额外的库,如Exchange Web Services (EWS)。 3. **更新依赖**:确认使用的JavaMail版本是否兼容你的应用程序需求,有时候旧版的JavaMail可能缺少对某些新邮件服务的支持。 4. **异常处理**:确保在读取邮件时捕获此类异常并给出友好的错误提示,以帮助调试。 如果你按照上述步骤仍然无法解决问题,可以考虑提供具体的错误日志或更多上下文信息以便进一步分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值