title: Getting Null pointer Exception while using the Java mail api to send mail from local host using gmail smtp.
body: <p>In servlet class i am initializing all the variable in init method</p>
  
  <pre><code>private String host="smtp.gmail.com";
  private String port="465";
  private String user="abc@gmail.com";
  private String pass="pass";
  public void init()
  {
      // reads SMTP server setting from web.xml file
      ServletContext context = getServletContext();
      host = context.getInitParameter("host");
      port = context.getInitParameter("port");
      user = context.getInitParameter("user");
      pass = context.getInitParameter("pass");
  }
  </code></pre>
  
  <p>In EmailUtility class i am having the code below</p>
  
  <pre><code>    Properties properties = new Properties();
      properties.put("mail.smtp.host", host);
      properties.put("mail.smtp.port", port);
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.starttls.enable", "true");
  
  
      Authenticator auth = new Authenticator() 
      {
          public PasswordAuthentication getPasswordAuthentication()
          {
              return new PasswordAuthentication(userName, password);
          }
      };
  
      Session session = Session.getInstance(properties, auth);
  
  
      Message msg = new MimeMessage(session);
  
      msg.setFrom(new InternetAddress(userName));
      InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
      msg.setRecipients(Message.RecipientType.TO, toAddresses);
      msg.setSubject(subject);
      msg.setSentDate(new Date());
      msg.setText(message);   
      Transport.send(msg);
  </code></pre>
  
  <p>Stack Trace of Exception is</p>
  
  <pre><code>    at java.lang.Thread.run(Unknown Source)
      java.lang.NullPointerException
      at java.util.Hashtable.put(Unknown Source)
      at EmailUtility.sendEmail(EmailUtility.java:31)
      at EmailSendingServlet.doPost(EmailSendingServlet.java:52)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  </code></pre>
  
  <p>I am trying to test this mail sending mechanism on my local host using gmail smtp. Unable to track why this Exception occurs. I have also tried to run this by changing the port number but ended up with the same problem.</p>