title: Solve error javax.mail.AuthenticationFailedException
body: <p>I'm not familiar with this function to send mail in java. I'm getting an error while sending email to reset a password. Hope you can give me a solution.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>public synchronized static boolean sendMailAdvance(String emailTo, String subject, String body)
  {
      String host = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-ADDRESS");
      String userName = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-USERNAME");
      String password = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-PASSWORD");
      String port = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-PORT");
      String starttls = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-STARTTLS");
      String socketFactoryClass = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-SOCKET-CLASS");
      String fallback = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-ALLOW-FALLBACK");                         
  
      try
      {
          java.util.Properties props = null;
          props = System.getProperties();
          props.put("mail.smtp.user", userName);
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.debug", "true");
  
          if(!"".equals(port))
          {
              props.put("mail.smtp.port", port);
              props.put("mail.smtp.socketFactory.port", port);
          }
  
          if(!"".equals(starttls))
              props.put("mail.smtp.starttls.enable",starttls);
  
          if(!"".equals(socketFactoryClass))                   
              props.put("mail.smtp.socketFactory.class",socketFactoryClass);
  
          if(!"".equals(fallback))
              props.put("mail.smtp.socketFactory.fallback", fallback);
  
          Session session = Session.getDefaultInstance(props, null);
          session.setDebug(true);
  
          MimeMessage msg = new MimeMessage(session);
          msg.setFrom(new InternetAddress(userName));
          msg.setSubject(subject);                
          msg.setText(body, "ISO-8859-1");
          msg.setSentDate(new Date());
          msg.setHeader("content-Type", "text/html;charset=\"ISO-8859-1\"");
          msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));
          msg.saveChanges();
  
          Transport transport = session.getTransport("smtp");
          transport.connect(host, userName, password);
          transport.sendMessage(msg, msg.getAllRecipients());
          transport.close();
              return true;
      }
      catch (Exception mex)
      {
          mex.printStackTrace();
          return false;
      }
  }
  </code></pre>
  
  <p>Throws the following error:</p>
  
  <blockquote>
    <p>DEBUG: setDebug: JavaMail version 1.4.1ea-SNAPSHOT</p>
  
  <pre><code>DEBUG: getProvider() returning   javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
  </code></pre>
    
    <p>DEBUG SMTP: useEhlo true, useAuth true</p>
    
    <p>DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false 220 mx.google.com ESMTP m4sm5929870pbg.38 - gsmtp</p>
    
    <p>DEBUG SMTP: connected to host "smtp.gmail.com", port: 465</p>
    
    <p>EHLO fatin</p>
    
    <p>250-mx.google.com at your service, [175.139.198.14]</p>
    
    <p>250-SIZE 35882577</p>
    
    <p>250-8BITMIME</p>
    
    <p>250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN</p>
    
    <p>250-ENHANCEDSTATUSCODES</p>
    
    <p>250 CHUNKING</p>
    
    <p>DEBUG SMTP: Found extension "SIZE", arg "35882577"</p>
    
    <p>DEBUG SMTP: Found extension "8BITMIME", arg ""</p>
    
    <p>DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"</p>
    
    <p>DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""</p>
    
    <p>DEBUG SMTP: Found extension "CHUNKING", arg ""</p>
    
    <p>DEBUG SMTP: Attempt to authenticate</p>
    
    <p>AUTH LOGIN</p>
    
    <p>334 VXNlcm5hbWU6</p>
    
    <p>YWNjb3VudEBibG9vbWluZy5jb20ubXk=</p>
    
    <p>334 UGFzc3dvcmQ6</p>
    
    <p>Ymxvb21pbmc=
    535-5.7.8 Username and Password not accepted. Learn more at
    535 5.7.8 <a href="http://support.google.com/mail/bin/answer.py?answer=14257" rel="noreferrer">http://support.google.com/mail/bin/answer.py?answer=14257</a></p>
    
    <p>m4sm5929870pbg.38 - gsmtp</p>
    
    <p>[STDOUT] javax.mail.AuthenticationFailedException</p>
    
    <p>[STDOUT] at javax.mail.Service.connect(Service.java:319)</p>
    
    <p>[STDOUT] at javax.mail.Service.connect(Service.java:169)</p>
    
    <p>[STDOUT] at com.vlee.util.mail.SendMail.sendMailAdvance(SendMail.java:283)</p>
    
    <p>[STDOUT] at com.vlee.servlet.ecommerce.DoMemberLogin.fnSendPwd(DoMemberLogin.java:251)</p>
    
    <p>[STDOUT] at com.vlee.servlet.ecommerce.DoMemberLogin.doPost(DoMemberLogin.java:72)</p>
  </blockquote>