title: Sending Email Using Java Netbeans (Exceptions)
body: <p>Recently I made web application which sends an E-mail. The code is working fine I have no error, I have runtime exception </p>
  
  <pre><code>(javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty)
  </code></pre>
  
  <p>My Code For index.jsp is:</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt; Java Mail &lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action="sendMail.jsp" method="POST"&gt;
          &lt;table border="0" align="center" cellpadding="5"&gt;
              &lt;tbody&gt;
                  &lt;thead&gt;&lt;tr&gt; &lt;td colspan="3" align="center"&gt;
                  &lt;b&gt; Send Mail &lt;/b&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt;
                  &lt;tr&gt;
                      &lt;td&gt; To &lt;/td&gt; &lt;td&gt; : &lt;/td&gt;
                      &lt;td&gt; &lt;input type="text" name="to" value="" /&gt; &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt; Subject &lt;/td&gt; &lt;td&gt; : &lt;/td&gt;
                      &lt;td&gt; &lt;input type="text" name="subject" value="" /&gt; &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt; Message &lt;/td&gt; &lt;td&gt; : &lt;/td&gt;
                      &lt;td&gt; &lt;textarea name="message" rows="8" cols="30"&gt;
                      &lt;/textarea&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td colspan="3" align="center"&gt;
                      &lt;input type="submit" value="Send Mail" /&gt;
  
                      &lt;input type="reset" value="Reset" /&gt;
                      &lt;td&gt;
                  &lt;/tr&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My code for sendMail.jsp is:</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset="&gt;
      &lt;jsp:useBean id="mail" scope="session" class="jMail.Mail" /&gt;
      &lt;jsp:setProperty name="mail" property="to" param="to" /&gt;
      &lt;jsp:setProperty name="mail" property="from" value="MyEmail@gmail.com" /&gt;
      &lt;jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" /&gt;
      &lt;jsp:setProperty name="mail" property="subject" param="subject" /&gt;
      &lt;jsp:setProperty name="mail" property="message" param="message" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;%
          String to = mail.getTo();
          int result;
          result = mail.sendMail();
          if (result == 0) {
              out.println(" Mail Successfully Sent to " + to);
          } else {
              out.println(" Mail NOT Sent to " + to);
          }
      %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My Code For Mail.java is:</p>
  
  <pre><code>package jMail;
  import javax.mail.*;
  import javax.mail.internet.*;
  import java.util.*;
  
  public class Mail {
  private String to;
  private String from;
  private String message;
  private String subject;
  private String smtpServ;
  
  /**
   * @return the to
   */
  public String getTo() {
      return to;
  }
  
  /**
   * @param to the to to set
   */
  public void setTo(String to) {
      this.to = to;
  }
  
  /**
   * @return the from
   */
  public String getFrom() {
      return from;
  }
  
  /**
   * @param from the from to set
   */
  public void setFrom(String from) {
      this.from = from;
  }
  
  /**
   * @return the message
   */
  public String getMessage() {
      return message;
  }
  
  /**
   * @param message the message to set
   */
  public void setMessage(String message) {
      this.message = message;
  }
  
  /**
   * @return the subject
   */
  public String getSubject() {
      return subject;
  }
  
  /**
   * @param subject the subject to set
   */
  public void setSubject(String subject) {
      this.subject = subject;
  }
  
  /**
   * @return the smtpServ
   */
  public String getSmtpServ() {
      return smtpServ;
  }
  
  /**
   * @param smtpServ the smtpServ to set
   */
  public void setSmtpServ(String smtpServ) {
      this.smtpServ = smtpServ;
  }
  
  
  public int sendMail(){
      try
      {
          Properties props = System.getProperties();
            // -- Attaching to default Session, or we could start a new one --
            props.put("mail.transport.protocol", "smtp" );
            props.put("mail.smtp.starttls.enable","true" );
            props.put("mail.smtp.host",smtpServ);
            props.put("mail.smtp.auth", "true" );
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, auth);
            // -- Create a new message --
            Message msg = new MimeMessage(session);
            // -- Set the FROM and TO fields --
            msg.setFrom(new InternetAddress(from));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,     false));
            msg.setSubject(subject);
            msg.setText(message);
            // -- Set some other header information --
            msg.setHeader("MyMail", "Mr. XYZ" );
            msg.setSentDate(new Date());
            // -- Send the message --
            Transport.send(msg);
            System.out.println("Message sent to"+to+" OK." );
            return 0;
      }
      catch (Exception ex)
      {
        ex.printStackTrace();
        System.out.println("Exception "+ex);
        return -1;
      }
  }
  
  
  
  private class SMTPAuthenticator extends javax.mail.Authenticator {
      @Override
      public PasswordAuthentication getPasswordAuthentication() {
          String username =  "MyEmail@gmail.com";           
          String password = "MyPassword";
          return new PasswordAuthentication(username, password);
      }
    }
  
  }
  </code></pre>
  
  <p>Please help me I always have runtime exception:</p>
  
  <p>Exception javax.mail.MessagingException: Could not convert socket to TLS;
    nested exception is:
      javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty</p>