title: javax.mail.AuthenticationFailedException: failed to connect, no password specified? But I know that the password is ok
body: <p>everyone!<br>
  I've got a problem during working with JavaMail API, I get an exception when trying to send a mail as follows: </p>
  
  <blockquote>
    <p>javax.mail.AuthenticationFailedException: failed to connect, no
    password specified
    </p>
  </blockquote>
  
  <p>Here is the code:</p>
  
  <pre><code>package sendmail;
  
  import javax.mail.*;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  import java.util.Properties;
  
  /**
   * Created by caesar-84 on 9/28/16.
   */
  public class SslGmailSender
  {
      private String username;
      private String password;
      private Properties props;
  
      public SslGmailSender(String username, String password)
      {
          this.username = username;
          this.password = password;
  
          props = new Properties();
          props.put("mail.smtp.host", "smtp.gmail.com");
          props.put("mail.smtp.socketFactory.port", "465");
          props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.port", "465");
      }
  
      public void send(String subject, String text, String from, String to)
      {
          Session thisSession = Session.getInstance(props, new Authenticator()
          {
              public PasswordAuthentication getPasswordAuthentification()
              {
                  return new PasswordAuthentication(SslGmailSender.this.username, password);
              }
          });
          try
          {
              MimeMessage message = new MimeMessage(thisSession);
              message.setFrom(new InternetAddress(from));
              message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
              message.setSubject(subject);
              message.setText(text);
  
              Transport.send(message);
              System.out.println("Yor e-mail has been sent.");
          }
          catch (MessagingException ex)
          {
              System.out.println("Uuups... something went wrong...");
              System.out.println(ex.getMessage());
              System.exit(2);
          }
      }
  }
  </code></pre>
  
  <p>And Main:</p>
  
  <pre><code>package sendmail;
  
  import java.io.*;
  
  /**
   * Created by caesar-84 on 9/28/16.
   */
  public class Main
  {
      public static void main(String[] args)
      {
          String username = "";
          String password = "";
          String subject = "";
          String text = "";
          String to = "";
          String typedText = "";
          StringBuilder sb = new StringBuilder();
  
          try(
                  BufferedReader console = new BufferedReader(new InputStreamReader(System.in)package sendmail;
  
  import java.io.*;
  
  /**
   * Created by caesar-84 on 9/28/16.
   */
  public class Main
  {
      public static void main(String[] args)
      {
          String username = "";
          String password = "";
          String subject = "";
          String text = "";
          String to = "";
          String typedText = "";
          StringBuilder sb = new StringBuilder();
  
          try(
                  BufferedReader console = new BufferedReader(new InputStreamReader(System.in)))
          {
              System.out.print("1. Enter manually\n2. Load data form file?\nType 1 or 2: ");
              String response = console.readLine();
              System.out.println();
              switch (response)
              {
                  case "1": {
                      System.out.print("Your e-mail: ");
                      username = console.readLine();
                      System.out.print("Password: ");
                      password = console.readLine();
                      System.out.print("Recipient: ");
                      to = console.readLine();
                      System.out.print("Message subject: ");
                      subject = console.readLine();
                      System.out.println("Text to send (type \"-END\" to finish):");
                      while (!typedText.contains("-END"))
                      {
                          typedText = console.readLine();
                          sb.append(typedText).append("\n");
                      }
                      sb.delete(sb.length() - 4, sb.length());
                  }
                  case "2":{
                      System.out.print("Enter the filename: ");
                      String input = console.readLine();
                      System.out.println();
                      //if (input.equals("def")) input = "/home/caesar-84/java_tries/learning/src/sendmail/mailtext.txt";
                      BufferedReader fr = new BufferedReader(new FileReader(input));
                      username = fr.readLine();
                      System.out.println("E-mail: " + username);
                      password = fr.readLine();
                      System.out.println("Password: " + password);
                      to = fr.readLine();
                      System.out.println("Recipient: " + to);
                      subject = fr.readLine();
                      System.out.println("Subject: " + subject);
  
                      while (fr.ready())
                      {
                          typedText = fr.readLine();
                          sb.append(typedText).append("\n");
                      }
                      fr.close();
                  }
              }
  
          }
          catch (FileNotFoundException ex)
          {
              System.out.println("Such file does not exist. Terminating.");
              System.exit(1);
          }
          catch (IOException ex){ex.printStackTrace();}
  
          System.out.println();
          text = sb.toString();
          System.out.println(text);
          String from = username;
          SslGmailSender thisSender = new SslGmailSender(username, password);
          thisSender.send(subject, text, from, to);
      }
  }))
          {
              System.out.print("1. Enter manually\n2. Load data form file?\nType 1 or 2: ");
              String response = console.readLine();
              System.out.println();
              switch (response)
              {
                  case "1": {
                      System.out.print("Your e-mail: ");
                      username = console.readLine();
                      System.out.print("Password: ");
                      password = console.readLine();
                      System.out.print("Recipient: ");
                      to = console.readLine();
                      System.out.print("Message subject: ");
                      subject = console.readLine();
                      System.out.println("Text to send (type \"-END\" to finish):");
                      while (!typedText.contains("-END"))
                      {
                          typedText = console.readLine();
                          sb.append(typedText).append("\n");
                      }
                      sb.delete(sb.length() - 4, sb.length());
                  }
                  case "2":{
                      System.out.print("Enter the filename: ");
                      String input = console.readLine();
                      System.out.println();
                      //if (input.equals("def")) input = "/home/caesar-84/java_tries/learning/src/sendmail/mailtext.txt";
                      BufferedReader fr = new BufferedReader(new FileReader(input));
                      username = fr.readLine();
                      System.out.println("E-mail: " + username);
                      password = fr.readLine();
                      System.out.println("Password: " + password);
                      to = fr.readLine();
                      System.out.println("Recipient: " + to);
                      subject = fr.readLine();
                      System.out.println("Subject: " + subject);
  
                      while (fr.ready())
                      {
                          typedText = fr.readLine();
                          sb.append(typedText).append("\n");
                      }
                      fr.close();
                  }
              }
  
          }
          catch (FileNotFoundException ex)
          {
              System.out.println("Such file does not exist. Terminating.");
              System.exit(1);
          }
          catch (IOException ex){ex.printStackTrace();}
  
          System.out.println();
          text = sb.toString();
          System.out.println(text);
          String from = username;
          SslGmailSender thisSender = new SslGmailSender(username, password);
          thisSender.send(subject, text, from, to);
      }
  }
  </code></pre>