Library tutorials & articles

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

Introduction

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)
{
    Console.WriteLine(t.Subject);
}

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.

Comments

  1. 05 Sep 2007 at 07:50

    I am working for fetching contacts from outlook express. I have explored a lot , But I couldn't .

    How can I get access microsoft outlook express's  conatacts?

    Is this possible?

     

  2. 05 Mar 2005 at 16:17

    Hii Friends,


    This is kumar, I facing the following problm. If Any body knows the solution please pass the  solution to my E-Mail Id.


    Query:          How To Dynamically Pass The .aspx Page Values Into an .Html Page In ASP.NET + C#. Here I am Passing more than one value. I dont want to write hard code.


    Query:         I am writing the code for getting the data from database. In this data i have images. Then i have to pass these images to web page. Its like a banner Engine. But, Here i dont wont to take a "adrotator". I had taken a Html Iframe control. Then I have to pass the image name throuth src. Then i had verified and got the required images through database comparisons. When i run the application i have to display the web like "http://wepage1.aspx/sample.html". In this sample.html page is the dynamic page. When u run the web page ".aspx" that time i need to display/ pass these wepage image values to .html(sample.html) page.
                   How this is possible? If anybody knows the solution please pass the solution to my ID


    Thnx and Regards
    Naresh Kumar
    ravuri_naresh@yahoo.co.in  

  3. 12 Feb 2005 at 12:13

    hello,



    I want somre help from u.we are using  check boxes in datagrid for activating and deactivating.how to put that
    check boxes in datagrid and reflect changes in database

                                                                                  thank you,
                                                                MohanVarma.
  4. 01 Jan 1999 at 00:00

    This thread is for discussions of How to access Outlook and post to a blog using C#.

Leave a comment

Sign in or Join us (it's free).

Simon Soanes

We'd love to hear what you think! Submit ideas or give us feedback