尝试声明新的共享电子邮件时出现错误...
说明:
我正在开发Windows服务,以使用Exchange EWS c#获取共享的电子邮件,并将其内容保存到数据库以及附件和收据等。
如下代码
public void GetSPCallCenterEmails()
{
try
{
ExchangeService service = new ExchangeService();
EmailApprovalRequestBLL EmailApprovalRequest = new EmailApprovalRequestBLL();
string SPUserName ="UserName";
string SPPassword ="Password"
string SPDomain ="LocalDomain";
int EmailsToGetCount =50;
string SPEmail ="sharedemail@companyLocalDomain.com";
string RequestAttachPath ="Path";
service.Credentials = new WebCredentials(SPUserName, SPPassword, SPDomain);
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
service.Url = new Uri("https://CASURL/EWS/Exchange.asmx");
service.UseDefaultCredentials = false;
EmailApprovalRequest.GetNewEmails(service, SPEmail, RequestAttachPath, EmailsToGetCount);
}
catch (Exception ex)
{
ExceptionHandlerConstants.CreateException(ex, "Service1.cs", "GetSPCallCenterEmails", "Front Service");
}
}
================================================ ================================
public bool GetNewEmails(ExchangeService service, string EmailToGetMails, string SaveAttachmentPath, int EmailsToGetCount)
{
try
{
List<EmailMessage> UnReadMessages = new List<EmailMessage>();
var IsNewEmails = EmailingService.GetSpecificSharedMailMessages(EmailToGetMails, service, EmailsToGetCount, out UnReadMessages);
if (IsNewEmails)
{
//Handle New Emails
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
ExceptionHandlerConstants.CreateException(ex, "EmailApprovalRequestBLL", "GetNewEmails", "GetNewEmails_BLL");
return false;
}
}
================================================ ================================
public static bool GetSpecificSharedMailMessages(string EmailAddress,ExchangeService service, int EmailsToGetCount,out List<EmailMessage> NewMessages)
{
try
{
FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox, EmailAddress);
ItemView itemView = new ItemView(EmailsToGetCount);
var Emails = service.FindItems(SharedMailbox, itemView);
List<EmailMessage> FinalEmailsList = new List<EmailMessage>();
foreach (EmailMessage msg in Emails)
{
//Retrieve Additional data for Email
EmailMessage message = EmailMessage.Bind(service,(EmailMessage.Bind(service, msg.Id)).Id,new PropertySet(BasePropertySet.FirstClassProperties,new ExtendedPropertyDefinition(0x1013, MapiPropertyType.Binary)));
FinalEmailsList.Add(message);
}
//Get UnRead Messages Only
FinalEmailsList = FinalEmailsList.ToList();
if (FinalEmailsList.Count>0)
{
NewMessages = FinalEmailsList;
return true;
}
else
{
NewMessages = null;
return false;
}
}
catch (Exception ex)
{
ExceptionHandlerConstants.CreateException(ex, "EmailingService", "GetSpecificSharedMailMessages", "GetSpecificSharedMailMessages_EmailingService");
NewMessages = null;
return false;
}
}
================================================ ==
错误是:异常:--------- System.ArgumentNullException:值不能为null。参数名称:Microsoft.Exchange.WebServices.Data.MapiTypeConverter处System.Convert.FromBase64String(String s)处的<.cctor> b__14(String s)
注意:此行生成错误“ FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox,EmailAddress);”
****注2:此服务在Windows Server 2012 R2上生成此错误,并且在Windows 10 ****上不会生成此问题