title: Exception: javax.mail.AuthenticationFailedException: failed to connect, no password specified? Even with PasswordAuthenticator?
body: <p>I am making a java application that sends emails. </p>
  
  <p>For some reason, I kept getting <code>javax.mail.AuthenticationFailedException</code>, even though I have the correct username and password. </p>
  
  <p>I've seen some posts say that the missing password authentication might cause this exception as well, but I think i have that part as well. Now I am so stuck and couldn't figure out where things go wrong.</p>
  
  <p>here is my code:</p>
  
  <pre><code>import javax.mail.*;
  import javax.mail.internet.AddressException;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  import java.util.Properties;
  import javax.swing.*;
  
  public class sendEmail {
      private static String subject = "Test", content = "This is a test";
  
      public sendEmail() {
  
      }
  
      public static void send() throws MessagingException{
          Properties properties = new Properties();
          properties.put("mail.smtp.auth","true");// set the authentication to true
          properties.put("mail.smtp.starttls.enable","true");
          properties.put("mail.smtp.host","smtp.gmail.com");
          properties.put("mail.smtp.port","587");
          String username = "clashroyale70532@gmail.com";
          String password = "********";
          Session ses = Session.getInstance(properties, new Authenticator() {
              protected PasswordAuthentication getPassWordAuthentication() {
                  return new PasswordAuthentication(username, password);
              }
          });
          ses.setDebug(true);
          Message message = prepareMessage(ses,username);
          Transport.send(message);
      }
  
      private static Message prepareMessage(Session session, String account) {
          try {
              Message msg = new MimeMessage(session);
              msg.setFrom(new InternetAddress(account));
              msg.setRecipient(Message.RecipientType.TO, new InternetAddress(account));
              msg.setSubject(subject);
              msg.setText(content);
              return msg;
          } catch (AddressException e) {
              e.printStackTrace();
          } catch (MessagingException m) {
              throw new RuntimeException(m);
          }
          return null;
      }
      public static void main(String[] args) {
          try {
              sendEmail.send();
              }
              catch (MessagingException m) {
                  m.printStackTrace();
              }
      }
  
  }
  </code></pre>
  
  <p>Here is the JavaMail debug output:</p>
  
  <pre><code> DEBUG: setDebug: JavaMail version 1.6.2
      DEBUG: getProvider() returning 
      javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
      DEBUG SMTP: need username and password for authentication
      DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=*******(name not shown, it's my real name), password=&lt;null&gt;
  </code></pre>
  
  <p>The user it shows is not associated with the email account I used in my code. I am not sure how it got my real name.</p>