2017-03-06 78 views
0

我有一个outlook插件,它贯穿一堆文件夹,将它们保存到磁盘并将它们移动到垃圾文件夹。无法获取MailItem上的任何信息

我添加的代码适用于99%的电子邮件。大量的尝试缓存是用于调试的,所以请忽略它们。

它每天提取几千封邮件,并且适用于所有内容,除了邮件在一个文件夹中。

我检查项目是否是MailItems,并且所有项都检出,但只要我尝试获取属性,它就会给我这种类型的错误。

支持(从HRESULT异常:0x80004002 (E_NOINTERFACE))没有这样的接口

上午10时57分51秒:在 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"]); 
         } 
        } 
       } 
      } 

回答

0

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

此外,请确保您使用向后计数器循环,因为您可能正在通过将项目移动到另一个文件夹(每个循环都不好用于Outlook对象)来修改该集合。调用持有Outlook对象的变量的Marshal.ReleaseCOMObject也是一个好主意。

+0

明天我只能访问邮箱来检查这个,但是当我有权访问时,我确实检查了这些消息,他们认为我是正常消息。 MailItem.Class属性值应该是什么? 我也更改了代码以使用for循环并现在使用它们上的ReleaseComObject,但只能明天确认。 – LeftOfHere

+0

43.谢谢msdn文档。 – LeftOfHere

+0

帮助使用for循环代替foreach循环。 也必须正确释放COM对象。 [msdn question](https://social.msdn.microsoft.com/Forums/vstudio/en-US/e460d068-f872-44cd-a032-8f74e92d68f8/failing-to-get-any-info-on-mailitem?论坛= outlookdev) – LeftOfHere