没有FileName的EWS FileAttachment

时间:2012-07-06 07:20:41

标签: c# exchangewebservices

我正在使用EWS接收邮件并将其导入我们的CRM系统。在99%的邮件中,一切正常。但不,我有问题,有些文件附件不告诉我文件名。

这是我的代码示例。

Item item = Item.Bind("id"); //id should be replaced by a leagle id

PropertySet ps = PropertyHelper.GetFullLoadItemPropertySet(m_Item.GetType()); 
//the propertyset is a manually created set with all relevant properties.

item.Load(ps)M

foreach (Attachment att in item.Attachments)
{                                    {
    FileAttachment fa = att as FileAttachment;
    if (fa != null)
    {
        fa.Load();
        if (string.IsNullOrWhiteSpace(fa.FileName))
        {
            System.Diagnostics.Debugger.Break();
        }
    }
}

如果我使用一点vba代码查看同一邮件,附件会有一个文件名,显示在outlook中。

Dim mail As MailItem

Set mail = Application.ActiveExplorer().Selection.Item(1)

debug.Print mail.Attachments.Item(0).FileName

有没有人知道为什么outlook会获得正确的文件名,但是EWS告诉我附件没有文件名?

3 个答案:

答案 0 :(得分:1)

并非您从EWS获得的所有Attachment项都是实际的FileAttachment项。 添加附件类型的检查。

foreach (Attachment att in item.Attachments)
{              
    if (att is FileAttachment)
    {
        FileAttachment fa = att as FileAttachment;
        // Do something with it
    }
}

答案 1 :(得分:0)

我在类似系统中使用了emailMessage架构并且没有注意到这个问题,可能以下内容会有所帮助:

//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(common.strInboxURL);

//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);

//this will bind the mailbox you're looking for using your service instance
Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, fid);

SearchFilter.SearchFilterCollection searchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And);
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));


// exclude any silly returned emails
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "Undeliverable")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "Out of Office")));

ItemView view = new ItemView(100);
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);

// This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(fid, searchFilterCollection, view);

if (results.Count() > 0)
   {

   // set the prioperties we need for the entire result set
   view.PropertySet = new PropertySet(
   BasePropertySet.IdOnly,
   ItemSchema.Subject,
   ItemSchema.DateTimeReceived,
   ItemSchema.DisplayTo, EmailMessageSchema.ToRecipients,
   EmailMessageSchema.From, EmailMessageSchema.IsRead,
   EmailMessageSchema.HasAttachments, ItemSchema.MimeContent,
   EmailMessageSchema.Body, EmailMessageSchema.Sender,
   ItemSchema.Body) { RequestedBodyType = BodyType.Text };

   // load the properties for the entire batch
   service.LoadPropertiesForItems(results, view.PropertySet);

    forech (Microsoft.Exchange.WebServices.Data.Attachment attachment in email.Attachments)
        {

        if (attachment is FileAttachment)
            {
            FileAttachment fileAttachment = attachment as FileAttachment;

答案 2 :(得分:0)

浏览EWS源显示只有在使用.Load方法将附件保存到本地磁盘时才会填充FileAttachment.FileName属性。