Introduction

This is an introduction to Microsoft Outlook and its accessible properties and methods you can access through C#. We will not be working with the entire set of properties and methods, just the properties that can be attributed to an email. We will be showing you how to access your Outlook email through C# and how to display that information on a screen.

In our previous article we described how you needed to install the .NET Programmability Support for version 2.0 so the required dynamic link libraries could be installed and utilized in Visual Studio for our example. We also learned how we needed to add a reference to this dynamic link library so we could access it in our code.

Implementation

In this article we will be adding the code that accesses the dynamic link library and creating the calls required to access the Email properties within Outlook. The Outlook collection returned could potentially have multiple Emails within it, but for the scope of this example we will just be accessing the first email in the returned collection.

To extend this application you could take that collection and within a foreach loop iterate through all of the Emails looking for a specific Email, or possibly even populating a grid with all of the Emails.

Includes

You will need to add a reference in your class to the Outlook reference you added to the project in our previous example. This is done by using the following syntax. If this include statement is not added to your class you will receive an error message when you are trying to build this project.

Create the Inbox object

This is where we will utilize the reference we added in the example above which returns the object with the Outlook Emails that are within your Inbox in Outlook.

Grab the Subject of an Email

In order to return the Subject of an email we will need to cast the item within the MAPI Inbox folder to a MailItem. Once it has been correctly casted you can then access the Subject property.
Please Note: When working with the Outlook Class you will need to start all array access with a 1 and not a zero. These are not zero based arrays and if you try to treat them as such you will receive an “Index could not be located” error when you try to run the application.

Attachment File Name

In order to return the Attachment File Name of an email we will need to cast the item within the MAPI Inbox folder to a MailItem. Once it has been correctly casted you can then access the Attachment Collection Object. This collection of attachments is also not a zero based collection and you will need to access the indexes by starting a 1 and not 0.

Body of the Email

In order to return the Body of an email we will need to cast the item within the MAPI Inbox folder to a MailItem. Once it has been correctly casted you can then access the Body property.

Next Up

In the next article we will finish this example by accessing the remaining properties we will need to populate the remaining fields on our screen. We will then be able to run the application to access our Email within Outlook.

What have we learned?

That we can access our Outlook Email from C#
How to call Outlook and return the Inbox Folder with Emails as a collection

Download Source Files