Thursday, September 10, 2009

Mass e-mail sender

Here's something I needed a few weeks ago and wouldn't pay for one, that's it! a mass e-mail sender. I needed to send ~60 e-mails but it had to be the same content(attachments also) and the receiver had to see only hes e-mail and mine.
After busting my azz on the Internet searching for a C++/C#/Delphi code that would do just that I freaked out, couldn't find one that actually works... tried to automate Outlook, what do you know it gave me a hard time, the 1st time it worked, 2nd time it worked also but 3rd time wasn't really my luckiest trial.
Anyways after I thought for a few minutes I've realized that Opera Browser is my savior, only thing I had to do now was to find it's shortcut's, like Ctrl+M :: New Mail, Ctrl+O(in new mail) :: Open Attachments, etc.
Here's a screenshot of the application on the job

Mass e-Mail sender

Here's the class which does all the hard work:
  TOperaAuto = class(TObject)
    private
      FOperaHWND: HWND;
    private
      procedure Wait(MSec: Cardinal);
    public
      procedure FindOpera;
      procedure Run;
      procedure NewMail;
      procedure SetTo(const sTo: String);
      procedure SetSubject(Const sSubject: String);
      procedure SetBody(const sBody: String);
      procedure SetAttachment(const sFileName: String);
      procedure PasteText;
      procedure Press_Tab;
      procedure Press_Return;
      procedure SelectAll;
      procedure SendMail;
    public
      constructor Create;
  end;

in order to send key combination to Opera I use this function found on Zarko Gajic's blog at this link
procedure PostKeyEx32(Key: Word; const Shift: TShiftState; SpecialKey: Boolean) ;
{ 
Parameters : 
* key : virtual keycode of the key to send. For printable keys this is simply the ANSI code (Ord(character)) . 
* shift : state of the modifier keys. This is a set, so you can set several of these keys (shift, control, alt, 
 mouse buttons) in tandem. The TShiftState type is declared in the Classes Unit. 
* specialkey: normally this should be False. Set it to True to specify a key on the
 numeric keypad, for example. 

Description: 
Uses keybd_event to manufacture a series of key events matching the passed parameters. The events go to the control with focus.
 Note that for characters key is always the upper-case version of the character. Sending without any modifier keys will result in a lower-case character, sending it with [ ssShift ] will result in an upper-case character! 
} 
type
   TShiftKeyInfo = record
     shift: Byte ;
     vkey: Byte ;
   end;

   ByteSet = set of 0..7 ;

const
  ShiftKeys: array [1..3] of TShiftKeyInfo =
    ((shift: Ord(ssCtrl) ; vkey: VK_CONTROL),
     (shift: Ord(ssShift) ; vkey: VK_SHIFT),
     (shift: Ord(ssAlt) ; vkey: VK_MENU));
var
   flag: DWORD;
   bShift: ByteSet absolute shift;
   j: Integer;
begin
   for j := 1 to 3 do begin
     if shiftkeys[j].shift in bShift then
       keybd_event(shiftkeys[j].vkey,
        MapVirtualKey(shiftkeys[j].vkey, 0), 0, 0);
   end;
   if specialkey then
     flag := KEYEVENTF_EXTENDEDKEY else
     flag := 0;

   keybd_event(key, MapvirtualKey(key, 0), flag, 0);
   flag := flag or KEYEVENTF_KEYUP;
   keybd_event(key, MapvirtualKey(key, 0), flag, 0);

   for j := 3 downto 1 do begin
     if shiftkeys[j].shift in bShift then
       keybd_event(shiftkeys[j].vkey,
        MapVirtualKey(shiftkeys[j].vkey, 0), KEYEVENTF_KEYUP, 0);
   end;
end;

I am too lazy to write my own, and why would I if someone already invested time in this one?
Things you need to know before you start using it:
 - You must have Opera Browser installed on your system(it's free) if not you can download Opera Browser from this link.
 - Set up an e-mail account in Opera
 - Download application as: binary(compiled executable), sourcecode or both.
How to use the application
 First you need the e-mail addresses separated by semicolon ";" put them in the To field, after the last e-mail don't put semicolon, modify the Subject field, now write the e-mail content in the Body field(only plain text is supported), if you need to send attachments just click the Add button in the right of the Attachments field, when your all done just press the Start Sending button and get a coffee.
P.S. the Mail List field is just to show you the complete list of e-mails.

2 comments:

  1. Nice post (and very nice blog).

    Another effective and easy way to do this is to use the open source Blat e-mailer. Simplest is to just run the executable version via WinExec or CreateProcess. There is also a DLL version where you can directly access the SMTP fuctionality with a little more control.

    http://www.blat.net/

    Steve Taylor

    ReplyDelete
  2. Thank you Steve,
    this is just a tutorial in how to develop your own automation process.
    I needed something like this some time ago for some customers and couldn't find a freeware tool(I was in a hurry) so I made this.

    ReplyDelete

Blogroll(General programming and Delphi feeds)