Forum Stats

  • 3,727,639 Users
  • 2,245,430 Discussions
  • 7,852,905 Comments

Discussions

Email Using Gmail SMTP

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited August 2017 in New To Java

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

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited August 2017 Accepted 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:

    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 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

    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: <[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

    D:\>

    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

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited August 2017

    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);

    ...

  • Unknown
    edited August 2017
    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.

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited August 2017

    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.

    D:\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 true

    DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 587

    Do you see something like the above? Please post your trace.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited August 2017

    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.

    D:\java prog\2017Self\email>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,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)

    D:\>

    D:\>echo %CLASSPATH%

    D:\download\mysql-connector-java-5.0.8-bin.jar;D:\download\jxl.jar;D:\download\javamail-1.4.7\lib\mailapi.jar;.

    D:\>

    D:\>echo %CLASSPATH%

    D:\download\mysql-connector-java-5.0.8-bin.jar;D:\download\jxl.jar;D:\download\javamail-1.4.7\lib\mailapi.jar;.

    D:\>

    Somebody please guide me.

    Zulfi.

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited August 2017
    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.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited August 2017

    Hi,

    I have included SMTP.jar in my CLASSPATH but i am now getting following error:

    D:\l>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 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

    D:\>cd D:\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:

    D:\>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:\>

    Some body please guide me.

    Zulfi.

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited August 2017

    check the link

    https://stackoverflow.com/questions/1990454/using-javamail-to-connect-to-gmail-smtp-server-ignores-specified-port-and-tr…

    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>
  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited August 2017

    First, change your code to look like ...

    Transport tr = session.getTransport("smtps");    tr.send(message);

    and test.

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited August 2017

    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
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited August 2017 Accepted 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:

    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 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

    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: <[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

    D:\>

    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.

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited August 2017

    Glad, you were able to figure it out yourself. Just to remind you, the code you posted contains your password.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited August 2017

    Thanks for your help. I would take necessary action.

    Zulfi.

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited August 2017

    Please make sure to change that password immediately if you haven't done so yet.

    If any of our responses helped you to find the answer to your question, please mark them as Helpful by clicking on the Helpful link.

This discussion has been closed.