none
Failing to get any info on MailItem RRS feed

  • Question

  • I have an outlook addin that runs through a bunch of folders, saves them to disk and moves them to the trash folder.

    The code I added is working for 99% of the emails. The bunch of try caches is for debugging, so please ignore them.

    It extracts a couple of thousand mails a day and works for everything, except the mails in one folder.

    I check if the items is MailItems and everything checks out, but as soon as I try to get a property on it it gives me this type of error.

    > No such interface supported (Exception from HRESULT: 0x80004002
    > (E_NOINTERFACE))
    >
    > 10:57:51 AM:                         at
    > Microsoft.Office.Interop.Outlook._MailItem.get_ReceivedTime()

    The method changes based on what I'm trying to access.

    Have been looking for solutions for this for a while, but to no avail.

    Please help.

         while (unreadFolders.Count > 0 && count < COUNT)
                    {
                        Outlook.Folder currentFolder = unreadFolders.FirstOrDefault().Key;
                        string path = unreadFolders.FirstOrDefault().Value;
                        Debug.WriteLine("reading folder: " + currentFolder.Name);
                        unreadFolders.Remove(currentFolder);
    
                        Outlook.Folder parent = GetParent(currentFolder);
                        var t = parent?.FullFolderPath;
                        //replenish the list
                        foreach (Outlook.Folder f in currentFolder.Folders) unreadFolders.Add(f, path + "\\" + f.Name);
    
                        //create directory if it doesnt exist
                        Directory.CreateDirectory(path);
    
                        Outlook.Items items = currentFolder.Items;
                        foreach (var item in items)
                        {
                            if (item != null && item is Outlook.MailItem)
                            {
                                if (count++ > COUNT) break;
                                var mailItem = (Outlook.MailItem)item;
                                if (mailItem == null) continue;
                                var fullpath = path + "\\";
                                try
                                {
                                    fullpath += "[(R)" + mailItem.ReceivedTime.ToWeirdDateFormat() + "]";
                                }
                                catch (Exception ex)
                                {
                                    using (StreamWriter file = new StreamWriter(@"C:\\temp\logs.txt", true))
                                    {
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\tReceived Time Broken");
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\t" + ex.Message);
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\t" + ex.StackTrace);
                                    }
                                }
                                try
                                {
                                    fullpath += "[(T)" + mailItem.To.MakeWindowsSafe() + "]";
                                }
                                catch (Exception ex)
                                {
                                    using (StreamWriter file = new StreamWriter(@"C:\\temp\logs.txt", true))
                                    {
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\tTo Broken");
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\t" + ex.Message);
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\t" + ex.StackTrace);
                                    }
                                }
                                try
                                {
                                    fullpath += "[(F)" + mailItem.SenderName.MakeWindowsSafe() + "]";
                                }
                                catch (Exception ex)
                                {
                                    using (StreamWriter file = new StreamWriter(@"C:\\temp\logs.txt", true))
                                    {
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\tSender name Broken");
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\t" + ex.Message);
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\t" + ex.StackTrace);
                                    }
                                }
                                try
                                {
                                    fullpath += "[+][(S)" + mailItem.Subject.MakeWindowsSafe() + "]";
                                }
                                catch (Exception ex)
                                {
                                    using (StreamWriter file = new StreamWriter(@"C:\\temp\logs.txt", true))
                                    {
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\tSubject Broken");
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\t" + ex.Message);
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\t" + ex.StackTrace);
                                    }
                                }
                                fullpath += ".msg";
                                //save message to directory
                                mailItem.SaveAs(fullpath, Outlook.OlSaveAsType.olMSG);
    
                                //move message to deleted
                                if (parent == null)
                                {
                                    using (StreamWriter file = new StreamWriter(@"C:\\temp\logs.txt", true))
                                    {
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\tParent Null");
                                        file.WriteLine(DateTime.Now.ToLongTimeString() + ":\t\t" + currentFolder.FullFolderPath);
                                    }
                                }
                                else
                                {
                                    mailItem.Move(parent.Folders["Deleted Items"]);
                                }
                            }
                        }
                    }
    Monday, March 6, 2017 12:07 PM

Answers

  • Hello,

    I'd suggest starting from releasing underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object.  Read more about that in the Systematically Releasing Objects article.

    Don't use the foreach loop for COM objects. Use the for loop instead. Thus, you will be able to release underlying COM objects.

    Also don't keep Outlook COM objects in the collections. Note, you can use the FolderPath to get the Folder instance. The EntryID property value can be used for getting an Outlook item instance.


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

    • Marked as answer by LeftOfHere Wednesday, March 8, 2017 10:19 AM
    Monday, March 6, 2017 12:42 PM

All replies

  • Hello,

    I'd suggest starting from releasing underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object.  Read more about that in the Systematically Releasing Objects article.

    Don't use the foreach loop for COM objects. Use the for loop instead. Thus, you will be able to release underlying COM objects.

    Also don't keep Outlook COM objects in the collections. Note, you can use the FolderPath to get the Folder instance. The EntryID property value can be used for getting an Outlook item instance.


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

    • Marked as answer by LeftOfHere Wednesday, March 8, 2017 10:19 AM
    Monday, March 6, 2017 12:42 PM
  • I don't understand why this worked, but it did and the code is cleaner now, so thats probably a good thing.

    Wednesday, March 8, 2017 10:20 AM