2014-03-06 49 views
1

我正在使用Exchange Web服务托管API。我正在添加一个扩展属性,以便在收件箱中邮寄邮件时,它们会根据某些条件进行处理。因此,并非所有的邮件都会得到这个扩展属性。无法获取电子邮件的内置属性以及扩展属性

接下来,我正在重新提取收件箱中的所有邮件,如果他们将此属性附加到他们,我再处理它们。

下面是一个简单的方法getAllMailsInInbox(),我曾写信给重新读取该邮件在收件箱:

class MyClass 
{ 
    private static Guid isProcessedPropertySetId; 
    private static ExtendedPropertyDefinition isProcessedPropertyDefinition = null; 

    static MyClass() 
    { 
     isProcessedPropertySetId = new Guid("{20F3C09F-7CAD-44c6-BDBF-8FCB324244}"); 
     isProcessedPropertyDefinition = new ExtendedPropertyDefinition(isProcessedPropertySetId, "IsItemProcessed", MapiPropertyType.String); 
    } 

    public List<EmailMessage> getAllMailsInInbox() 
    { 
     List<EmailMessage> emails = new List<EmailMessage>(); 
     ItemView itemView = new ItemView(100, 0); 
     FindItemsResults<Item> itemResults = null; 

     PropertySet psPropSet = new PropertySet(BasePropertySet.IdOnly); 
     itemView.PropertySet = psPropSet; 

     PropertySet itItemPropSet = new PropertySet(BasePropertySet.IdOnly,            
              ItemSchema.Attachments, 
              ItemSchema.Subject,            
              ItemSchema.Importance, 
              ItemSchema.DateTimeReceived, 
              ItemSchema.DateTimeSent, 
              ItemSchema.ItemClass, 
              ItemSchema.Size, 
              ItemSchema.Sensitivity, 
              EmailMessageSchema.From, 
              EmailMessageSchema.CcRecipients, 
              EmailMessageSchema.ToRecipients,  
              EmailMessageSchema.InternetMessageId,                
              ItemSchema.MimeContent, 
              isProcessedPropertyDefinition); //*** 

     itemResults = service.FindItems(WellKnownFolderName.Inbox, itemView); 
     service.LoadPropertiesForItems(itemResults.Items, itItemPropSet); 

     String subject = itItem.Subject; //Exception: "You must load or assign this property before you can read its value." 
     //.... 
    } 
} 

正如你所看到的,随叫随到service.LoadPropertiesForItems(),这不加载任何特性,从而导致You must load or assign this property before you can read its value.异常而访问任何这些属性。

如果我从itItemPropSet属性集中删除isProcessedPropertyDefinition,它会正确提取所有属性。

所以我可以只知道如何获取所有内置EmailMessage属性以及扩展属性?

回答

2

您的GUID在最后一个破折号之后的两位数字太短。奇怪你没有看到FormatException。不过,您应该更新代码以检查每个项目的GetItemResponse。这样,如果一个项目发生某种错误,您的代码可以知道它。这意味着你需要创建另一个集合才能返回。

更新你的代码是:

ServiceResponseCollection<ServiceResponse> responses = service.LoadPropertiesForItems(itemResults.Items, itItemPropSet); 

foreach (ServiceResponse response in responses) 
{ 
     if (response.Result == ServiceResult.Error) 
     { 
      // Handle the error associated 
     } 

     else 
     { 
      String subject = (response as GetItemResponse).Item.Subject; 
     } 
} 
+0

对不起,我在编写问题时只是操纵了GUID。别管它。 – Mahesha999

0

而不是做 service.LoadPropertiesForItems的(itemResults.Items,itItemPropSet);

尝试做

itemResult.LoadPropertiesForItems(itItemPropSet);

Casue一旦你有了物品,你可以通过加载特定物品来加载物品的扩展属性。