我正在开发 Outlook 2007 加载项。我找到了一些代码来循环所有文件夹,但我无法弄清楚如何循环inside任何给定的文件夹来检查 MailItem 对象(最终,我想将电子邮件保存在其他位置并修改 .Subject 属性)。
这是我到目前为止所拥有的:
private void btnFolderWalk_Click(object sender, EventArgs e)
{
// Retrieve the name of the top-level folder (Inbox) , for
// the purposes of this demonstration.
Outlook.Folder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
as Outlook.Folder; // Cast the MAPI folder returned as an Outlook folder
// Retrieve a reference to the top-level folder.
if (inbox != null)
{
Outlook.Folder parent = inbox.Parent as Outlook.Folder; // the mailbox itself
if (parent != null)
{
RecurseThroughFolders(parent, 0);
}
}
}
private void RecurseThroughFolders(Outlook.Folder theRootFolder, int depth)
{
if (theRootFolder.DefaultItemType != Outlook.OlItemType.olMailItem)
{
return;
}
lbMail.Items.Add(theRootFolder.FolderPath);
foreach (Object item in theRootFolder.Items)
{
if (item.GetType() == typeof(Outlook.MailItem))
{
Outlook.MailItem mi = (Outlook.MailItem)item;
lbMail.Items.Add(mi.Subject);
//-------------------------------------------------------------------------
// mi.Subject is actually a folder name as it's full path.
// How to "open it" to get emails?
// need loop here to modify .Subject of MailItem(s) in certain subfolders
//-------------------------------------------------------------------------
}
}
foreach (Outlook.Folder folder in theRootFolder.Folders)
{
RecurseThroughFolders(folder, depth + 1);
}
}
我在这个阶段使用列表框来解决问题,当前的输出如下所示。我想“处理”“项目nnnnnn” 文件夹。
\\Personal Folders
\\Personal Folders\Deleted Items
\\Personal Folders\Inbox
\\Personal Folders\Inbox\MySubFolder
\\Personal Folders\Inbox\MySubFolder\Project456212
\\Personal Folders\Inbox\MySubFolder\Project318188
\\Personal Folders\Inbox\Outbox
\\Personal Folders\Inbox\SentItems
EDIT:
我通过对上面的循环进行轻微更改来修复此问题(即删除当前项目是邮件项目的检查):
foreach (Object item in theRootFolder.Items)
{
Outlook.MailItem mi = (Outlook.MailItem)item;
string modifiedSubject = "Modifed Subject: " + mi.Subject;
lbMail.Items.Add(modifiedSubject);
mi.Subject = modifiedSubject;
mi.Save();
// insert call webservice here to upload modified MailItem to new data store
}