Loop thru an inbox

Add-in Express™ Support Service
That's what is more important than anything else

Loop thru an inbox
 
Olivier Bruylandt




Posts: 1
Joined: 2006-02-14
Hello,

Here is my question : can someone gives me a little help to set up a loop in the mails ?

I am using a code (OOM) which retrieves the first mail of the box. I am also using the way to skip the outlook security pop up.

			object nameSpace = null, items = null, folder = null; 
			object outlookApp = null, mailItem = null;
			tbInfo.Clear();
			Type olType = Type.GetTypeFromProgID("Outlook.Application", false);
			if (olType != null)
			{
				try
				{
					outlookApp = Activator.CreateInstance(olType);
					if (outlookApp != null)
					{
						securityManager1.ConnectTo(outlookApp);
						securityManager1.DisableOOMWarnings = true;
					}
					try
					{
						nameSpace = outlookApp.GetType().InvokeMember("GetNamespace", BindingFlags.InvokeMethod, null, outlookApp, new object[]{"MAPI"});
						folder = nameSpace.GetType().InvokeMember("GetDefaultFolder", BindingFlags.InvokeMethod, null, nameSpace, new object[]{OlDefaultFolders.olFolderInbox});
						
						items = folder.GetType().InvokeMember("Items", BindingFlags.GetProperty, null, folder, null);
						
						mailItem = items.GetType().InvokeMember("Item", BindingFlags.InvokeMethod, null, items, new object[]{1});
						if (mailItem != null)
						{
							string[] lines = new string[5];
							lines[0] = ("*** First message in Microsoft Outlook inbox (Outlook Object Model) ***");
							lines[1] = "";
							lines[2] = "From: "+Convert.ToString(mailItem.GetType().InvokeMember("SenderName", BindingFlags.GetProperty, null, mailItem, null));
							lines[3] = "Subject: "+Convert.ToString(mailItem.GetType().InvokeMember("Subject", BindingFlags.GetProperty, null, mailItem, null));
							lines[4] = "Body: "+Convert.ToString(mailItem.GetType().InvokeMember("Body", BindingFlags.GetProperty, null, mailItem, null));
							tbInfo.Lines = lines;
						}
					}
					catch
					{
					}
				}
				finally
				{
					securityManager1.DisableOOMWarnings = false;
					if (mailItem != null)
						Marshal.ReleaseComObject(mailItem);
					if (items != null)
						Marshal.ReleaseComObject(items);
					if (folder != null)
						Marshal.ReleaseComObject(folder);
					if (nameSpace != null)
						Marshal.ReleaseComObject(nameSpace);
					if (outlookApp != null)
					{
						outlookApp.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, outlookApp, null);
						Marshal.ReleaseComObject(outlookApp);
					}
				}
			}


This works fine but :

- how can I implement in this a loop to pass thru all the mails of the folder instead of only looking the 1rst one ? Can't find the trigger on which perform a foreach or for. (In fact, the project is to find a string in a received mail to chain actions behind)

- I am also looking how to move a mail from a folder to another one.

- let me say I have a folder renamed in "test123", is it possible to read the mails in this sub folder ?

Regards,

/Olivier
Posted 14 Feb, 2006 05:49:47 Top
Eugene Starostin


ADX Team
Add-in Express team


Posts: 430
Joined: 2004-04-05
Hello Olivier,

Sorry, but how do these three points relate to Outlook Security Manager?


To All,

It seems the time for me to say the following:

Dear Customers,

Please remember that we are glad to cunsult you on how to use Outlook, Word, Excel, PowerPoint, Publisher, Access, Visio, MapPoint, Project, FrontPage objects if you are subscribed to our Premium Support Service.

Thank you for your understanding.
Posted 14 Feb, 2006 09:23:33 Top
Sergey Grischenko


Add-in Express team


Posts: 7205
Joined: 2004-07-05
Hi Olivier.

- how can I implement in this a loop to pass thru all the mails of the folder instead of only looking the 1rst one ? Can't find the trigger on which perform a foreach or for. (In fact, the project is to find a string in a received mail to chain actions behind)


You can use the code below:

...........
try
{
nameSpace = outlookApp.GetType().InvokeMember("GetNamespace", BindingFlags.InvokeMethod, null, outlookApp, new object[]{"MAPI"});
folder = nameSpace.GetType().InvokeMember("GetDefaultFolder", BindingFlags.InvokeMethod, null, nameSpace, new object[]{OlDefaultFolders.olFolderInbox});
items = folder.GetType().InvokeMember("Items", BindingFlags.GetProperty, null, folder, null);
int count = Convert.ToInt32(items.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, items, null));
for(int i=1; i<=count; i++)
{
mailItem = null;
try
{
mailItem = items.GetType().InvokeMember("Item", BindingFlags.InvokeMethod, null, items, new object[]{i});
if (mailItem != null)
{
string[] lines = new string[4];
lines[0] = ("*** First message in Microsoft Outlook inbox (Outlook Object Model) ***");
lines[1] = "";
lines[2] = "From: "+Convert.ToString(mailItem.GetType().InvokeMember("SenderName", BindingFlags.GetProperty, null, mailItem, null));
lines[3] = "Subject: "+Convert.ToString(mailItem.GetType().InvokeMember("Subject", BindingFlags.GetProperty, null, mailItem, null));
tbInfo.Lines = lines;
this.Update();
}
}
finally
{
if (mailItem != null)
Marshal.ReleaseComObject(mailItem);
}
}
}
catch
{
}

.............

I am also looking how to move a mail from a folder to another one.

The Outlook.MailItemClass contains the Move method. You can use it to move your emails.

let me say I have a folder renamed in "test123", is it possible to read the mails in this sub folder ?

You need to pass through all folders (using Outlook.NameSpaceClass.Folders collection) and find the "test123" folder. Then you can use the Outlook.MAPIFolder.Items property to get all email.

Posted 14 Feb, 2006 10:55:55 Top