65.9K
CodeProject is changing. Read more.
Home

Sending messages by email and Microsoft Office Communicator 2005

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (2 votes)

Jun 2, 2008

CPOL

1 min read

viewsIcon

29220

downloadIcon

213

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.

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:

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:

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

In main():

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

This is the important part:

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.