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 smtp.gmail.com supports authentication and STARTTLS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
public void sendEmail() throws Exception { String hostname = "smtp.gmail.com"; int port = 587; String password = "gmailpassword"; String login = "account@gmail.com"; String from = login; String subject = "subject" ; String text = "message"; AuthenticatingSMTPClient client = new AuthenticatingSMTPClient(); try { String to = "recipient@email.com"; // optionally set a timeout to have a faster feedback on errors client.setDefaultTimeout(10 * 1000); // you connect to the SMTP server client.connect(hostname, port); // you say ehlo and you specify the host you are connecting from, could be anything client.ehlo("localhost"); // if your host accepts STARTTLS, we're good everything will be encrypted, otherwise we're done here if (client.execTLS()) { client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN, login, password); checkReply(client); client.setSender(from); checkReply(client); client.addRecipient(to); checkReply(client); Writer writer = client.sendMessageData(); if (writer != null) { SimpleSMTPHeader header = new SimpleSMTPHeader(from, to, subject); writer.write(header.toString()); writer.write(text); writer.close(); if(!client.completePendingCommand()) {// failure throw new Exception("Failure to send the email "+ client.getReply() + client.getReplyString()); } } else { throw new Exception("Failure to send the email "+ client.getReply() + client.getReplyString()); } } else { throw new Exception("STARTTLS was not accepted "+ client.getReply() + client.getReplyString()); } } catch (Exception e) { throw e; } finally { client.logout(); client.disconnect(); } } private static void checkReply(SMTPClient sc) throws Exception { if (SMTPReply.isNegativeTransient(sc.getReplyCode())) { throw new Exception("Transient SMTP error " + sc.getReply() + sc.getReplyString()); } else if (SMTPReply.isNegativePermanent(sc.getReplyCode())) { throw new Exception("Permanent SMTP error " + sc.getReply() + sc.getReplyString()); } } |
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 LaPoste.net example (free email accounts offered by the french post) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public void sendEmail() throws Exception { String hostname = "smtp.laposte.net"; int port = 465; String password = "password"; String login = "firstname.lastname"; String from = login + "@laposte.net"; String subject = "subject" ; String text = "message"; // this is the important part : you tell your client to connect using SSL right away AuthenticatingSMTPClient client = new AuthenticatingSMTPClient("TLS",true); try { String to = "anthony.dahanne@gmail.com"; // optionally set a timeout to have a faster feedback on errors client.setDefaultTimeout(10 * 1000); client.connect(hostname, port); client.ehlo("localhost"); client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN, login, password); checkReply(client); client.setSender(from); checkReply(client); client.addRecipient(to); checkReply(client); Writer writer = client.sendMessageData(); if (writer != null) { SimpleSMTPHeader header = new SimpleSMTPHeader(from, to, subject); writer.write(header.toString()); writer.write(text); writer.close(); if(!client.completePendingCommand()) {// failure throw new Exception("Failure to send the email "+ client.getReply() + client.getReplyString()); } } else { throw new Exception("Failure to send the email "+ client.getReply() + client.getReplyString()); } } catch (Exception e) { throw e; } finally { client.logout(); client.disconnect(); } } |
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 :
1 2 3 4 5 |
<dependency> <groupid>commons-net</groupid> <artifactid>commons-net</artifactid> <version>3.3</version> </dependency> |
If you’re using Gradle for your Android project, you can also use the following build.gradle file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.4.2' } } apply plugin: 'android' repositories { mavenCentral() } dependencies { compile fileTree(dir: 'libs', include: '*.jar'), 'commons-net:commons-net:3.3' } android { compileSdkVersion 17 buildToolsVersion "17.0.0" sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } instrumentTest.setRoot('tests') } } |
Enjoy !
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
Hi, client.connect throws an exeption… really stuck. Any help would be greatly appreciated.
Hi, same error as Yeglan… what could it be?!
To resolve error just add
client.addRecipient(to);
checkReply(client);
after
client.addRecipient(to);
checkReply(client);
right beforewriter obtain.
Sorry, misspelled, right code cut:
….
client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN, username, password);
checkReply(client);
client.setSender(username);
checkReply(client);
// Add this to resolve Exception
client.addRecipient(aTo);
checkReply(client);
Writer writer = client.sendMessageData();
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
.. I got the solution myself .. one has to use an AsyncTask.
What if I want to start sending emails including attachments instead of just writing text to the Writer.
The following exception was thrown:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
For those asking about attachments, here’s an example of using commons-net’s SmtpClient to send an e-mail with attachments
Trying the first option to gmail.com
In logcat I get the error message MalformedServerRepleyException: could not parse response code.
How can I solve that?
Thank you. This example helped a lot.
535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials k4sm5974537wmk.26 – gsmtp
i got this error some body guide me