title: Authentication error in java mail program on openshift
body: <p>I am trying to send mails using Java Mail API &amp; Gmail SMTP server. Here is my code:</p>
  
  <pre><code>final String username = "myusername@gmail.com";
  final String password = "mypassword";
  Properties props = new Properties();
  props.put("mail.smtp.user", username);
  props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.port", "25");
  props.put("mail.debug", "true");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.EnableSSL.enable", "true");
  props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  props.setProperty("mail.smtp.socketFactory.fallback", "false");
  props.setProperty("mail.smtp.port", "465");
  props.setProperty("mail.smtp.socketFactory.port", "465");
  
  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(username));
      message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(clientEmail));
      message.setSubject(subject);
      message.setText("Dear "+clientName+",\n\n  "+body);
  
      Transport.send(message);
  } catch (MessagingException e) {
      throw new RuntimeException(e);
  }
  </code></pre>
  
  <p>It is running perfectly on local but giving AuthenticationFailedException on Openshift. Below is the openshift error log.</p>
  
  <pre><code>DEBUG: JavaMail version 1.4.7
  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,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Orcle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle],com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
  DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
  DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
  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 465, isSSL false 220 mx.google.com       ESMTP k9sm38376122qaj.7 - gsmtp
  DEBUG SMTP: connected to host "smtp.gmail.com", port: 465 EHLO ex-std-node454.prod.rhcloud.com 250-mx.google.com at your service, [54.166.173.168] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN 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 XOAUTH XOAUTH2 PLAIN-CLIENT TOKEN"
  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: STARTTLS requested but already using SSL
  DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
  DEBUG SMTP: AUTH LOGIN command trace suppressed
  http://mooark-romitchhabra.rhcloud.com/mooark/ADP/Thanks.jsp
  DEBUG SMTP: AUTH LOGIN failed
  Exception in thread "Thread-4" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 &lt;https://accounts.google.com/ContinueSignIn?sarp=1&amp;scc=1&amp;plt=AKgnsbvrd
  534-5.7.14 sdnRpIjv6joT7_WeGn1AAxAwQL6nET1nfNhi7oUeKqgdxHVmsc5XgZ4UFD26KJ0PfVSnLO
  534-5.7.14 m6C8BUAB5Xk3Yp0L5Is0c0mUcP5MQMA4exuzGXiaBe0zPxMtfRLPz_y8shSO0WwDwjsV6D
  534-5.7.14 ZP5fRWHRtbMDoI0hx81GBM59dc7tAM4nSsbTl1J3MoQfzeaK3_WYZ-BLtUl28P-qgcSkd
  534-5.7.14 eZMQKxw&gt; Please log in via your web browser and then try again.
  534-5.7.14 Learn more at
  534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 k9sm38376122qaj.7 - gsmtp
      at com.adp.thread.SendMail.run(SendMail.java:89)
  Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14    &lt;https://accounts.google.com/ContinueSignIn?sarp=1&amp;scc=1&amp;plt=AKgnsbvrd
  534-5.7.14 sdnRpIjv6joT7_WeGn1AAxAwQL6nET1nfNhi7oUeKqgdxHVmsc5XgZ4UFD26KJ0PfVSnLO
  534-5.7.14 m6C8BUAB5Xk3Yp0L5Is0c0mUcP5MQMA4exuzGXiaBe0zPxMtfRLPz_y8shSO0WwDwjsV6D
  534-5.7.14 ZP5fRWHRtbMDoI0hx81GBM59dc7tAM4nSsbTl1J3MoQfzeaK3_WYZ-BLtUl28P-qgcSkdh
  534-5.7.14 eZMQKxw&gt; Please log in via your web browser and then try again.
  534-5.7.14 Learn more at
  534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 k9sm38376122qaj.7 - 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 com.adp.thread.SendMail.run(SendMail.java:85)
  </code></pre>
  
  <p>I have tried all the answers given at <a href="https://stackoverflow.com/questions/19311823/java-mail-fails-to-authenticate-smtp-setting-in-openshift-server">Java Mail fails to authenticate smtp setting in openshift server</a>. I have tried logging into the gmail account and then sending the mail through my app in the same browser but still its not working. Dual verification is not activated in the gmail account &amp; the account is not protected by modern security standards and hence it allows less secure apps to connect (according to gmail).</p>
  
  <p><strong>Alternate Solution</strong>: Instead of Gmail SMTP, I tried Mailgun which exposes its API to send mail as REST services. It is free for developers with an upper limit on the number of mails which can be sent using a free account. It is working perfectly on Openshift. Here I have created an example to use Mailgun service using Jersey 2.x.
  <a href="https://gist.github.com/chhabraromit/0c0a16545dd31f471332" rel="nofollow noreferrer">https://gist.github.com/chhabraromit/0c0a16545dd31f471332</a></p>