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 the files here, from the Add-In we developed.
Step 1. Before we begin to extending our last program (add-in), would like to quickly brief few concepts:
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 Application object. How do we get this.? In “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 “CodeSteps.cs” file; where we need to access the Outlook objects.
From anywhere in the Project, we can get the Application object; from Globals. Below statement gives 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, Explorer window will display the contents of the Folder; e-mails, contacts etc.,.
So, we need to get the Explorer object; before we get mail message. Below statement, will return an Active Explorer object; where folder content is displayed.
Step 5. From 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 Selection property of Explorer object.
So, if we select e-mail(s), we get the the list of e-mail(s) we selected from Selection property. As I mentioned before, 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 for e-mail item. If the selection is an e-mail / mail message; Object Model returns MailItem object; deals with 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 open the Outlook application; and display the “Show Subject” button what we have added through our Add-in. Click on the Button, and you will see the message box displaying with the selected mail Subject.
If you not selected any mail message, clicking on 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 below Comments section.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.