我正在为我的公司使用VS19在WinForms中创建一个“简单” VSTO(Outlook插件),它将扫描所有文件夹中的社会保险号,并在listView中列出电子邮件项目,以便用户可以删除邮件。
我正在使用EWS托管API客户端:https : //docs.microsoft.com/zh-cn/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications ?redirectedfrom = MSDN
我已经实现了上述罚款,但问题是我不知道如何正确访问Exchange Server。在链接中提供的示例中,用户名和密码是硬编码的,并且由于这是一个发布给多个不同用户的应用程序,因此我想使用Windows登录名(这是我公司的同一登录名)来访问交换服务器。
using Microsoft.Exchange.WebServices.Data;
// Setup how to download emails from the exchange server
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new WebCredentials("test.tester", "test123","domaintest");
service.AutodiscoverUrl("[email protected]");
// Add all the mails from exchange server (inbox) to a list
bool more = true;
ItemView view = new ItemView(int.MaxValue, 0, OffsetBasePoint.Beginning);
view.PropertySet = PropertySet.IdOnly;
FindItemsResults<Item> findResults;
List<EmailMessage> emails = new List<EmailMessage>();
while (more)
{
findResults = service.FindItems(WellKnownFolderName.Inbox, view);
foreach (var item in findResults.Items)
{
emails.Add((EmailMessage)item);
}
more = findResults.MoreAvailable;
if (more)
{
view.Offset += 1000;
}
}
当我将我的信息硬编码在上面的代码中时,一切正常,我可以访问收件箱文件夹并列出电子邮件列表
搜索了几个小时没有结果后,希望您能为我提供帮助。
如果您以本地Exchange服务器为目标,并且您的用户凭据已加入域:
service.UseDefaultCredentials = true;
假设本地凭据与邮箱关联,则以本地Exchange服务器为目标的加入域的客户端可以使用登录用户的默认凭据。在实例化ExchangeService对象之后,添加以下代码。
// Setup how to download emails from the exchange server
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.UseDefaultCredentials = true;
service.AutodiscoverUrl("[email protected]");
但是,如果您以Exchange Online或Office 365开发人员站点邮箱为目标,则必须传递显式凭据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句