SMTPMailer logo

During my AS3 session at the Flash Festival 2006 in Paris, i presented an application sending attached files through emails, with the Socket class available in the Player 9 and AS3.

A lot of people were asking for the source code to do the job, here is the complete source code for Flash CS3, here is a little description of what's inside :)

Just drop me an email, if you use it in an application, that would be cool, i'd love seeing it in some projects !

Mailer.as : This class deals with the SMTP socket server, 2 methods are available for the moment (sendAttachedMail and sendHTMLMail) the first one sends an email with an attached file (picture or any kind of file), the second one sends only a simple HTML email.

Base64.as : To send attached files through the wire, you have to encode binary data to Base64 string which is the best solution for compatibilities with SMTP servers, the base64 string is decoded as binary by your mail manager.

JPEGEncoder.as : This class is included in the corelib package, it takes an incoming BitmapData and encode it as a JPEG stream.

PNGEnc.as : This class is included in the corelib package, this version had been optimised by Patrick Mineault from 5etdemi.com, it takes an incoming BitmapData and encode it as a PNG stream.

Update : In my example, a snapshot of the stage is done, of course you can pass any display objects (Video, MovieClip) to get captures from a webcam for instance. In the .fla demo, the Mailer class attaches picture files.

Update : 31/10/06 : Version 0.2 added MD5 encoding for boundary string

Update : 12/10/06 : Version 0.3 added the capability to send PNG, JPEG image files, appropriate image headers are auto-added, just pass now the image name without the extension, the Mailer class will read the header and add the extension for you and the appropriate headers, sources have been modified ;)

Update : 17/02/07 : Version 0.4 is dispatching proper events, added following events :

// event dispatched when mail is successfully sent
myMailer.addEventListener(SMTPEvent.MAIL_SENT, onMailSent);
// event dispatched when mail could not be sent
myMailer.addEventListener(SMTPEvent.MAIL_ERROR, onMailError);
// event dispatched when SMTPMailer successfully connected to the SMTP server
myMailer.addEventListener(SMTPEvent.CONNECTED, onConnected);
// event dispatched when SMTP server disconnected the client for different reasons
myMailer.addEventListener(SMTPEvent.DISCONNECTED, onDisconnected);

Update : 24/06/07 : Version 0.5 handles every kind of files for attachment (Content-Type: application/octet-stream) has been added for all kind of binary files. A few bugs have been fixed (thanks Marc), next version will include authentification.

Update : 03/07/07 : Version 0.6 handles authentification (thanks Wein Pearson). To authenticate, just call the authenticate method.

Update : 04/08/08 : Version 0.7 fixes some little bugs.

Update : 04/16/09 : Version 0.8 fixes one nasty bug and QMail support.

Update : 02/02/10 : Version 0.9 was refactored.

// event dispatched when the client has authenticated successfully
myMailer.addEventListener(SMTPEvent.AUTHENTICATED, onAuthSuccess);
// event dispatched when the client could not authenticate
myMailer.addEventListener(SMTPEvent.BAD_SEQUENCE, onAuthFailed);
// pass a username and password
myMailer.authenticate ("bob", "bob");

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.

Sources available here