[英]Folder.Bind Throws ArgumentNullException
我正在尝试创建一个电子邮件客户端,该客户端可以使用EWS托管API从特定日期向我提供电子邮件列表,但是当我调用Folder.Bind()时,总是收到异常消息。
引发的异常:mscorlib.dll中的'System.ArgumentNullException'其他信息:值不能为null。
它说这是ArgumentNullException,但一个参数是枚举,而另一个参数在运行时不为null。 从堆栈上看,这似乎是EWS API中的一个异常,处理不好,但我不知道问题出在哪里。
这是我的代码。
using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Integration
{
public class ExchangeEmailClient : EmailClientBase
{
public ExchangeEmailClient(string emailAddress, string password)
{
ExchangeService = new ExchangeService();
ExchangeService.Credentials = new WebCredentials(emailAddress, password);
ExchangeService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
}
public ExchangeService ExchangeService { get; private set; }
public override IEnumerable<Email> Receive(DateTime getFrom)
{
//Get emails
var inbox = Folder.Bind(ExchangeService, WellKnownFolderName.Inbox); //Exception thrown here!
var filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, getFrom));
var emails = inbox.FindItems(filter, new ItemView(int.MaxValue));
//Transform (use data transformation tool?)
foreach (EmailMessage exchangeEmail in emails)
{
var standardEmail = new Email();
standardEmail.Body = exchangeEmail.Body;
standardEmail.Subject = exchangeEmail.Subject;
standardEmail.From = exchangeEmail.From.Address;
standardEmail.Attachments = exchangeEmail.Attachments.Select(x => new Uri(x.ContentLocation)).ToList();
standardEmail.CarbonCopy = exchangeEmail.CcRecipients.Select(x => x.Address).ToList();
yield return standardEmail;
}
}
public override void Send(Email email)
{
var outgoingMail = new EmailMessage(ExchangeService);
outgoingMail.Body = email.Body;
outgoingMail.Subject = email.Subject;
outgoingMail.From = new EmailAddress(email.From);
//Get attachments
email.Attachments.ForEach(x => outgoingMail.Attachments.AddFileAttachment(x.ToString()));
//Set addresses
email.To.ForEach(x => outgoingMail.ToRecipients.Add(new EmailAddress(x)));
email.CarbonCopy.ForEach(x => outgoingMail.CcRecipients.Add(new EmailAddress(x)));
email.BlindCarbonCopy.ForEach(x => outgoingMail.BccRecipients.Add(new EmailAddress(x)));
//Send
outgoingMail.SendAndSaveCopy();
}
}
}
获取像这样的项目会引发相同的异常:
//Get emails
var filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, getFrom));
var emails = ExchangeService.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(int.MaxValue));
在EWS API SDK的2.2.0和1.2.0版本中都会发生这种情况。
此异常在EWS库中处理,并且我刚刚关闭了“仅启用我的代码”。 该代码将正常执行并继续。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.