Access your Email within Outlook - Part Two of Three
This is Part Two of this article series. Read Part 1 here
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
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
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.
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.
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.
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
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.
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
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