Initiate action when a new mailitem arrive in an Outlook folder

Some times we need to trigger an action when a new mailitem arrives in a Outlook Folder.

Requisite may be anything from notifying the user about the new Mailitem to manipulate the arriving mailitem.

We can achieve this functionality by registering the YourFolder_ItemAddevent of the required folder in the ThisAddIn_Startup event and then performing the required action in the YourFolder_ItemAdd event.

 

Following is the ThisAddIn_Startup method :

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{

string folderPath =

Application.Session.
DefaultStore.GetRootFolder().FolderPath
+ @”\Outbox\My Outbox”;
Outlook.Folder quatFolder = GetFolder(folderPath);

_OutboxItems = quatFolder.Items;
_OutboxItems.ItemAdd += OutboxItems_ItemAdd;
Marshal.ReleaseComObject(quatFolder);

}

 

In the above method we have defined an event that will run whenever a item arrives in the My Outbox folder of Outlook.

private void OutboxItems_ItemAdd(object item)
{
if (item is MailItem)
{
Outlook.MailItem mailItem = item as Outlook.MailItem;

if (mailItem != null)
{

//Here you can do whatever is the required action that you want to perform

}

}

 

In the above method first we have checked that the incoming item is a mailIItem and then we can perform desired action.

We can manipulate the properties of the mailItem like attachments , To , CC , BCC and others.

We can notify the user about the same by showing a messagebox.

This is how we can trigger a code on the incoming mailItem on a Outlook Folder.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s