일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 서버운영
- reactor
- 웹 커리큘럼
- 웹앱
- Spring Batch
- ipTIME
- reactor core
- spring reactive
- 공유기 서버
- Spring Framework
- reactive
- 웹 스터디
- Today
- Total
Hello World
Sending a mail in Java (and Android) with Apache Commons Net SMTP : STARTTLS, SSL 본문
Sending a mail in Java (and Android) with Apache Commons Net SMTP : STARTTLS, SSL
EnterKey 2016. 1. 12. 16:46Recently 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
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) :
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 !
출처: http://www.javacodegeeks.com/2013/11/sending-a-mail-in-java-and-android-with-apache-commons-net-smtp-starttls-ssl.html?utm_content=buffer7de2c&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer
'Java > Core' 카테고리의 다른 글
ElasticSearch Tutorial for Beginners (0) | 2016.01.12 |
---|---|
10 Examples of using ArrayList in Java (0) | 2016.01.12 |
Unirest for Java (0) | 2016.01.12 |
자바 Primitive Type과 Boxed Primitives 둘 중 무엇을 사용할까? (0) | 2016.01.11 |
Java Reflection Tutorial (0) | 2016.01.11 |