首先添加对 Microsoft.Office.Interop.Outlook 的引用,然后添加这些 using 语句
using Microsoft.Office.Interop.Outlook;
using _Application = Microsoft.Office.Interop.Outlook._Application;
using Outlook = Microsoft.Office.Interop.Outlook;
然后添加这个
//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
下一部分正是我所做的,请更改所有这些代码以满足您的需求。
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;
}
}
}
}
}
我省略了其余的代码,因为它是特定于我的处理的。