title: Can't send mail via SSL or TLS using SMTP using Javamail
body: <p>Happy New Year!</p>
  
  <p>I'm working on a application where the user gets an email whenever a particular trigger occurs.</p>
  
  <p>This is the function that I'm using to send Email:</p>
  
  <pre><code>public static void sendEmail(String host, String port, String useSSL, String useTLS, String useAuth, String user, String password, String subject, String content, String type, String recipients)
              throws NoSuchProviderException, AddressException, MessagingException  {
          final Properties props = new Properties();
          props.setProperty("mail.transport.protocol", "smtp");
          props.setProperty("mail.smtp.host", host);
          props.setProperty("mail.smtp.port", port);        
          if (useSSL != null &amp;&amp; !useSSL.equals("false") &amp;&amp; useSSL.equals("true")) {
              props.setProperty("mail.smtp.ssl.enable", useSSL);
              props.setProperty("mail.smtp.socketFactory.class",
                      "javax.net.ssl.SSLSocketFactory");
              props.setProperty("mail.smtp.socketFactory.port", port);
  
          }
          if (useTLS != null &amp;&amp; !useTLS.equals("false") &amp;&amp; useTLS.equals("true")) {
              props.setProperty("mail.smtp.starttls.enable", useTLS);
              props.setProperty("mail.smtp.socketFactory.fallback", "true");
          }   
          props.setProperty("mail.smtp.auth", useAuth);
          props.setProperty("mail.from", user);  
          props.setProperty("mail.smtp.user", user);
          props.setProperty("mail.password", password);
  
          Session mailSession = Session.getDefaultInstance(props, new Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(props.getProperty("mail.smtp.user"), props
                          .getProperty("mail.password"));
              }
          });   
  
          Transport transport = mailSession.getTransport();
  
          MimeMessage message = new MimeMessage(mailSession);
          message.setHeader("Subject", subject);
          message.setContent(content, type);
  
          StringTokenizer tokenizer = new StringTokenizer(recipients, ";");
          while (tokenizer.hasMoreTokens()) {
              String recipient = tokenizer.nextToken();
              message.addRecipient(Message.RecipientType.TO,
                      new InternetAddress(recipient));
          }
  
          transport.connect();
          transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
          transport.close();
  </code></pre>
  
  <p>Odd enough, whenever I try to run the above code with main method it'd send the email for both SSL and TLS protocols successfully.</p>
  
  <pre><code>public static void main(String args[])
      {
          try {
              Notifier.sendEmail("smtp.gmail.com", "587", "false", "true", "true","sender_email@gmail.com", "testpassword", "CHECKING SETTINGS", "CHECKING EMAIL FUNCTIONALITY", "text/html", "cc_email@gmail.com");
          } catch (Exception ex) {
              ex.printStackTrace();
          } 
      }
  </code></pre>
  
  <p>But it fails whenever I try to run the same code through my web application. </p>
  
  <p>Sending it via SSL throws this error:</p>
  
  <pre><code>com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at
  jvm 1    | 530 5.5.1  https://support.google.com/mail/answer/14257 f12sm88286300pat.20 - gsmtp
  jvm 1    | 
  jvm 1    |  at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
  </code></pre>
  
  <p>Sending it via TLS throws this error:</p>
  
  <pre><code>javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  jvm 1    |   nested exception is:
  jvm 1    |  javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
  jvm 1    |  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
  </code></pre>
  
  <p>Any kind of help is appreciated.</p>
  
  <p>EDIT1:</p>
  
  <p>Here's the tpl file from front end</p>
  
  <pre><code>&lt;div class="label1"&gt;&lt;h3 class="label"&gt;Host:&lt;/h3&gt;&lt;/div&gt;
       &lt;div class="field1"&gt;&lt;input type="text" class="input1" name="host" size="20" value="$HOST$"&gt;&lt;/div&gt;
           &lt;div class="port"&gt;&lt;h3 class="label"&gt;Port:&lt;/h3&gt;&lt;/div&gt;
           &lt;div class="fieldport"&gt;&lt;input type="text" class="fieldport" name="port" size="5" value="$PORT$"&gt;&lt;/div&gt;
           &lt;div class="ssl"&gt;
                  &lt;input type="radio" name="sslEnable" value="$SSLENABLE$"&gt;
                      Enable SSL?
           &lt;/div&gt;
           &lt;div class="tls"&gt;
                  &lt;input type="radio" name="tlsEnable" value="$TLSENABLE$"&gt;
                      Enable TLS?
           &lt;/div&gt;
           &lt;div class="auth"&gt;
                  &lt;input type="checkbox" name="auth"$AUTH$&gt;
                      Enable Authentication?
           &lt;/div&gt;            
       &lt;div class="label2"&gt;&lt;h3 class="label"&gt;User:&lt;/h3&gt;&lt;/div&gt;
       &lt;div class="field2"&gt;&lt;input type="text" class="input1" name="user" size="20" value="$USER$"&gt;&lt;/div&gt;
       &lt;div class="label3"&gt;&lt;h3 class="label"&gt;Password:&lt;/h3&gt;&lt;/div&gt;
       &lt;div class="field3"&gt;&lt;input type="password" class="input1" name="password" size="20" value="$PASSWORD$"&gt;&lt;/div&gt;
       &lt;div class="label4"&gt;&lt;h3 class="label"&gt;Recipient(s):&lt;/h3&gt;&lt;/div&gt;
       &lt;div class="field4"&gt;&lt;input type="text" class="input1" name="recipients" size="50" value="$RECIPIENTS$"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>The values gets saved in a configuration file which looks like this:</p>
  
  <pre><code>host=smtp.gmail.com
  port=587
  ssl=false
  tls=true
  auth=true
  user=send_user_email@gmail.com
  password=O0UbYboDfVFRaiA=
  recipients=cc_user_email@gmail.com
  trigger1=false
  attempt=0
  trigger2=false
  percent=5
  anyOrAll=ANY
  trigger3=true
  format=HTML
  trigger4=true
  trigger5=true
  </code></pre>
  
  <p>EDIT2:</p>
  
  <pre><code>public static void sendEmail(String message)
        throws NoSuchProviderException, AddressException, MessagingException
    {
      if (message == null || message.trim().equals("")) return;
  
      StringBuffer content = new StringBuffer();
      content.append(getHeader());
      content.append(message);
      content.append(getFooter());
      String format = NotifyProps.getFormat();
      String type = "text/plain";
      if (format.equals(NotifyProps.HTML)) type = "text/html";
  
      sendEmail(NotifyProps.getHost(), NotifyProps.getPort(), Boolean.toString(NotifyProps.getUseAuth()), Boolean.toString(NotifyProps.getUseSSL()), Boolean.toString(NotifyProps.getUseTLS()),NotifyProps.getUser(), NotifyProps.getPassword(),
                "Transaction Processor Auto Notification", content.toString(), type,
                NotifyProps.getRecipients())
    }
  </code></pre>
  
  <p>This is the the class that sets and gets the properties:</p>
  
  <p><a href="https://codeshare.io/5G8ki">https://codeshare.io/5G8ki</a></p>
  
  <p>Thank you.</p>