Tim Huffam

Dotting the I and crossing the T of I.T.

  Home  |   Contact  |   Syndication    |   Login
  129 Posts | 0 Stories | 874 Comments | 677 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

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:

  1. In VS.NET right click on References and choose Add Reference.
  2. Select the COM tab
  3. Choose "Microsoft Outlook 11.0 Object Library" (this is for MS Office 2003 - I think 10.0 is for Office XP) and click Select.
  4. 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;
}

HTH

Tim

posted on Friday, May 26, 2006 10:35 AM

Feedback

# re: Accessing Outlook items from C# .NET 5/26/2006 4:00 PM alcsharp
I was looking for exactly this. The only thing I had to change was casting to a MailItem instead of a PostItem in the for loop cuz I was getting an error. Works great.... TY

# re: Accessing Outlook items from C# .NET 5/27/2006 8:40 AM Abdul Kader Jilani
item = (Microsoft.Office.Interop.Outlook.PostItem)subFolder.Items[i];
its giving ...
Unhandled Exception: System.InvalidCastException: Specified cast is not valid.

# re: Accessing Outlook items from C# .NET 5/27/2006 8:43 AM Abdul Kader Jilani
Thanks its working now.

# re: Accessing Outlook items from C# .NET 5/27/2006 10:19 AM Abdul Kader Jilani
its not working in other PC

# re: Accessing Outlook items from C# .NET 5/27/2006 10:20 AM Abdul Kader Jilani
its deployment version is not working in other PC

# Fixed 10/10/2006 6:59 AM rippingandfailing
Hey guys, its a simple fix. change

Microsoft.Office.Interop.Outlook.PostItem

to

Microsoft.Office.Interop.Outlook.MailItem

=)

# re: Accessing Outlook items from C# .NET 9/7/2007 6:01 AM Naveen
can you help how to &quot;Mark an Email as Read&quot;?
That will help me a lot.
I will be very thankful to you on reply.


# re: Accessing Outlook items from C# .NET 10/10/2007 9:47 AM Sebas
I've got a list of 502 items. I can get the first (newest) 240 items. The I cant get them because the items are not postitems or mailitems (or any other item, checked them all) I suspect that there was a migration from exchange 2000 to 2003 arround the date of itemnumber 241. Doe this ring a bell??

Thanks,

Bas

# re: Accessing Outlook items from C# .NET 10/12/2007 4:44 AM Jayanth
I have got 4 attachments it's extensions are .xsn & .lgo. i am able to extract attachments with extension'e .lgo through Microsoft.Office.Interop.Outlook.PostItem. but i can't extract .xsn files. it's giving me error like unable to convert system._comobject. is there any other way to type cast the items? please help me.

# re: Accessing Outlook items from C# .NET 11/26/2007 1:14 PM satish
when i use this (Outlook.PostItem) _item; i am getting error "Specified cast is not valid. "

# re: Accessing Outlook items from C# .NET 12/17/2007 12:38 PM abhishek
hi,

i want to disable "automatic download" feature of MS Outlook 2007 in "trust center" thrugh C#.net.
pls tell me the solution.

# rdisable "automatic download" in C# .NET 12/17/2007 1:30 PM saurabh
hi,

i want to disable "automatic download" feature of MS Outlook 2007 in "trust center" thrugh C#.net.

pls tell me the solution.


Thanks in Advance..

Saurabh..


# re: Accessing Outlook items from C# .NET 1/9/2008 4:22 AM srinivas
hi,


i want to access the SEND button in compose. using c# how can we display a message when send button clicked?

pls tell me the solution


thnks n regards,
Srinivas

# re: Accessing Outlook items from C# .NET 1/10/2008 11:24 PM chris
How to run this code in a deployment scenario? coz as far as i can guess, this runs in the context of the logged in user,so if we are deploying ths code to a server, surely it will fail.
So how to access the mailbox of a user in this scenario?



# re: Accessing Outlook items from C# .NET 2/20/2008 11:34 AM adam
How can we specify which user's mailbox to log on to?

# re: Accessing Outlook items from C# .NET 2/22/2008 12:17 PM Sean
Hi,
I just want to say how utterly fantastic I found this page. This is just what I was looking for also. Perfect!

It will allow me to do custom scaping of my outlook files for certain text and make email lists!!

Cheers to you!

Best - Sean

# re: Accessing Outlook items from C# .NET 4/16/2008 6:06 PM Johnson
Hi,

After the 240 Mail items i can't access the mail items.

Could u help me.....

Thanks.

# re: Accessing Outlook items from C# .NET 5/26/2008 8:12 PM Harry_cherry
Hi ;

My task is to read alll the emails from the outlook component . Here is te code I am using and getting the error :" Specified cast not valid"

Dim _objItem As Outlook.MailItem = Nothing
Dim _objInboxFolder As Outlook.MAPIFolder = Nothing
Dim _objSubfolder As Outlook.MAPIFolder
Dim _outlook As Outlook.MailItemClass
Dim _outlookObj As New Outlook.Application
Dim _outlookNamespace As Outlook.NameSpace
_outlookNamespace = _outlookObj.GetNamespace("MAPI")
_outlookNamespace.Logon(Nothing, Nothing, False, False)
_objInboxFolder = _outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
_objSubfolder = _objInboxFolder.Folders(1)
Response.Write("Folder Name: " & _objSubfolder.Name & " Entry ID" & _objSubfolder.EntryID)
Response.Write("Num Items: " & _objSubfolder.Items.Count)
Dim _itemCount As Integer = _objSubfolder.Items.Count
While (_itemCount <> 0)
_objItem = CType(_objSubfolder.Items(_itemCount), Outlook.PostItem)
Response.Write("Item " & _objItem.Subject.ToString)
_itemCount -= 1
End While



Kindly let me know where I am wrong
Thanks

# re: Accessing Outlook items from C# .NET 6/21/2008 5:46 AM Scott Welker
Thanks!

MailItem vs PostItem - a gold nugget. I still seek a method for determining the kind of item in a folder's Items collection so that I can cast it (or skip it). Seeks hokey to use nested "if (myItems[i] is Microsoft.Office.Interop.Outlook.<item kind>)

Anyway, thanks again!

# re: Accessing Outlook items from C# .NET 8/28/2008 9:34 PM Robbert
Johnson, you aren't releasing your COM objects so the server/client hits it's message limit of 250 items. I struggled with the same issue.

Scott, you can use Reflection to get the MessageClass of the object.

# re: Accessing Outlook items from C# .NET 9/1/2008 8:34 PM Sergey Vasiliev
Hi,

Thank you for the code.
But I did not find how to connect to the non default Exchange server?


# re: Accessing Outlook items from C# .NET 9/4/2008 9:20 PM narasiman
hi,

I cannt found the namaspace Microsoft.Office.Interop.Outlook and i am using vs 2003 and pls guide me

# re: Accessing Outlook items from C# .NET 9/19/2008 12:30 PM Peter Smith
I have the following code :


if (item is Outlook.MailItem)
{
Outlook.MailItem mailItem = item as Outlook.MailItem;
ProcessMailItem(mailItem);
}
else if (item is Outlook.PostItem)
{
Outlook.PostItem postItem = item as Outlook.PostItem;
ProcessPostItem(postItem);
}
else if (item is Outlook.MeetingItem)
{
Outlook.MeetingItem meetingItem = item as Outlook.MeetingItem;
meetingItem.UnRead = false;
}
else
{
Log("unknown item type - " + item.ToString() + " in folder:" + folder.FullFolderPath, MSPLLogMessage.Severity.critical);
}


What we actually have is an Excel object - by dragging an Excel sheet into the Outlook public folder, but our server does not have Office installed, only Outlook. So the else clause only says we have a System.__ComObject. So it doesnt know what an Excel object is. Is there any way we can mark the Excel file as read?

# re: Accessing Outlook items from C# .NET 10/2/2008 10:14 PM Peter Izuoba
Nice work,thanks.

# re: Accessing Outlook items from C# .NET 10/15/2008 6:15 PM Hemnath S
Awesome dude.Thanks

# re: Accessing Outlook items from C# .NET 10/17/2008 3:22 AM Rick
I just want to give an update on the "Unable to cast COM object of type..." error I am getting and was asking previously. This error that I was getting was caused by conflicts on items in the outlook folder that I was processing. The thing is, when you do a visual check on the items on the folder, there is no way to tell that an item was on conflict until you open it or double click on it. You have to pinpoint the actual item, open it and resolve the conflict before that item becomes a normal item again. Hope this helps cause this issue caused me real grief!

# re: Accessing Outlook items from C# .NET 11/1/2008 12:22 AM Phil
This code just sits there in the browser and doesn't retrieve anything.

It opens an Outlook object on the web server and the CPU is running at 75%, so it's doing something!

Any ideas what might be happening?

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 1 and 2 and type the answer here: