Anthony Dahanne's blog

« | »

Sending a mail in Java (and Android) with Apache Commons Net SMTP : STARTTLS, SSL

Recently working on an Android experiment, I wanted to send emails using a SMTP server, using authentication and encryption, from an android app.

Well, I found out that javax.mail on Android is not a really good option, since it depends on awt classes (legacy I guess) ; some people have tried to adapt it so that you don’t require the whole awt package, but I had little success with that; not mentioning those people have refactored javax.mail for Android few years ago themselves, without any maintenance.

Another option that came to my mind is re using Apache Commons Net : since the community added an SMTPSClient and an AuthenticatingSMTPClient to the original SMTP client (and applied a little patch of mine for SSL and authentication), you can embed this library in your Android app (no transitive dependencies needed) to send mail using authentication over a secured layer. (this post actually inspired me, but it is using an old version of Apache Commons Net, using 3.3 you don’t need to do that anymore)

SMTP Authentication and STARTTLS with Commons Net

Usually the port used for this matter is 25 or the alternate 587 port : you connect to the SMTP server on a  plain connection, you ask for the available commands, if STARTTLS is supported, you use it and the rest of the communication is encrypted.

Let’s take the gmail example, since supports authentication and STARTTLS

Nothing much to add here, of course the exception handling could be optimized if you used your own exception classes.

SMTP Authentication and SSL with Commons Net

Some SMTP servers are configured to only accept “a to z SSL” : you have to secure the communication right before issuing any commands to the server; usually the port used is 465.

Let’s take the example (free email accounts offered by the french post) :

I did not repeat the checkReply() method here, since it is the same for both code snippets; you will have noticed that using SSL right away means you don’t have to check for execTls() response (in fact it won’t work if you do so).

Wrapping up

That’s about it; if you want to make those examples work in your environment, you can add the apache commons net 3.3 jar to your classpath

If you’re using Maven add the dependency  :

If you’re using Gradle for your Android project, you can also use the following build.gradle file :

Enjoy !

Posted by on Lundi 17 juin 2013.

Categories: android, java, open source

8 Responses

  1. Hello,

    you code works nicely!! (instead of javax one which doesn’t work on jelly bean!)

    Is it possible to add an attachment with this method?
    Thank you so much.

    Best Regards

    by Emanuele on juin 21, 2013 at 12:47

  2. Hi, client.connect throws an exeption… really stuck. Any help would be greatly appreciated.

    by Yeglan on juil 17, 2013 at 15:57

  3. Hi, same error as Yeglan… what could it be?!

    by Alessandro on juil 26, 2013 at 06:33

  4. To resolve error just add




    right beforewriter obtain.

    by Alexander Kazakov on août 16, 2013 at 01:17

  5. Sorry, misspelled, right code cut:

    client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN, username, password);


    // Add this to resolve Exception

    Writer writer = client.sendMessageData();

    by Alexander Kazakov on août 16, 2013 at 01:19

  6. Hey, thanks for the code! client.connect still throws an error and I don’t really understand your comment how to fix it ..? would be grateful for help :-)

    by Phil on déc 11, 2013 at 10:27

  7. .. I got the solution myself .. one has to use an AsyncTask.

    by Phil on déc 12, 2013 at 08:16

  8. What if I want to start sending emails including attachments instead of just writing text to the Writer.

    by Jelle on déc 13, 2013 at 07:45

Leave a Reply

5 + = 10

« | »

Articles récents


About Anthony Dahanne's blog

Since I got my engineer degree (2005), I have been working on several missions (as a software developer, or a trainer) involving Java technologies from JEE to OSGi, including Android, GWT and even sometimes with Ruby / Ruby on Rails. Those missions touched business areas as different as banking (SGCIB, Crédit Agricole S.A.), travel (Club […]more →