3

我正在使用 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 告诉我附件没有文件名?

4

3 回答 3

1

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

foreach (Attachment att in item.Attachments)
{              
    if (att is FileAttachment fa)
    {
        // Do something with 'fa'
    }
}
于 2014-07-17T11:32:24.177 回答
1

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

于 2016-11-24T14:21:24.057 回答
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;
于 2014-05-31T01:32:04.700 回答