Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Email Using Gmail SMTP

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:
<
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)
>
My code is:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS2 {
public static void main(String[] args) {
final String username = "***@gmail.com";
final String password = "&&";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("**@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Some body please guide me.
Zulfi.
Best Answer
-
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:
\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 hind 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: <[email protected]>
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
\j>javac SendMailTLS3.java
\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: <[email protected]>
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
\>
and the program is:
/* after removing the try catch block */
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS3 {
public static void main(String[] args) {
final String username = "****@gmail.com";
final String password = "*****";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("****@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("*****@yahoo.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
}catch (MessagingException e) {
//throw new RuntimeException(e);
e.printStackTrace();
}
}
}
Thanks all.
Zulfi.
Answers
-
enabling the debug mode on the mail session may help you.
...
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
session.setDebug(true);
Message message = new MimeMessage(session);
...
-
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:<Exception in thread "main" java.lang.RuntimeException: javax.mail.NoSuchProviderException: smtp . . .at SendMailTLS2.main(SendMailTLS2.java:41)>
Well YOU are the ONLY one that knows what line in your code is line 41. As you can see above that is the line that caused the exception.
And this in your code says you really do NOT care what exceptions get raised or what causes them.
} catch (MessagingException e) {throw new RuntimeException(e);}
Why do you 'catch' an exception and then totally hide what it is telling you?
Don't catch exceptions unless you plan to handle them. Adding that code just hides the REAL exception info you were given.
Remove that junk from your code and don't put it back.
Then rerun the code and SHOW US:
1. WHAT you do
2. HOW you do it
3. WHAT results you get
That includes posting a copy of the execution that includes the FULL exception and message you get.
But a missing provider message likely means you are missing a jar file that is needed.
-
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;public class Test
{
public static void main(String[] args)
{final String username = "***@gmail.com";
final String password = "&&";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
try
{Session session = Session.getInstance(props,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
});session.setDebug(true);
Message message = new MimeMessage(session);message.setFrom(new InternetAddress("**@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
}
catch (javax.mail.MessagingException e)
{
e.printStackTrace();
}
}
}I ran it using the windows command line - just to make sure the IDE does not add anything to the classpath.
\projects\otn\bin>java -classpath .;d:\lib\mail-lib\mail.jar Test
The top part of the output ...
DEBUG: setDebug: JavaMail version 1.3 (my version of mail.jar is old)
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth trueDEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 587
Do you see something like the above? Please post your trace.
-
Hi,
Thanks everybody for kindly concentrating on my problem. God bless you people.
I have done the following as requested:
- Remove that junk from your code and don't put it back.
Yes
- But a missing provider message likely means you are missing a jar file that is needed.
I have provided the value of CLASSPATH by displaying the variable
- That includes posting a copy of the execution that includes the FULL exception and message you get.
Yes I have posted the full message
session.setDebug(true);
Yes I did this
- Well YOU are the ONLY one that knows what line in your code is line 41. As you can see above that is the line that caused the exception.
I have put the line numbers in the code.
MY code with line numbers is:
1/* after removing the try catch block */
2import java.util.Properties;
3
4import javax.mail.Message;
5import javax.mail.MessagingException;
6import javax.mail.PasswordAuthentication;
7import javax.mail.Session;
8import javax.mail.Transport;
9import javax.mail.internet.InternetAddress;
10import javax.mail.internet.MimeMessage;
11
12public class SendMailTLS3 {
13
14 public static void main(String[] args) {
15
16 final String username = "***@gmail.com";
17 final String password = "*****";
18
19 Properties props = new Properties();
20 props.put("mail.smtp.auth", "true");
21 props.put("mail.smtp.starttls.enable", "true");
22 props.put("mail.smtp.host", "smtp.gmail.com");
23 props.put("mail.smtp.port", "587");
24
25 Session session = Session.getInstance(props,
26 new javax.mail.Authenticator() {
27 protected PasswordAuthentication getPasswordAuthentication() {
28 return new PasswordAuthentication(username, password);
29 }
30 });
31 session.setDebug(true);
32
33 try{
34 Message message = new MimeMessage(session);
35 message.setFrom(new InternetAddress("***@gmail.com"));
36 message.setRecipients(Message.RecipientType.TO,
37 InternetAddress.parse("***@yahoo.com"));
38 message.setSubject("Testing Subject");
39 message.setText("Dear Mail Crawler,"
40 + "\n\n No spam to my email, please!");
41
41 Transport.send(message);
42
42 System.out.println("Done");
43 }catch (MessagingException e) {
44 //throw new RuntimeException(e);
45 e.printStackTrace();
46 }
47
48
49 }
50}
=====
Compilation & output of code.
\java prog\2017Self\email>javac SendMailTLS3.java
\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,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 SendMailTLS3.main(SendMailTLS3.java:42)
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 SendMailTLS3.main(SendMailTLS3.java:42)
\>
\>echo %CLASSPATH%
\download\mysql-connector-java-5.0.8-bin.jar;D:\download\jxl.jar;D:\download\javamail-1.4.7\lib\mailapi.jar;.
\>
\>echo %CLASSPATH%
\download\mysql-connector-java-5.0.8-bin.jar;D:\download\jxl.jar;D:\download\javamail-1.4.7\lib\mailapi.jar;.
\>
Somebody please guide me.
Zulfi.
-
java.lang.ClassNotFoundException: com.sun.mail.smtp.SMTPTransport
Your classpath does not have com.sun.mail.smtp.SMTPTransport class available to load hence the java.lang.ClassNotFoundException.
Edit:
First, download the mail-1.4.7.jar from https://mvnrepository.com/artifact/javax.mail/mail/1.4.7 and add to your classpath.
Then if you need, download smtp.jar from https://mvnrepository.com/artifact/com.sun.mail/smtp/1.4.5
I think you will also need further libraries - activation.jar.
-
Hi,
I have included SMTP.jar in my CLASSPATH but i am now getting following error:
\l>javac SendMailTLS3.java
\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 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
\>cd
\download\javamail-1.4.7\lib
Kindly tell me what other jar files do i have to include in the CLASSPATH???
Following is the directory of jar files:
\>dir
Volume in drive D has no label.
Volume Serial Number is B269-AF38
Directory of
\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
\>
Some body please guide me.
Zulfi.
-
check the link
Your code needs to be adjusted
Copy + Paste:
<span class="typ">Transport</span><span class="pln"> transport </span><span class="pun">=</span><span class="pln"> session</span><span class="pun">.</span><span class="pln">getTransport</span><span class="pun">(</span><strong><span class="str" style="color: #7ed529;">"smtps"</span></strong><span class="pun">);</span><span class="pln"><br/>transport</span><span class="pun">.</span><span class="pln">connect </span><span class="pun">(</span><span class="pln">smtp_host</span><span class="pun">,</span><span class="pln"> smtp_port</span><span class="pun">,</span><span class="pln"> smtp_username</span><span class="pun">,</span><span class="pln"> smtp_password</span><span class="pun">);</span><span class="pln"><br/>transport</span><span class="pun">.</span><span class="pln">sendMessage</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">,</span><span class="pln"> msg</span><span class="pun">.</span><span class="pln">getAllRecipients</span><span class="pun">());</span><span class="pln"><br/>transport</span><span class="pun">.</span><span class="pln">close</span><span class="pun">();</span>
-
First, change your code to look like ...
Transport tr = session.getTransport("smtps"); tr.send(message);
and test.
-
I was able to connect using your code with just the changes in reply #8.
Transport tr = session.getTransport("smtps"); tr.send(message);
output:
Subject: Testing SubjectMIME-Version: 1.0Content-Type: text/plain; charset=us-asciiContent-Transfer-Encoding: 7bitDear Mail Crawler, No spam to my email, please!.250 2.0.0 OK 1502393442 x53sm3929839edd.79 - gsmtpQUIT221 2.0.0 closing connection x53sm3929839edd.79 - gsmtpDone
-
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:
\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 hind 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: <[email protected]>
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
\j>javac SendMailTLS3.java
\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: <[email protected]>
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
\>
and the program is:
/* after removing the try catch block */
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS3 {
public static void main(String[] args) {
final String username = "****@gmail.com";
final String password = "*****";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("****@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("*****@yahoo.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
}catch (MessagingException e) {
//throw new RuntimeException(e);
e.printStackTrace();
}
}
}
Thanks all.
Zulfi.