2012-07-06 73 views
3

我使用EWS接收郵件並將它們導入到我們的CRM系統。在99%的郵件中,一切正常。但沒有我有這個問題,有些fileattachments不告訴我的文件名。EWS FileAttachment without FileName

這是我的代碼的一個例子。

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告訴我,附件沒有文件名?

+0

EWS是否通過['Attachment.Name']包含正確的'DisplayName'(http://msdn.microsoft.com/zh-cn/library/microsoft.exchange.webservices.data.attachment.name%28v = exchg.80%29.aspx)屬性? – SliverNinja 2012-07-09 16:30:17

+0

是的。在當前的案例中,Attachment.Name顯示「Service Bericht」,這正是Outlook在DisplayName屬性中顯示的內容。但是我無法訪問任何顯示Outlook中FileName屬性值的值。我應該說,這些郵件是由服務創建的,並不是在Outlook中手動創建的。 – 2012-07-10 06:00:44

+0

Outlook本身不使用EWS與Exchange Server交談,而是使用MAPI。有時候這會導致很小的差異(像這樣)。 – Halvard 2013-08-14 08:47:15

回答

1

並非所有的Attachment項目從EWS得到的是實際的FileAttachment項目。 添加對附件類型的檢查。

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

OP已經在使用這個檢查,在這裏:'if(fa!= null)' – VMAtm 2014-07-17 11:56:52

+0

'FileAttachment'繼承自'Attachment',所以任何'Attachment'都可以轉換爲'FileAttachment'。這並不意味着原始的對象類型是'FileAttachment'。其轉換後的值不會爲空,這使得'if(fa!= null)'無意義。 – dePatinkin 2014-07-17 13:33:01

+2

>> FileAttachment繼承自附件,因此任何附件都可以轉換爲FileAttachment。 - 這不是真的。只有反之亦然 - 任何'FileAttachment'都可以轉換爲'Attachment'。所以,他們使用這個語句:'att as FileAttachment',如果'att'不是'FileAttachement,結果將是null。 – VMAtm 2014-07-17 13:42:03

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; 
0

瀏覽EWS來源表明,當附件被保存到本地磁盤與.Load方法FileAttachment.FileName屬性僅填充。

相關問題