C# – MailItem object deals with mail messages in Outlook 2010 Object Model. We will extend our program, which was written in our previous article, “Outlook 2010 Add-In : Add Button to Outlook using C#“, to display the Subject of the selected Mail item, when we click on the Button (which was added through our add-in).
Note that, I am referring to the files here, from the Add-In we developed.
Step 1. Before we begin to extend our last program (add-in), would like to quickly brief a few concepts:
- The Application object represents the Outlook Application and it is the high-level object in Outlook Object Model.
- Folder object represents a folder (Inbox, Outbox, Drafts, etc.,) that contains e-mails, tasks, contacts, etc.
- Explorer object represents a window that displays contents of the Folder; like e-mails, tasks, etc.
Step 2. Open our previous project (add-in), in Visual Studio; and open the ribbon-control (added button on it), what we have added. Double click on the Button, Visual Studio will create an event handler to handle the button click event. Change the text of the button to “Show Subject” instead of “Hello!“; for more readability.
Step 3. To access any Outlook object, we must start from the Application object. How do we get this.? In the “ThisAddIn.cs” file, we can directly get the Application object. But when we click on Button, Visual Studio added the button click handler in another class which is in the “CodeSteps.cs” file; where we need to access the Outlook objects.
From anywhere in the Project, we can get the Application object; from Globals. The below statement gives an Application object to us to use in our Button handler:
Step 4. We have the Application object now; how do we get the mail item.? As I mentioned earlier, the Explorer window will display the contents of the Folder; e-mails, contacts, etc.
So, we need to get the Explorer object; before we get a mail message. The below statement will return an Active Explorer object; where folder content is displayed.
Step 5. From the Outlook window, we can select the e-mails, contacts, etc. When we select the item(s); the list of items we have selected can get from the
Selection property of
So, if we select e-mail(s), we get the list of e-mail(s) we selected from the
Selection property. As I mentioned before, the
Selection property returns all the selected items, not only e-mails.
We will consider only one item selection, and we will write our code to handle only e-mail items. If the selection is an e-mail/mail message; the Object Model returns the
MailItem object; deals with an e-mail message.
The subject of the e-mail message, we get from MailItem’s Subject property. Now we will add the code to display the selected Mail item’s Subject.
Step 4. Putting everything in the code. And the code looks like below:
Step 5. Let’s re-build & Run our Project. Visual Studio opens the Outlook application; display the “Show Subject” button that we have added through our Add-in. Click on the button, and you will see the message box displaying the selected mail Subject.
- If you have not selected any mail message, clicking on the Button doesn’t show anything.
- If you select multiple mail messages and click on the button will show the Subject of only one selected message.
I hope, you like this article. Please give the comments in the below comments section.