我有一个outlook插件,它运行在一堆文件夹中,将它们保存到磁盘并将它们移动到垃圾文件夹。
我添加的代码适用于99%的电子邮件。一堆try缓存用于调试,所以请忽略它们。
它每天提取几千封邮件,适用于所有内容,但一个文件夹中的邮件除外。
我检查项目是否是MailItems并且所有内容都检出,但是一旦我尝试获取它的属性,它就会给我这种类型的错误。
不支持此类接口(来自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"]);
}
}
}
}
答案 0 :(得分:0)
检查MailItem.Class属性的值 - 它可能是46(OlObjectClass.olReport)(并且MailItem.MessageClass将是REPORT.IPM.Note.NDR)。无法传递的报告消息是ReportItem对象,但它们可能被视为MailItem对象,从而通过您的评估。
另外请确保使用向后计数器循环,因为您可能通过将项目移动到另一个文件夹来修改集合(此外,每个循环都不适合与Outlook对象一起使用)。在包含Outlook对象的变量上调用Marshal.ReleaseCOMObject也是一个好主意。