Exchange Web Service (EWS) API 使用笔记
2013-08-05 14:32
555 查看
有时候我们需要使用程序来进行收发邮件, 传统的POP3/SMTP/IMAP等协议也正广泛的被使用着,有关这些协议的收发邮件代码网上一抓一大把~ 如果要用这些协议可以去别处看看.
我这里要谈的是使用Microsoft Exchange Sever来进行邮件的收发.
通过网上查找资料我找到了微软官方的Exchange
Server Developer Center, 从中得知了可以使用Microsoft Exchange Web Services Managed API来进行Exchange Server的交互. 你可以在这里下载到当前的最新1.1版本.
下载时有两个文件,根据你的实际情况选择即可:
Instructions
For x64 (64-bit) computers, download and run EwsManagedApi.msi
For x86 (32-bit) computers, download and run EwsManagedApi32.msi
安装之后的安装目录如图:
要使用EWS, 你需要将Microsoft.Exchange.WebServices.dll引用到你的项目之中.
一些基本的使用可以阅读GettingStarted.doc文件,里面说的比较清楚.
英文不好的同学可以查看这些篇文章
EWS
Managed API 介绍(上)
EWS
Managed API 介绍(下)
来自博客园的这两篇文章基本是对于官方GettingStarted.doc文件的翻译.
但是在我的应用中, 我需要读取所有在Inbox(收件箱)里的新邮件, 而已读邮件将被我忽略掉, 在读到新邮件后经过处理再将其标记为已读. 在示例代码中给出的读取Inbox中的邮件功能显然不能满足我的要求.
经过调研,写出如下代码已实现目标
[csharp]
view plaincopyprint?
//ExchangeService版本为2007SP1
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
//参数是用户名,密码,域
service.Credentials = new WebCredentials(Info.UserName, Info.Password, Info.Domain);
//给出Exchange Server的URL http://xxxxxxx
service.Url = new Uri(Info.SeverUrl);
//你自己的邮件地址 xxx@xxx.xxx
service.AutodiscoverUrl(Info.Email);
//创建过滤器, 条件为邮件未读.
SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead,
false);
//查找Inbox,加入过滤器条件,结果10条
FindItemsResults<item> findResults = service.FindItems(
WellKnownFolderName.Inbox,
sf,
new ItemView(10));
foreach (Item item
in findResults.Items)
{
EmailMessage email = EmailMessage.Bind(service, item.Id);
if (!email.IsRead)
{
Console.WriteLine(email.Body);
//标记为已读
email.IsRead = true;
//将对邮件的改动提交到服务器
email.Update(ConflictResolutionMode.AlwaysOverwrite);
}
}</item>
我这里要谈的是使用Microsoft Exchange Sever来进行邮件的收发.
通过网上查找资料我找到了微软官方的Exchange
Server Developer Center, 从中得知了可以使用Microsoft Exchange Web Services Managed API来进行Exchange Server的交互. 你可以在这里下载到当前的最新1.1版本.
下载时有两个文件,根据你的实际情况选择即可:
Instructions
For x64 (64-bit) computers, download and run EwsManagedApi.msi
For x86 (32-bit) computers, download and run EwsManagedApi32.msi
安装之后的安装目录如图:
要使用EWS, 你需要将Microsoft.Exchange.WebServices.dll引用到你的项目之中.
一些基本的使用可以阅读GettingStarted.doc文件,里面说的比较清楚.
英文不好的同学可以查看这些篇文章
EWS
Managed API 介绍(上)
EWS
Managed API 介绍(下)
来自博客园的这两篇文章基本是对于官方GettingStarted.doc文件的翻译.
但是在我的应用中, 我需要读取所有在Inbox(收件箱)里的新邮件, 而已读邮件将被我忽略掉, 在读到新邮件后经过处理再将其标记为已读. 在示例代码中给出的读取Inbox中的邮件功能显然不能满足我的要求.
经过调研,写出如下代码已实现目标
[csharp]
view plaincopyprint?
//ExchangeService版本为2007SP1
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
//参数是用户名,密码,域
service.Credentials = new WebCredentials(Info.UserName, Info.Password, Info.Domain);
//给出Exchange Server的URL http://xxxxxxx
service.Url = new Uri(Info.SeverUrl);
//你自己的邮件地址 xxx@xxx.xxx
service.AutodiscoverUrl(Info.Email);
//创建过滤器, 条件为邮件未读.
SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead,
false);
//查找Inbox,加入过滤器条件,结果10条
FindItemsResults<item> findResults = service.FindItems(
WellKnownFolderName.Inbox,
sf,
new ItemView(10));
foreach (Item item
in findResults.Items)
{
EmailMessage email = EmailMessage.Bind(service, item.Id);
if (!email.IsRead)
{
Console.WriteLine(email.Body);
//标记为已读
email.IsRead = true;
//将对邮件的改动提交到服务器
email.Update(ConflictResolutionMode.AlwaysOverwrite);
}
}</item>
相关文章推荐
- Exchange Web Service (EWS) API 使用笔记
- 使用 EWS(Exchange Web Service)协议读取邮件、发送邮件
- Java for Web学习笔记(七一):Service和Repository(6)在Spring框架中使用Listener
- C#调用ExchangeWebService发送邮件、查询日程、查询用户已使用邮箱空间
- [Exchange]使用EWS托管API2.0同步邮箱
- 财务与机票使用web service 交互笔记
- 如何使用Exchange Web Service获取日历(包含循环会议)
- 为什么 web 开发人员需要迁移到. NET Core, 并使用 ASP.NET Core MVC 构建 web 和 webservice/API
- 如何使用Exchange Web Service Managed API获取公共文件夹日历(包括循环会议)
- 读取Exchange邮件或任务(2)--通过Exchange Web Service Managed API
- 学习笔记:使用Web Service Software Factory开发简易留言本服务以及Mobile调用实现-1.创建Service
- 使用SQL SEVER的表值自定义函数与Exchange Web Service
- 使用WCF web API测试基于REST的WCF Service
- 如何在 Web 服务中使用 Service Data Objects 2.1 的动态数据 API
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)
- ios 使用webservice restful api使用ASIHttpRequest即可,restKit较大,可放后
- Symbian学习笔记(17) - 初探WebServices API的使用(上)
- 在 Web 应用程序中使用 SharePoint API 以避免页面验证错误
- python使用json web token (jwt)实现http api的加密传输
- ExchangeService(EWS)错误:The response received from the service didn't contain valid XML.