Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Email program in Java using Gmail SMTP  RSS feed

 
Zulfi Khan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to send email using gmail SMTP. I got the following code from internet but I am getting following run-time error.
Kindly guide:


Error Message is:



Exception in thread "main" java.lang.RuntimeException: javax.mail.NoSuchProviderException: smtp
        at SendMailTLS2.main(SendMailTLS2.java:46)
Caused by: javax.mail.NoSuchProviderException: smtp
        at javax.mail.Session.getService(Session.java:792)
        at javax.mail.Session.getTransport(Session.java:728)
        at javax.mail.Session.getTransport(Session.java:668)
        at javax.mail.Session.getTransport(Session.java:648)
        at javax.mail.Session.getTransport(Session.java:705)
        at javax.mail.Transport.send0(Transport.java:192)
        at javax.mail.Transport.send(Transport.java:124)
        at SendMailTLS2.main(SendMailTLS2.java:41)
 
Ron McLeod
Saloon Keeper
Posts: 1527
210
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enable debugging using session.setDebug(true); and compare to what I saw when I ran your code.



 
Zulfi Khan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am getting following out after debugging:

D:>java SendMailTLS2
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.7]
DEBUG: Exception loading provider, THROW:
java.lang.ClassNotFoundException: com.sun.mail.smtp.SMTPTransport
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at javax.mail.Session.getService(Session.java:788)
        at javax.mail.Session.getTransport(Session.java:728)
        at javax.mail.Session.getTransport(Session.java:668)
        at javax.mail.Session.getTransport(Session.java:648)
        at javax.mail.Session.getTransport(Session.java:705)
        at javax.mail.Transport.send0(Transport.java:192)
        at javax.mail.Transport.send(Transport.java:124)
        at SendMailTLS2.main(SendMailTLS2.java:41)
Exception in thread "main" java.lang.RuntimeException: javax.mail.NoSuchProviderException: smtp
        at SendMailTLS2.main(SendMailTLS2.java:46)
Caused by: javax.mail.NoSuchProviderException: smtp
        at javax.mail.Session.getService(Session.java:792)
        at javax.mail.Session.getTransport(Session.java:728)
        at javax.mail.Session.getTransport(Session.java:668)
        at javax.mail.Session.getTransport(Session.java:648)
        at javax.mail.Session.getTransport(Session.java:705)
        at javax.mail.Transport.send0(Transport.java:192)
        at javax.mail.Transport.send(Transport.java:124)
        at SendMailTLS2.main(SendMailTLS2.java:41)

D:>



I am again putting the code. Note asterisk means that i have hidden the actual text for security reasons:



Zulfi.
 
Ron McLeod
Saloon Keeper
Posts: 1527
210
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well .. it looks like com.sun.mail.smtp.SMTPTransport cannot be found.  It would normally be in the same jar file as javax.mail classes.  How are you building the application?
 
Zulfi Khan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your reply. Your are right. I have included smtp.jar but now i ma getting following error:



D:>java SendMailTLS3
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 smtp.gmail.com ESMTP p83sm6345650wmf.38 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO HP-PC
250-smtp.gmail.com at your service, [119.157.246.113]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
        at javax.mail.Service.connect(Service.java:317)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:125)
        at javax.mail.Transport.send0(Transport.java:194)
        at javax.mail.Transport.send(Transport.java:124)
        at SendMailTLS3.main(SendMailTLS3.java:42)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
        at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478)
        at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)
        at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
        at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
        at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
        at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486)
        at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
        ... 7 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
        at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
        at sun.security.validator.Validator.validate(Validator.java:260)
        at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
        at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
        at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460)
        ... 17 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
        at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
        at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
        ... 23 more




What other jar files  do i have to include in the CLASSPATH ??? Kindly reply me.


The directory shows other jar files:

D:\download\javamail-1.4.7\lib>dir
Volume in drive D has no label.
Volume Serial Number is B269-AF38

Directory of D:\download\javamail-1.4.7\lib

03/06/2013  04:19 PM    <DIR>          .
03/06/2013  04:19 PM    <DIR>          ..
03/06/2013  04:18 PM            19,171 dsn.jar
03/06/2013  04:17 PM            14,769 gimap.jar
03/06/2013  04:17 PM           181,492 imap.jar
03/06/2013  04:16 PM           279,893 mailapi.jar
03/06/2013  04:17 PM            44,405 pop3.jar
03/06/2013  04:17 PM            52,869 smtp.jar
               6 File(s)        592,599 bytes
               2 Dir(s)  565,493,882,880 bytes free

D:\download\javamail-1.4.7\lib>

Zulfi.
 
Ron McLeod
Saloon Keeper
Posts: 1527
210
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would just use a single jar like JavaMail 1.4.7 which contains the API and the providers.
 
Ron McLeod
Saloon Keeper
Posts: 1527
210
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed being caused by anti-virus applications that generate fake certificates to allow inspection of TLS traffic.  If you have an anti-virus application running, try disabling it and see if that helps.
 
Zulfi Khan
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thanks Ron McLeod. Your advice was correct.

Hi,
Thanks for your reply. Actually the prob was related to anti virus running on my machine. I have to disable it, But still it did not run and i got following authentication message:

D:\java prog\2017Self\email>java SendMailTLS3
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 smtp.gmail.com ESMTP 52sm5621520wrt.38 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587


EHLO HP-PC
250-smtp.gmail.com at your service, [119.153.34.57]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO HP-PC
250-smtp.gmail.com at your service, [119.153.34.57]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtE
534-5.7.14 vsqdQl3Ab4ew0xIjU53ikGi2Mulx2Kpm8VDoJUuPRxRsYcXObtg2FxGUa22IDalwLi92Ft
534-5.7.14 LEZX8JRDtfMveTDZ2GhEQcQ91RGm0GzbVZVfnoef5V_5nQf_4z3Ei9iIPn0uMBeyrVwSkw
534-5.7.14 EWf8vnh-8SS5eYY3h251PT3crYDSzBVFdDjwhxdihxO8Y4TqXwF7fb7lJY-yquNLVhHigC
534-5.7.14 6TJPZ2CJxElot746cBuJVjPu4LfEg> Please log in via your web browser and
534-5.7.14 then try again.

534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 52sm5621520wrt.38 - gsmtp


        at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
        at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
        at javax.mail.Service.connect(Service.java:317)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:125)
        at javax.mail.Transport.send0(Transport.java:194)
        at javax.mail.Transport.send(Transport.java:124)
        at SendMailTLS3.main(SendMailTLS3.java:42)
  


===
However there was an important hint which i have highlighted above:

So i logged in and found an email in my account with subject:

Review blocked sign-in attempt


after that , i have to give permission to Less secure app.

However it did not run even after that because i was sending email to my yahoo account. So i send email to my gmail account and it worked. Then i tried sending email to my yahoo account and it worked too. Final output is:

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 smtp.gmail.com ESMTP 5sm2859057wre.5 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587


EHLO HP-PC
250-smtp.gmail.com at your service, [119.153.34.57]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO HP-PC
250-smtp.gmail.com at your service, [119.153.34.57]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<***@gmail.com>
250 2.1.0 OK 5sm2859057wre.5 - gsmtp
RCPT TO:<****@yahoo.com>
250 2.1.5 OK 5sm2859057wre.5 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   ****@yahoo.com
DATA
354  Go ahead 5sm2859057wre.5 - gsmtp
From: ****@gmail.com
To: *****@yahoo.com
Message-ID: <19689154.0.1502559615978.JavaMail.HP@HP-PC>
Subject: Testing Subject
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


Dear Mail Crawler,


No spam to my email, please!
.
250 2.0.0 OK 1502559635 5sm2859057wre.5 - gsmtp
QUIT
221 2.0.0 closing connection 5sm2859057wre.5 - gsmtp
Done


AGAIN

D:\j>javac SendMailTLS3.java


D:\java prog\2017Self\email>java SendMailTLS3
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 smtp.gmail.com ESMTP x14sm4540009wmd.19 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587


EHLO HP-PC
250-smtp.gmail.com at your service, [119.153.34.57]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO HP-PC
250-smtp.gmail.com at your service, [119.153.34.57]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<*****@gmail.com>
250 2.1.0 OK x14sm4540009wmd.19 - gsmtp
RCPT TO:<******@gmail.com>
250 2.1.5 OK x14sm4540009wmd.19 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   *****@gmail.com
DATA
354  Go ahead x14sm4540009wmd.19 - gsmtp
From: *****@gmail.com
To: *****@gmail.com
Message-ID: <19689154.0.1502560213747.JavaMail.HP@HP-PC>
Subject: Testing Subject
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


Dear Mail Crawler,


No spam to my email, please!
.
250 2.0.0 OK 1502560233 x14sm4540009wmd.19 - gsmtp
QUIT
221 2.0.0 closing connection x14sm4540009wmd.19 - gsmtp
Done


D:\>
and the program is:
/* after removing the try catch block */



Thanks all.

Zulfi.
 
Knute Snortum
Sheriff
Posts: 3833
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for getting back to us and posting your solution.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!