C# Exchange web服务-获取附件始终为空
我有一个接受C# Exchange web服务-获取附件始终为空,c#,.net,attachment,exchangewebservices,C#,.net,Attachment,Exchangewebservices,我有一个接受ExchangeWebServices.MessageType参数的方法。我使用此变量从邮件中获取附件。当我获取附件的内容时,它总是空的。但文件名被正确读取。下面是我正在使用的代码: public void StripAttachments(MessageType fullMessage) { AttachmentType[] attachments = fullMessage.Attachments; if (attachments != null &&a
ExchangeWebServices.MessageType
参数的方法。我使用此变量从邮件中获取附件。当我获取附件的内容时,它总是空的。但文件名被正确读取。下面是我正在使用的代码:
public void StripAttachments(MessageType fullMessage)
{
AttachmentType[] attachments = fullMessage.Attachments;
if (attachments != null && attachments.Length > 0)
{
foreach (AttachmentType attachment in attachments)
{
if (attachment is FileAttachmentType)
{
FileAttachmentType file = (FileAttachmentType)attachment;
byte[] contents = file.Content; //Always null
try
{
if(contents != null)
{
System.IO.File.WriteAllBytes(@"C:\TestLocation" + file.Name, contents);
}
}
catch (Exception ex)
{
}
}
}
}
}
是否有更好的方法从特定邮件获取附件?尝试使用而不是MessageType
您需要使用EmailMessage.Bind(ExchangeService,ItemId)
填充附件列表并使用它们,否则将引发异常
public void StripAttachments(ItemId)
{
EmailMessage email=EmailMessage.Bind(服务,id)
foreach(电子邮件中的附件a。附件)
{
如果(是文件附件)
{
//做你的事
}
}
}
另外,请查看更多想法。我收到错误信息
“Microsoft.Exchange.WebServices.Data.EmailAddress”不包含“绑定”的定义facepalm正如您所看到的,我尝试使用EmailAddress
而不是EmailMessage
。它现在正在工作。知道为什么会出现这种情况吗?不完全确定,EmailMessage
自Exchange 2007 SP1以来就一直存在,而且它看起来可以用于更高版本。查看MessageType
时,我找不到任何有用的工具。