以下是使用EWS绑定和加载文件夹的代码示例:
using System;
using Microsoft.Exchange.WebServices.Data;
namespace EWSExample
{
class Program
{
static void Main(string[] args)
{
// 设置EWS连接
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Credentials = new WebCredentials("username", "password");
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
try
{
// 绑定邮箱根文件夹
Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
// 加载文件夹中的邮件
PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties);
propertySet.Add(FolderSchema.DisplayName);
propertySet.Add(FolderSchema.ItemCount);
inbox.Load(propertySet);
// 打印文件夹信息
Console.WriteLine("文件夹名称: " + inbox.DisplayName);
Console.WriteLine("邮件数量: " + inbox.ItemCount.ToString());
}
catch (Exception ex)
{
Console.WriteLine("发生错误:" + ex.Message);
}
Console.ReadLine();
}
}
}
请注意,上述代码示例假设您已经安装了Exchange Web Services API
并将其添加到项目中。在代码中,您需要替换username
和password
为您的实际邮箱用户名和密码。另外,请确保将https://outlook.office365.com/EWS/Exchange.asmx
替换为您实际的Exchange服务器URL。