title: How to resolve javax.mail.AuthenticationFailedException issue?
body: <p>I am doing a <code>sendMail</code> <code>Servlet</code> with <code>JavaMail</code>. I have <code>javax.mail.AuthenticationFailedException</code> on my output. Can anyone please help me out? Thanks.</p>
  
  <p>sendMailServlet code:</p>
  
  <pre><code>try {
          String host = "smtp.gmail.com";
          String from = "my@gmail.com";
          String pass = "pass";
          Properties props = System.getProperties();
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.user", from);
          props.put("mail.smtp.password", pass);
          props.put("mail.smtp.port", "587");
          props.put("mail.smtp.auth", "true");
          props.put("mail.debug", "true");
  
          Session session = Session.getDefaultInstance(props, null);
          MimeMessage message = new MimeMessage(session);
          Address fromAddress = new InternetAddress(from);
          Address toAddress = new InternetAddress("test1@gmail.com");
  
          message.setFrom(fromAddress);
          message.setRecipient(Message.RecipientType.TO, toAddress);
  
          message.setSubject("Testing JavaMail");
          message.setText("Welcome to JavaMail");
          Transport transport = session.getTransport("smtp");
          transport.connect(host, from, pass);
          message.saveChanges();
          Transport.send(message);
          transport.close();
  
      }catch(Exception ex){
  
          out.println("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;");
          out.println("ERROR: " + ex);
          out.println("&lt;/body&gt;&lt;/html&gt;");
      }
  </code></pre>
  
  <p>Output on GlassFish 2.1:</p>
  
  <pre><code>DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
  220 mx.google.com ESMTP 36sm10907668yxh.13
  DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
  EHLO platform-4cfaca
  250-mx.google.com at your service, [203.126.159.130]
  250-SIZE 35651584
  250-8BITMIME
  250-STARTTLS
  250-ENHANCEDSTATUSCODES
  250 PIPELINING
  DEBUG SMTP: Found extension "SIZE", arg "35651584"
  DEBUG SMTP: Found extension "8BITMIME", arg ""
  DEBUG SMTP: Found extension "STARTTLS", arg ""
  DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
  DEBUG SMTP: Found extension "PIPELINING", arg ""
  STARTTLS
  220 2.0.0 Ready to start TLS
  EHLO platform-4cfaca
  250-mx.google.com at your service, [203.126.159.130]
  250-SIZE 35651584
  250-8BITMIME
  250-AUTH LOGIN PLAIN
  250-ENHANCEDSTATUSCODES
  250 PIPELINING
  DEBUG SMTP: Found extension "SIZE", arg "35651584"
  DEBUG SMTP: Found extension "8BITMIME", arg ""
  DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
  DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
  DEBUG SMTP: Found extension "PIPELINING", arg ""
  DEBUG SMTP: Attempt to authenticate
  AUTH LOGIN
  334 VXNlcm5hbWU6
  aWpveWNlbGVvbmdAZ21haWwuY29t
  334 UGFzc3dvcmQ6
  MTIzNDU2Nzhf
  235 2.7.0 Accepted
  DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
  DEBUG SMTP: useEhlo true, useAuth true
  </code></pre>