Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Outlook Mail Speaker

4.75/5 (5 votes)
21 Jan 2016CPOL1 min read 11.4K  
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

Image 1

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:

C#
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:

C#
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.

C#
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.

C#
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.

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)