title: Unable to send mail using java mail client
body: <p>I've looked over the previous links on SO concerning the matter but they don't solve my problem.</p>
  
  <p>So here is my code:</p>
  
  <pre><code>public class SendMail {
  
      private String from;
      private String to;
      private String host;
      private String subject;
      private String message;
      private String pass;
  
      private static final Logger LOGGER = Logger.getLogger(SendMail.class.getName());
  
      public SendMail(String from, String pass, String to, String host, String subject, String message) {
          super();
          this.from = from;
          this.to = to;
          this.host = host;
          this.subject = subject;
          this.message = message;
          this.pass = pass;
      }
  
      public void sendMail(){
          Properties properties = System.getProperties();
          final String USERNAME = from;
          final String PASSWORD = pass;
  
          properties.put("mail.smtp.user", USERNAME);
          properties.put("mail.smtp.host", "smtp.gmail.com");
          properties.put("mail.smtp.port", "25");
          properties.put("mail.debug", "true");
          properties.put("mail.smtp.auth", "true");
          properties.put("mail.smtp.starttls.enable", "true");
          properties.put("mail.smtp.EnableSSL.enable", "true");
          properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
          properties.setProperty("mail.smtp.socketFactory.fallbac k", "false");
          properties.setProperty("mail.smtp.port", "465");
          properties.setProperty("mail.smtp.socketFactory.port", "465");
  
          Session session = Session.getInstance(properties, new javax.mail.Authenticator(){
              protected PasswordAuthentication getPasswordAuthenticated(){
                  return new PasswordAuthentication(USERNAME, PASSWORD);
              }
          });
  
  
          //properties.setProperty("mail.imap.ssl.enable", "true");
          //properties.put("mail.debug", "true");
          //Session session = Session.getInstance(properties);
  
          try {
              //Store store = session.getStore("imap");
              //store.connect(host, USERNAME, PASSWORD);
  
              MimeMessage mime = new MimeMessage(session);
              mime.setFrom(new InternetAddress(USERNAME));
              mime.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
              mime.setSubject(subject);
              mime.setText(message);
              Transport.send(mime);
              LOGGER.log(Level.INFO, "Mail sent successfully");
          } catch (MessagingException e) {
              // TODO Auto-generated catch block
              LOGGER.log(Level.SEVERE, "Unable to send mail\n");
          }   
      }
  
  }
  </code></pre>
  
  <p>And here are the logs:</p>
  
  <pre><code>DEBUG: JavaMail version 1.4.4
  DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
  DEBUG: Tables of loaded providers
  DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
  DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
  DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
  DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
  DEBUG SMTP: useEhlo true, useAuth true
  Dec 30, 2015 4:51:31 PM utility.mailclient.SendMail sendMail
  SEVERE: Unable to send mail
  </code></pre>
  
  <p>Also, the commented part in the above code is taken from this link:</p>
  
  <p><a href="http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail" rel="nofollow">http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail</a></p>
  
  <p>which is supposed to be correct. But I'm getting the same result. Here are the logs using this code (and commenting out all the <code>properties.put</code> and <code>properties.setProperty</code> lines above)</p>
  
  <pre><code>DEBUG: JavaMail version 1.4.4
  DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
  DEBUG: Tables of loaded providers
  DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
  DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
  DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
  DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
  DEBUG: mail.imap.fetchsize: 16384
  DEBUG: mail.imap.statuscachetimeout: 1000
  DEBUG: mail.imap.appendbuffersize: -1
  DEBUG: mail.imap.minidletime: 10
  DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL true
  * OK Gimap ready for requests from 182.75.40.98 j10mb367075283iee
  A0 CAPABILITY
  * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH
  A0 OK Thats all she wrote! j10mb367075283iee
  DEBUG IMAP: AUTH: XOAUTH2
  DEBUG IMAP: AUTH: PLAIN
  DEBUG IMAP: AUTH: PLAIN-CLIENTTOKEN
  DEBUG IMAP: AUTH: OAUTHBEARER
  DEBUG IMAP: AUTH: XOAUTH
  DEBUG: protocolConnect login, host=imap.gmail.com, user=mailid04@gmail.com, password=&lt;non-null&gt;
  A1 AUTHENTICATE PLAIN
  + 
  AGF0dWxzcGFtMDRAZ21haWwuY29tAGF0dWxzcGFt
  * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS
  A1 OK mailid@gmail.com authenticated (Success)
  A2 CAPABILITY
  * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS
  A2 OK Success
  DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
  DEBUG SMTP: useEhlo true, useAuth false
  DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
  Dec 30, 2015 5:11:24 PM utility.mailclient.SendMail sendMail
  SEVERE: Unable to send mail
  </code></pre>
  
  <p>PS: I've turned on the check of gmail which allows less secure apps to access it.</p>