Click here to Skip to main content
15,895,813 members
Articles / Programming Languages / C#
Tip/Trick

Outlook Mail Speaker

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
21 Jan 2016CPOL1 min read 11K   7   4
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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNeed complete source code please Pin
Biswojit Kumar Sahu22-Jan-16 10:56
professionalBiswojit Kumar Sahu22-Jan-16 10:56 
Hi Arun,

It is really a good article in terms of usage and learning. Please send me the complete source code to me biswojit_sahu@yahoo.co.in

Many Thanks Again
Biswojit

GeneralRe: Need complete source code please Pin
PIEBALDconsult22-Jan-16 13:24
mvePIEBALDconsult22-Jan-16 13:24 
GeneralRe: Need complete source code please Pin
Biswojit Kumar Sahu23-Jan-16 5:57
professionalBiswojit Kumar Sahu23-Jan-16 5:57 
GeneralRe: Need complete source code please Pin
PIEBALDconsult23-Jan-16 6:04
mvePIEBALDconsult23-Jan-16 6:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.