Archive

Posts Tagged ‘Outlook Add-in’

Outlook Suggestions

A great feature was recently added to Gmail, and I think it’s been called “Gmail Suggestions”. This feature remind you to add contacts to your recipients list when sending mails, based on mails from the past.

quick search on Google.com may result the same feature for Outlook, but since I wanted to write some add-in for Outlook 2010 long time ago, it’s sounds like a nice practice.

in this example, I’m not going to spend much time in the Contact search algorithm, performance and some other staff – I only want to show the concept. so, this is how it’s goanna work:

  1. Creating an Object for all recipients by sent mails.
  2. When adding recipients to a new mail, we’ll search for relevant contacts and add them to a window located on the bottom of the new mail screen.

Creating Outlook 2010 Add-in Project

First, I’ll create new Outlook add-in project, by selecting the right project template:

image

As you can see, VS created the ThisAddIn.cs file for you, and except the Auto generated code – we have two event handlers here: one for the startup, and the second for shutdown.

As I said before, in order to complete this feature and make it ready for publish, some points need to be considered. For now, I only want to concentrate in the basic concept: suggest the outlook user some recipients he may forgot.

So first, whenever the outlook client is launched, I’ll look for any sent mail and found the recipients. This operation might be heavy for large mailboxes, so we should consider change this mechanism before production. For now, it should be fine:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Contacts = new Dictionary<List<string>, int>();

Outlook.NameSpace ns = this.Application.GetNamespace("MAPI");
Outlook.MAPIFolder sentMailsFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);

foreach (Outlook.MailItem item in sentMailsFolder.Items.OfType<Outlook.MailItem>())
{
var contactList = new List<string>();

foreach (Outlook.Recipient rec in item.Recipients)
{
contactList.Add(rec.Name);
}

contactList = contactList.OrderBy(c => c).ToList();

/**
* Contacts.ContainsKey won't work here beacuse the list has different reference.
* So we must user foreach.
**/
bool found = false;
foreach (var contacts in Contacts.Keys)
{
if (contacts.SequenceEqual(contactList))
{
contactList = contacts;
found = true;
continue;
}
}

if (!found)
Contacts.Add(contactList, 1);
else
Contacts[contactList]++;
}

Creating the Form Region

Inside Outlook mail window, we want to suggest the user some relevant contacts, and allow him to add them to the To line as quickly as possible.

Right click on your project –> Create New Item – > Outlook Form region, wizard will appear:

We want to create a new form region, so in the first screen the first option will fit our needs. click next in order to get the Type screen. In the type screen we have some options:

  • Separate – this option creates different page to the form, so you cannot see the mail body and the form region at the same time.
  • Adjoining – Select this option, in order the get the form region as part of the mail body screen. we’ll choose that.
  • Replacement – like Separate, this will create new page, but this option will mark this page as the default page for this item.
  • Replace All – let the form region be the only page for that item.

image

Select the Adjoining option, and click next.

In the next page, give the region name, say Suggestion, and check only the first check-box. we will only want the region to appear when we’re in compose mode, no suggestions needed in reading pane / reading mode. After clicking next. keep the MailMessage item as the only one checked, and click Finish.

Create the Suggestion control

Basically outlook region form uses win forms control, so we’ll use simple Flow Layout control for our suggestion control. Go to the toolbox, and add FlowLayout control to the design surface, set the docking to Fill, and add some other design properties if you like to.

when creating the form region, Visual studio automatically created a code behind file with a partial factory class named (in our case) SugestionsFactory. this partial class already contains event handler for the FormRegionInitializing event, so we can make some manipulations like cancel the form region before it’s get into view. For now, no need to put any code over there.

The only thing we need to care of is, updating the suggestion list based on the contacts already appear in the To list. In order to do that, we’ll create a listener to the PropertyChanged event when the form loaded, like this:

private void Suggestions_FormRegionShowing(object sender, System.EventArgs e)
{
this.item = this.OutlookItem as Outlook.MailItem;
item.PropertyChange += new Outlook.ItemEvents_10_PropertyChangeEventHandler(item_PropertyChange);
}

Now, when this listener is called, just need to select the relevant suggestions from the Contacts static property we’ve created before. We decently have more than one option here. my logic was to take all the suggestions that contains all the current recipients, and give them a rank based on how many mails sent to them. then, I choose the best 7, thus not giving the user list with hundreds of items.

After choosing the relevant contacts, add them to the FlowLayout control with a ClickEventHandler. Whenever the user clicks a contact, it should be added to contact list. Notice we called Resolve to force Outlook to resolve this new string as a contact:

void ShowSuggestion(string contact)
{
LinkLabel lb = new LinkLabel();
lb.Text = contact;
lb.Click += new EventHandler(lb_Click);
this.flowLayoutPanel1.Controls.Add(lb);
}

void lb_Click(object sender, EventArgs e)
{
LinkLabel lb = sender as LinkLabel;
item.Recipients.Add(lb.Text);
item.Recipients.ResolveAll();
}

Many things need to be done before using this simple app in real world scenario, like handling contact name changing, and refresh the contact list when new item sent, etc. For now, this is how it looks like:

image

Enjoyסמיילי

Source Code Here

Categories: C#, Outlook Tags: , , , , ,
%d bloggers like this: