In servlet class i am initializing all the variable in init method
private String host="smtp.gmail.com";
private String port="465";
private String user="[email protected]";
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");
}
In EmailUtility class i am having the code below
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);
Stack Trace of Exception is
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)
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.