65.9K
CodeProject is changing. Read more.
Home

Outlook Mail Speaker

Jan 21, 2016

CPOL

1 min read

viewsIcon

11428

Create a service which is used to produce speech of mail content

Introduction

We tried to integrate the system.speech DLL and system.outlook.interop DLL. We created a Windows service and analyzed the incoming mail of Outlook inbox by an event. When the mail entered, then tell content of mail by the use of system.speech DLL.

Background

Sometime, we accidentally ignored urgent Outlook mail because we could not see the mail. So we tried to create a service which is used to produce speech of mail content.

Using the Code

Create a Windows Service

Then Create Class Library.cs. Refer to the system.speech and Microsoft.interop.outlook DLLs in our project.

Create a function MailReader with the following content:

    Application application = new Application();
    Microsoft.Office.Interop.Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
    nameSpace.Logon("your mailID", "your password", Missing.Value, Missing.Value);

Introduce the Outlook mail credentials. Give mail id and password here. Then, add an event ItemAdd to our inbox of Outlook like:

    try
    {
        var inboxFolder = nameSpace.Folders["your mailID"].Folders["Inbox"];
        inboxFolder.Items.ItemAdd += Items_ItemAdd;
    }
    catch (System.Exception e)
    {

    }

You can create SpeechSynthesizer when mail entered, then the event will be executed and filter the mail body, mail sender and mail subject from that mail. Create the sound of mail by the Speak function from SpeechSynthesizer class.

private static void Items_ItemAdd(object Item)
{
    SpeechSynthesizer synthesizer = new SpeechSynthesizer();
    synthesizer.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Teen);
    synthesizer.Volume = 100;
    synthesizer.Rate = 0;

    string body = (Item as Microsoft.Office.Interop.Outlook.MailItem).Body;
    string sender = (Item as Microsoft.Office.Interop.Outlook.MailItem).SenderName;
    string sub = (Item as Microsoft.Office.Interop.Outlook.MailItem).Subject;
    synthesizer.Speak(sender + " Send you a mail.");
    synthesizer.Speak("Subject is " + sub);
    synthesizer.Speak(body.Replace("_", ""));
 }

Override the OnStart method and we can call the MailReader method in OnStart method.

protected override void OnStart(string[] args)
{
    Library.MailReader();
}

Call OnStart method from onDebug method in Service with null parameter and start the Service from our main function.

Service1 myService = new Service1();
myService.onDebug();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
     new Service1()
};
ServiceBase.Run(ServicesToRun);

Then, run the service. It will detect the mail and automatically produce the sound of mail content.