How to access Outlook and post to a blog using C#


This is my first attempt at an instructional article, so opinions on quality would be great!  Let me know if I made any mistakes too... It's mainly for all those like Robert Scoble who would like to be able to drag and drop an item to a folder in their Outlook and post it instantly to their Blog, but it also covers web services and talking to Outlook.

Accessing Outlook

The first requirement is to be able to access Outlook.  For those with Outlook/Office 2003, you should run the office install and choose .NET Programmability Support. For Office XP, you can download the Interop assemblies from here. The appropriate assemblies will then be installed in the GAC, and you can then add a reference from your Visual Studio project to Microsoft.Office.Interop.Outlook.dll.

Add an appropriate using clause:

using Outlook = Microsoft.Office.Interop.Outlook;

Then you should be able to instantiate an Outlook object and make requests of it:

Outlook.Application app = new Outlook.ApplicationClass();
Outlook.NameSpace NS = app.GetNamespace("MAPI");
Outlook.MAPIFolder inboxFld = NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

This will give you access to inboxFld, which will allow you to iterate through the contents of the inbox! You can also change this to iterate through notes, or through calendar entries, tasks, etc. as you want.

For example, to iterate through your mail you can do:

foreach(Outlook.MailItem t in inboxFld.Items)

To write out all the subjects on the console. The only annoying thing will be you need to say yes to a security dialog when you access mail items - this is a new security "feature" of Outlook  - I'm working on getting around this, it doesn't happen for tasks or notes, etc.

Once you are able to access Outlook, your next objective is to post data to your weblog.  You can avoid duplicates through one of two ways:

  • Keep track of what has been posted by maintaining an ArrayList of articles on your blog and checking before trying to post one.
  • Keep track of what has been posted by changing something in the MailItem's - e.g. - set or clear a flag.

The first method requires keeping a list synchronised with the blog, the second is quickest and easiest, but wouldn't be suited with multiple people possibly posting things.

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An idiot with a computer is a faster, better idiot” - Rich Julius