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();
keySequenceArray.Add("%(a)s");
keySequenceArray.Add(line);
keySequenceArray.Add("{TAB}{TAB}{ENTER}");
destinatario += line;
while ((line = sr.ReadLine()) != null)
{
destinatario += ",";
line = line.Trim();
keySequenceArray.Add("%(a)j");
keySequenceArray.Add(line);
keySequenceArray.Add("{TAB}{TAB}{ENTER}");
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()
{
string servidor = "company.smtp.server";
string html = "";
MailMessage message = new MailMessage("myemail@domain.eh",destinatario);
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);
cliente.Credentials = CredentialCache.DefaultNetworkCredentials;
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;
}
if (!this.FindProcess())
{
return;
}
SetForegroundWindow(this.processWindowHandle);
SetActiveWindow(this.processWindowHandle);
mandarEmails();
if (this.noDataFilesRequiredCheckBox.Checked)
{
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.
I have studied Telecommunication engineering at the Public University of Navarra. Now working in Business Intelligence as an analyst consultant in TIC projects.