无法获取有关 MailItem 的任何信息

Failing to get any info on MailItem

我有一个 outlook 插件,它运行一堆文件夹,将它们保存到磁盘并将它们移动到垃圾文件夹。

我添加的代码适用于 99% 的电子邮件。一堆try缓存是调试用的,请无视

它每天提取几千封邮件,适用于所有内容,但一个文件夹中的邮件除外。

我检查项目是否为 MailItems 并且所有内容都已检查完毕,但是当我尝试在其上获取 属性 时,它会给我这种类型的错误。

No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))

10:57:51 AM: at Microsoft.Office.Interop.Outlook._MailItem.get_ReceivedTime()

方法会根据我尝试访问的内容而变化。

一段时间以来一直在寻找解决方案,但无济于事。

请帮忙。

 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"]);
                        }
                    }
                }
            }

检查 MailItem.Class 属性 的值 - 它可能是 46 (OlObjectClass.olReport)(而 MailItem.MessageClass 将是 REPORT.IPM.Note.NDR)。无法投递的报告消息是 ReportItem 对象,但它们可能会被视为 MailItem 对象,从而通过您的评估。

还要确保您使用反向 for 计数器循环,因为您可能通过将项目移动到另一个文件夹来修改集合(加上 for each 循环不适合用于 Outlook 对象)。对保存 Outlook 对象的变量调用 Marshal.ReleaseCOMObject 也是一个好主意。