0

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.

2
  • Where specifically in this code block is this exception being thrown? Commented Apr 19, 2015 at 6:29
  • In EmailUtility class at properties.put("mail.smtp.host", host); Commented Apr 19, 2015 at 6:34

1 Answer 1

1

host is coming back null. Since Properties extends Hashtable, you're making use of Hashtable's put method. But, put will throw an NPE if either the key or value are null:

Throws:

NullPointerException - if the key or value is null

Ensure that you're getting a value back from all invocations of context.getInitParameter and that they're making their way to their correct variables.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.