none
Exception in accessing user properties of MaiLitem RRS feed

  • Question

  • Hi All,

    I have one requirement where I have to traverse mails in draft and then check for custom suer properties. I have written the code like below :--

                                    MAPIFolder draftFolder = currentStore.GetDefaultFolder(OlDefaultFolders.olFolderDrafts);
                                    Items draftItems = draftFolder.Items;
                                    for (int draftIndex = draftItems.Count; draftIndex >= 1; draftIndex--)
                                    {
                                      var item = draftItems[draftIndex];
                                      MailItem draftMail = null;
                                      draftMail = item as MailItem;
                                      if (draftMail != null && draftMail.UserProperties != null)
                                      {

                                        ......................

                                        if (draftMail != null)
                                          Marshal.ReleaseComObject(draftMail);

                                       }

                                    }

    My problem is that for some mails, it is throwing exception :--

    The operation failed.
       at Microsoft.Office.Interop.Outlook._MailItem.get_UserProperties()

    Now I am aware that if user had saved mail in draft from some other mail client, then UserProperties would be null. But I am not aware why they are throwing exception ? Apart from that. is there any way to avoid this exception ? I could put try catch around but it will slow down process dramatically.

    Thanks

    Dev

    Thursday, October 10, 2019 11:28 AM

All replies

  • Hello Dev,

    You need to check the UserProperties.Count property which returns an integer indicating the count of objects in the specified collection.

    MAPIFolder draftFolder = currentStore.GetDefaultFolder(OlDefaultFolders.olFolderDrafts);
    Items draftItems = draftFolder.Items;
    for (int draftIndex = draftItems.Count; draftIndex >= 1; draftIndex--)
    {
       var item = draftItems[draftIndex];
       MailItem draftMail = null;
       draftMail = item as MailItem;
       if (draftMail != null)
       {
          Outlook.UserProperties ups = draftMail.UserProperties;
          if (ups.Count > 0)
          { 
             .......
          }
                    
          if (draftMail != null) Marshal.ReleaseComObject(draftMail)
       }
    }


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, October 10, 2019 11:49 AM
  • HI Eugene,

    Thanks for answer. But my issue is following line is throwing exception :-- 

    draftMail.UserProperties

    So this code will also throw exception.

    Thanks

    Dev

    Thursday, October 10, 2019 4:37 PM
  • Dev,

    I would check if the source object is released correctly. Can it be that the email gets deleted/moved when you call this property?

    I would store the value retrieved via MailItem.EntryId and use NameSpace.GetItemFromId to re-create the COM object whenever required. In this case, GetItemFromId produces an exception if the item is moved/deleted. If you need to prevent moving/deleting the item, you can cancel the BeforeItemMove event.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, October 10, 2019 5:25 PM
  • HI Eugene,

    Thanks for suggestion. Let me try and get back to you.

    Thanks

    Dev

    Sunday, October 13, 2019 3:22 AM