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"]);
}
}
}
}
Check the value for the MailItem.Class property - it may be 46 (OlObjectClass.olReport) (and MailItem.MessageClass would be REPORT.IPM.Note.NDR). Undeliverable report messages are ReportItem objects but it is possible they could be seen as a MailItem object and thus pass your evaluation.
Also make sure you use a backwards for counter loop, as you may be modifying the collection by moving the item to another folder (plus for each loops are bad to use with Outlook objects). It's also always a good idea to call Marshal.ReleaseCOMObject on your variables holding Outlook objects.