I'm curious if such is possible with .NET. I wrote a small C# Windows GUI app and I was wondering if there's any way to "tap into" Microsoft Outlook 2010 and check if it has any new (unopened) emails present? Normally such condition will be indicated by the presence of an envelope icon on the system tray:
-
Do you want to interrogate Outlook, or do you want to receive an event notification? – Robert Harvey May 29 '13 at 21:15
-
Polling or "interrogating" would be better. But, still, what options do I have? – ahmd0 May 29 '13 at 21:20
-
This is probably very close to what you want: http://stackoverflow.com/questions/2055811/get-unread-mails-from-outlook – Robert Harvey May 29 '13 at 21:23
1 Answers
Hope this helps...
The following demostrates how to retreive data from items within an Outlook folder (called "MySubFolderName" under the Inbox folder) using .NET:
First add a reference to the Outlook COM object your project:
In VS.NET right click on References and choose Add Reference. Select the COM tab Choose "Microsoft Outlook 11.0 Object Library" (this is for MS Office 2003 - I think 10.0 is for Office XP) and click Select. Click OK. Note that you can access any Outlook/Exchange object types, eg Appointments, Notes, Tasks, Emails etc - just use intellisense to select which one (eg Microsoft.Office.Interop.Outlook. ... - see definition of variable called 'item' below).
Here's the code:
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.PostItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
try
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
ns.Logon(null,null,false, false);
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
subFolder = inboxFolder.Folders["MySubFolderName"]; //folder.Folders[1]; also works
Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());
for(int i=1;i<=subFolder.Items.Count;i++)
{
item = (Microsoft.Office.Interop.Outlook.PostItem)subFolder.Items[i];
Console.WriteLine("Item: {0}", i.ToString());
Console.WriteLine("Subject: {0}", item.Subject);
Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
Console.WriteLine("Categories: {0}", item.Categories);
Console.WriteLine("Body: {0}", item.Body);
Console.WriteLine("HTMLBody: {0}", item.HTMLBody);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
ns = null;
app = null;
inboxFolder = null;
}
Source @ http://geekswithblogs.net/TimH/archive/2006/05/26/79720.aspx.
Also check:
http://msdn.microsoft.com/en-us/library/bb610835(v=office.14).aspx

- 12,078
- 3
- 26
- 50

- 126
- 1
- 10