Inserting Signature In Outlook MailItem through C#

We will be looking into how to insert a signature in an outlook email whenever an end user compose a new email.

We will be basically creating an Outlook Inspector for the new mail and then after few validations will be inserting a signature through manipulating the Email HtmlBody.

Declare the inspector

Outlook.Inspectors olInspectors;

Trapping the Application.Inspectors.NewInspector and adding a event handler to it in ThisAddin_StartUp.

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
olInspectors = this.Application.Inspectors;
olInspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(My_NewInspector);

}

Adding the event Handler:

void My_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
if (mailItem != null)
{
if (mailItem.EntryID == null)
{
mailItem.HTMLBody = “Thanks & Regards,<br/> Nitin Rawat <br/> <a style=’text-decoration: none’ href=’https://nitinasp.wordpress.com/’>Blog</a&gt; “;
}
}
}

A newly composed mail does not have a EntryID but a sent mail have a EntryID so this check is placed to enter the signature only when the mail is new.

We can perform a large set of operations in the My_NewInspector method depending on our need.

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