Fun with code (since 2006)

SMTP Mailer 0.9

10.10.06 Posted in Actionscript 3, ByteArray, Socket by

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 ! : 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. : 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. : This class is included in the corelib package, it takes an incoming BitmapData and encode it as a JPEG stream. : This class is included in the corelib package, this version had been optimised by Patrick Mineault from, 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

182 Responses to “SMTP Mailer 0.9”

  1. Mathieu says:

    Hi j DAPOT…

    You talkin’ to me ? (cf. Travis Bickle)
    Regarding my TLS/SSL smtp server question ?

    If so, maybe you should ask Thibault to put your updated sources somewhere in this article, thus anyone could get it.

    If not… well have a good day.

  2. vkwave says:

    I am not able to send attachment. Plan html mail is sending successfully. It is tracing me suspicious message. I am trying to send a mp3 file as an attachment using flex4+air. Any hint to resolve this issue. Please guide.


  3. Jürgen says:

    Hi Thibault,

    in the Development Environment of Flex 4.1, the SMTPMailer works fine with different accounts on and
    But when I export a release build and start this build, I will get the following error:
    Error #2044: securityError unverarbeitet. text=Error #2048: Verletzung der Sicherheits-Sandbox: file:///C:/Temp/mailrel/Mailtest.swf kann keine Daten von laden.
    Any idea what’s the difference between development and release?

    Kind regards


  4. hungconcon says:

    Help me pls, I recently created an AS3 API Mail, if the compilation and implementation of environmental applications then it is possible to send , but if you publish the Web and send mail it not go. is it necessary crossdomain?
    I try, protocol tls.
    Everybody help me?
    Thanks in advance

  5. Helder Pereira says:

    var pDestArray:Array = pDest.split(“,”)
    for (var i:int = 0; i < pDestArray.length; i++)
    if (pDestArray[i] != "")
    writeUTFBytes("RCPT TO: \r\n”);

  6. ctmanic says:

    Can you send a pdf as an attachment?

  7. Punit says:

    I am having problem in connecting to server. SMTPEvent.CONNECTED is never triggered. Please help me.

  8. Palanivelrajan says:

    i want to send mail automatically from my air application..what code i have to use for that…

  9. Punit says:


    I am conneting to “” at port 587, but while authenticating i am getting the mailError as
    mailError at your service, []

    250-SIZE 35882577




    Help me.


  10. Gagi says:

    I am aware this might be too boring a question, but I have to ask man… how do I use SMTPMailer, anyway? some code sample would be greatly appreciated, or a link to where a sample might be found. I can’t seem to google successfully for it. I swear I’ll put the sample on my modest blog for others to find if someone gives us a sample. Thank you.

  11. Gagi says:

    hey there again,
    It turned out to be easier than I feared it is to use Thibault’s class. For everyone that still has to figure out, here it is:

    //instantiate extract the files after download somewhere where your project can find them. perhaps in src folder… I’m using flex
    //then, instantiate SMTPMailer
    var mailer:SMTPMailer = new SMTPMailer(smtpServerAddress, serversListeningPort);
    //authenticate = sign in = log in
    mailer.authenticate(user, password);
    //build and send message
    mailer.sendHTMLMail(sender, receiver, subject, message);

  12. Gagi says:

    What I can’t seem to figure out yet is how to send attachments? Specifically, images and cvs files. Would anyone please help me. Thank you in advance. sample code appreciated greatly.

  13. Gagi says:

    it worked all fine. I did it using Ronald Ruly’s code that can be found amongst these comments.

    Thibault, merci beaucoup pour les code.

    My problem happened to be the fact that I was using version 0.6. Soon as I used version 0.9, everything worked just perfectly.

    thank you Thibault once again.

  14. Windsor says:

    Howdy and thanks for sharing this! I’ve been able to incorperate this into a larger system and it all worked beautifully when I tested it locally, but when I loaded it on the server, it did not send an email.

    I placed a call to my online Trace system in each of the functions: SMTPEvent.MAIL_SENT/ MAIL_ERROR / CONNECTED / AUTHENTICATED… so I could see what it was doing. Locally, it provided the same feedback trace output does. Online I got nothing, no errors, no connection, nothing from the SMTP Mailer system.

    Any idea of what I should try next?


  15. Hey,

    Bytearray is very cool and I keep ending up at it. This time I’m trying to use your smtpmailer class on Adobe Air for iOS but I get no errors or confirmation in any listeners and myMailer.connected reports false. This is same for running from Flash IDE or when debugging straight on the iPad. Using air 3.0 so the bugs that existed with sockets should be fixed (says so in change log). What annoys me is nothing gets triggered at all, not one listener.

  16. I went with another library with starttls for gmail, i think the problem wasn’t ios but gmail .. thanks anyways

  17. Uli Janzen says:

    Hi Thibault,

    thanks for sharing that great piece of code.

    Deepak Nayak already asked for multiple attachments. Do you already have a solution for that. You mentioned you are willing to implement that. This would really help me a lot.

    Thanks in advance, Uli

  18. Vishwas says:

    I get this error :
    Error :

    554 5.7.1 : Client host rejected: Access denied

    Code :


    I don’t know where to go from here ?

  19. Vishwas says:

    I now get a huge output ( probably bitmap data ) like this :
    ÿb¯ø`ÏøRÿðº?áYÅËÿ†£ÿ…‹ÿ×ü+¯Œ_¾,țÿ ëàOííøA?°?äjҿ³ÿµµӾÃý›xÿÙ,

    But mail never goes from my gmail account to another gmail account. I even don’t get any error

  20. Iris says:

    Is there anyway that this works in a Flex Mobile AS3 Project??

  21. sstauross says:

    How can we send mail via Gmail is this solved?? please help!

  22. Andrew Worthington says:

    Error :




    334 VXNlcm5hbWU6

    334 UGFzc3dvcmQ6

    451 Internal error.


    250 Sender accepted.

    553 Sorry, that domain isn’t in my list of allowed rcpthosts.

    503 You must send RCPT TO: first

    this make any sense to anyone?

  23. Nicky says:

    How Can we know, no of new email in my inbox of(Gmail,yahoo,AOL), and also how can we access inbox in our Adobe AIR App.
    Please assist me asap.

  24. Don says:

    Hi Thibault,

    Noob here. I’m trying to use your SMTP Mailer for a simple project of mine on AIR for Android. I can connect to the server but there is something wrong with the authentication. I get notifications – Authentication OK! then Authentication error.

    What seems to be causing this?

  25. Smauz says:

    Hi Thibault,
    I have the same problem of others.. if I deploy the swf with the library, the mail in not sent and there been sent the well known message :”Error #2044: Unhandled SecurityErrorEvent:. text=Error #2048: …. Security Sandbox Violation ..”. How to solve this? thanks


  26. Cellina says:

    Hi, I use the SMTPMailer to send a mail from gmail, but it doesn’t work. can you give me some hint? the code is following:
    var m:model.utils.smtp2.mailer.SMTPMailer = new SMTPMailer(“”,465);

  27. Steave says:

    Hi Thibault,
    First of all thank you for sharing your great work.I’m trying to use your SMTP Mailer for a simple project of mine on AIR.I succeed to get connected with SMTP server & authentication,but while using sendHTMLMail(),somehow connection get closed.

    Any idea of what should i try?

  28. Is this still an active project? need to send a screen grab from an air app without php or files. this mostly works, sometimes it hangs on the auth. has there been an update at all ? and the hyphen thing should i fix it? Thnx

  29. Tim says:

    Hey Thibault!
    First of all, thank you for this library! Very helpful in getting my attachments added on when using mailgun, as HTTPService does not support multipart/form-data.

    Secondly, there is a small error in your code for the date in the SMTPMailer class:
    writeUTFBytes (“Date : “+new Date().toString()+”\r\n”);
    There should be no space between the ‘Date’ and the colon. This caused errors for server to parse the MIME message.
    When I deleted it and used:
    writeUTFBytes (“Date: “+new Date().toString()+”\r\n”);
    it parsed correctly.

    Again, thank you for this great library. If it is ok with you, I would like to write a blog post with instructions for integrating this with AlivePDF for runtime generated PDF distribution via email. Please let me know!

  30. Andrew says:

    You mentioned using this with Mailgun.
    Do you mind sharing what needed to be done to make it work?

  31. GeorgeM says:

    Hi. I got this to work on Flash Builders 4.6 emulator, and it sends my attached file with no problem, but when I install the app on the IPhone it wont send the email. Any suggestions
    (I am using a gmail account)

Leave a Reply

Open Sort Options

Sort comments by:
  • * Applied after refresh
Fun with code since 2006.