Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Exchange web服务-获取附件始终为空_C#_.net_Attachment_Exchangewebservices - Fatal编程技术网

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
时,我找不到任何有用的工具。