c# - How to loop through all MailItems of certain Outlook subfolders -
i'm working on outlook 2007 add-in. found code loop through folders have not been able figure out how loop inside given folder examine mailitem objects (ultimately, want save emails elsewhere , modify .subject property).
here have far:
private void btnfolderwalk_click(object sender, eventargs e) { // retrieve name of top-level folder (inbox) , // purposes of demonstration. outlook.folder inbox = application.session.getdefaultfolder(outlook.oldefaultfolders.olfolderinbox) outlook.folder; // cast mapi folder returned outlook folder // retrieve reference top-level folder. if (inbox != null) { outlook.folder parent = inbox.parent outlook.folder; // mailbox 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 folder name it's full path. // how "open it" emails? // need loop here modify .subject of mailitem(s) in subfolders //------------------------------------------------------------------------- } } foreach (outlook.folder folder in therootfolder.folders) { recursethroughfolders(folder, depth + 1); } }
i'm using listbox @ stage of working things out , output looks below. want "process" email messages of "projectnnnnnn" folders.
\\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:
i fixed slight change in loop above (i.e. removing check current item mailitem):
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 upload modified mailitem new data store }
while above code may work come across unhandled invalidcastexception not items in root folder mail items (e.g. meeting requests).
following code worked me:
foreach (object item in items) { if (item outlook.mailitem) { ///the rest of code } }
Comments
Post a Comment