August 16, 2007

Code which speaks when new email arrives in Outlook's Inbox!

Here is the code which speaks whenever new email arrives in your outlook email box. It looks MS speech API is not yet so clear...

When new email arrives, the program speaks - "New Email Arrived. Send by ??? Subject - ??? " where '???' are actual sender name or subject

Note that you might get security warning "A program is trying to access e-mail addresses you have stored in Outlook". You can get more info on this at -

using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using OutLookApp = Microsoft.Office.Interop.Outlook.Application;
using System.Speech.Synthesis;

public class MailTest {

static void Main(string[] args) {
MailTest m = new MailTest();
Console.WriteLine("Please wait for new messages...");

ApplicationClass outLookApp;
public void Start() {
// Create an Outlook application object.
outLookApp = new ApplicationClass();

Microsoft.Office.Interop.Outlook.MAPIFolder inbox = outLookApp.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
inbox.Items.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(InboxFolderItemAdded);

private void InboxFolderItemAdded(object Item) {
if (Item is Microsoft.Office.Interop.Outlook.MailItem) {
// New mail item in inbox folder
Microsoft.Office.Interop.Outlook.MailItem x = (Microsoft.Office.Interop.Outlook.MailItem)Item;

SpeechSynthesizer synth = new SpeechSynthesizer();
// You can use following voices
//LH Michael
//LH Michelle
//Microsoft Sam
synth.SelectVoice("LH Michelle");
synth.Speak("New Eamil Arrived. Send by " + x.SenderName + " . Subject " + x.Subject);