Click here to Skip to main content
15,881,559 members
Articles / Desktop Programming / Windows Forms

Sending messages by email and Microsoft Office Communicator 2005

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
2 Jun 2008CPOL1 min read 28.7K   213   11   7
Save time sending massive messages.

Introduction

This program tries to make your life easier when you're at work. Every time you want to go on break time, you would have to type emails or add a lot of contacts to a conversation to say: "let's go and have a coffee!" Now, only with a double click, this program does it for you.

Background

The message sender is based on the article: "Automate key sequences for repetitive tasks, reference XML data". With very little changes, you can improve it to reach the objective. So, please read SathishVJ's article before reading this, so you can understand the changes I have done.

Using the code

The application reads from a text file named emails.txt, where you include the emails of the people in your company. You have to list each email in one line without separating them with a comma. The first line is read and it opens a new messenger conversation. Then, the following emails are added into the conversation. Using the SendKeys function, the program takes control of the messenger and writes the message you want to your workmates.

C#
string destinatario = "";

public bool LoadKeySequence() 
{
    ArrayList keySequenceArray = new ArrayList();

    StreamReader sr = new StreamReader(@"emails.txt");
    String line;
    if ((line = sr.ReadLine()) != null) 
    {
        line = line.Trim();
        //Opens new conversation
        keySequenceArray.Add("%(a)s");
        keySequenceArray.Add(line);
        keySequenceArray.Add("{TAB}{TAB}{ENTER}");
        destinatario += line;
        while ((line = sr.ReadLine()) != null)
        {
            destinatario += ",";
            line = line.Trim();
            //Add to conversation
            keySequenceArray.Add("%(a)j");
            keySequenceArray.Add(line);
            keySequenceArray.Add("{TAB}{TAB}{ENTER}");
            //Email receivers list separated by comma
            destinatario = destinatario+ line;
        }
        keySequenceArray.Add("Let's have a break and a coffee");
        keySequenceArray.Add("{ENTER}");
        keySequenceArray.Add("Auto message!!!");
        keySequenceArray.Add("{ENTER}");
        keySequenceArray.Add("{ESC}");
     }

    this.sendKeysSequence = (string[])keySequenceArray.ToArray(typeof(string));
    return true;
}

Now, I'm going to explain how to send the emails:

C#
string asunto = "BREAK TIME";
string body = "Don't answer to this email. Let's meet at the office door!!!!";

private void mandarEmails()
{
    //Write your company SMTP server.
    string servidor = "company.smtp.server";
    string html = "";
    // Write who's sending the email and the receivers of your email 
    MailMessage message = new MailMessage("myemail@domain.eh",destinatario);
    // You can write what ever you want in the email and put your signature
    message.IsBodyHtml = true;
    html += @"<html><head> … ";
        .
        .
        .
    html += @" … </head></html>";
    System.Text.StringBuilder messageBody = new System.Text.StringBuilder();
    messageBody.Append(html);
    message.Body = messageBody.ToString();
    message.BodyEncoding = System.Text.Encoding.UTF8;
    message.Subject= asunto;
    SmtpClient cliente = new SmtpClient(servidor);
    //Add credentials to the server
    cliente.Credentials = CredentialCache.DefaultNetworkCredentials;
    //Here you send the email 
    try 
    {
        cliente.Send(message);
    }
    catch (System.Net.Mail.SmtpException ex)
    {

 
        MessageBox.Show("ERROR!!");
    }
}

In MainForm(), you have to specify the process you want to open, in this case, it is communicator.exe:

C#
System.Diagnostics.Process p= new System.Diagnostics.Process();
p.StartInfo.FileName = @"communicator.exe";
p.Start();

In main():

C#
static void Main()
{
     MainForm brum = new MainForm();
     brum.arranca(); 
}

This is the important part:

C#
private void arranca()
{
    if (!this.LoadKeySequence())
    {
        return;
    }
     //Find process handle 
    if (!this.FindProcess())
    {
        return;
    }
    SetForegroundWindow(this.processWindowHandle);
    SetActiveWindow(this.processWindowHandle);
    mandarEmails();
    if (this.noDataFilesRequiredCheckBox.Checked)
    {
        //call other method 
        for (int i = 0; i< this.repeatCountNumericUpDown.Value; i++)
         { 
            ExecuteKeySequence(null);
        }
    SetForegroundWindow(Process.GetCurrentProcess().MainWindowHandle);
    MessageBox.Show("I have sent everything!!");


    Application.Exit();
    return;
    }
}

Finally, set thse in the MainForm.cs design interface:

  • keySequenceFileTextBox.Text= Path where the file "emails.txt" is.
  • targetAppTitleTextBox.Text= Microsoft Office Communicator

And your program is finished and ready to run!! Have fun with it.

License

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


Written By
Engineer
Spain Spain
I have studied Telecommunication engineering at the Public University of Navarra. Now working in Business Intelligence as an analyst consultant in TIC projects.

Comments and Discussions

 
GeneralDoesn't compile Pin
DumpsterJuice2-Jun-08 9:27
DumpsterJuice2-Jun-08 9:27 
Maybe providing the full code set would help, but I can' get this to compile.

Where there's smoke, there's a Blue Screen of death.

GeneralRe: Doesn't compile Pin
Mikel A. Rodriguez3-Jun-08 1:09
Mikel A. Rodriguez3-Jun-08 1:09 
GeneralRe: Doesn't compile Pin
DumpsterJuice3-Jun-08 1:48
DumpsterJuice3-Jun-08 1:48 
GeneralRe: Doesn't compile Pin
Mikel A. Rodriguez3-Jun-08 3:03
Mikel A. Rodriguez3-Jun-08 3:03 
GeneralTHE TITLE IS IN CAPS! Pin
leppie2-Jun-08 1:42
leppie2-Jun-08 1:42 
GeneralRe: THE TITLE IS IN CAPS! Pin
Vasudevan Deepak Kumar2-Jun-08 2:20
Vasudevan Deepak Kumar2-Jun-08 2:20 
GeneralRe: THE TITLE IS IN CAPS! Pin
Chris Maunder2-Jun-08 7:00
cofounderChris Maunder2-Jun-08 7:00 

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.