First add a reference to Microsoft.Office.Interop.Outlook, then add these using statements
using Microsoft.Office.Interop.Outlook;
using _Application = Microsoft.Office.Interop.Outlook._Application;
using Outlook = Microsoft.Office.Interop.Outlook;
Then add this
//declare Outlook application
_Application objOutlook = new Outlook.Application(); //create it
_NameSpace objNs = objOutlook.Session; //create new session
MAPIFolder oPublicFolders = objNs.Folders["Public Folders"];
MAPIFolder oAllPublicFolders = oPublicFolders.Folders["All Public Folders"];
Items itms = oAllPublicFolders.Items;
foreach (MailItem item in itms)
{
GetNewMailItem(objOutlook, objContacts, item);
}
Marshal.ReleaseComObject(objOutlook); //release outlook com object
This next piece is just what I did, please change all of this code to fit your needs.
private static void GetNewMailItem(_Application objOutlook, MAPIFolder objContacts, MailItem item)
{
if(item.ReceivedTime.Date == DateTime.Now.Date.AddDays(-1) || item.UnRead)
{
if (item.Attachments.Count > 0)
{
var attachments = item.Attachments;
foreach (Attachment attachment in attachments)
{
if(attachment.Type == OlAttachmentType.olEmbeddeditem)
{
ProcessEmbeddedEmailAttachment(attachment, objOutlook, objContacts);
}
else if (attachment.FileName.EndsWith(".doc") || attachment.FileName.EndsWith(".docx"))
{
ExtractAttachment(attachment);
item.UnRead = false;
}
}
}
}
}
I am leaving out the rest of the code because it was specific to my processing.