title: JavaMailSender returning null pointer exception
body: <p>I am trying to send a register confirmation email in my spring MVC web application and Tomcat 7 using JavaMailSender but it always returns a NullPointerException. Can anyone help me find out why I get the null pointer? Below are my config settings:</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;!-- jndi mail session --&gt;
  &lt;resource-ref&gt;
      &lt;description&gt;
          Resource reference to a factory for javax.mail.Session
          instances that may be used for sending electronic mail
          messages, preconfigured to connect to the appropriate
          SMTP server.
      &lt;/description&gt;
      &lt;res-ref-name&gt;mail/Session&lt;/res-ref-name&gt;
      &lt;res-type&gt;javax.mail.Session&lt;/res-type&gt;
      &lt;res-auth&gt;Container&lt;/res-auth&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p>application-servlet.xml</p>
  
  <pre><code>    &lt;!-- Mail Sender bean definition --&gt;
  
  &lt;bean id="smtpSession" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
          &lt;property name="jndiName" value="java:comp/env/mail/session"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"&gt;
      &lt;property name="session" ref="smtpSession"/&gt;
       &lt;!--
      &lt;property name="host" value="smtp.gmail.com" /&gt;
      &lt;property name="port" value="465" /&gt;
      &lt;property name="username" value="abc@gmail.com" /&gt;
      &lt;property name="password" value="test123" /&gt;
      &lt;property name="protocol" value="smtp" /&gt;
  
  
      &lt;property name="defaultEncoding" value="UTF-8"/&gt; 
  
      &lt;property name="javaMailProperties"&gt;
          &lt;props&gt;
              &lt;prop key="mail.smtp.auth"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.smtp.connectiontimeout"&gt;5000&lt;/prop&gt;
              &lt;prop key="mail.smtp.sendpartial"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.smtp.userset"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.mime.charset"&gt;UTF-8&lt;/prop&gt;
              &lt;prop key="mail.smtp.isSecure"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.smtp.requiresAuthentication"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.smtp.auth"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.smtp.port"&gt;465&lt;/prop&gt;
              &lt;prop  key="mail.smtp.socketFactory.class"&gt;javax.net.ssl.SSLSocketFactory&lt;/prop&gt;
              &lt;prop key="mail.smtp.socketFactory.fallback"&gt;false&lt;/prop&gt;
              &lt;prop key="mail.smtp.starttls.enable"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.debug"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
       --&gt;
  &lt;/bean&gt;
  
  &lt;bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"&gt;
      &lt;property name="from" value="abc@hotmail.com"/&gt;       
  &lt;/bean&gt;
  </code></pre>
  
  <p>context.xml (in tomcat conf folder)</p>
  
  <pre><code>&lt;Context path="/MVC" docBase="MVC" debug="5" crossContext="false"&gt;
  &lt;Resource name="mail/session" 
      auth="Container"
          type="javax.mail.Session"
          username="abc@gmail.com"
          password="test23"
          mail.debug="true"
          mail.user="abc@gmail.com"
          mail.password="test123"
          mail.transport.protocol="smtp"
          mail.smtp.host="smtp.gmail.com"
          mail.smtp.auth="true"
          mail.smtp.port="25"
          mail.smtp.starttls.enable="true"
  /&gt;
  </code></pre>
  
  <p>SendMail.java </p>
  
  <pre><code>import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.mail.MailException;
  import org.springframework.mail.MailParseException;
  import org.springframework.mail.MailSender;
  import org.springframework.mail.SimpleMailMessage;
  import org.springframework.mail.javamail.JavaMailSender;
  import org.springframework.mail.javamail.MimeMessageHelper;
  import org.springframework.mail.javamail.MimeMessagePreparator;
  import org.springframework.stereotype.Service;
  
  import com.wbkit.mvc.domain.User;
  
  
  @Service
  public class SendMail 
  {
  @Autowired
  private JavaMailSender mailSender;
  @Autowired
  private SimpleMailMessage simpleMailMessage;
  
  
  
  public void confirmRegistrationMail(User user)
  {
  
  
      MimeMessage message = mailSender.createMimeMessage();
  
      try
      {
          MimeMessageHelper helper = new MimeMessageHelper(message, true);
  
          helper.setFrom(simpleMailMessage.getFrom());
          helper.setTo(user.getEmail());
          helper.setSubject("MVC Registration");
          helper.setText("Hello " + user.getFirstName() + " "
                + user.getLastName() + ", \n" +
                " Your MVC Account Registration was successful. Your user ID is "
                + user.getEmail() + " and your password is "
                + user.getUserPass() + ".\n" +
                "You can now access the mobile applicatoin or the web application " 
                + " your user name (email) and password.");
  
  
      }
      catch (MessagingException e) 
      {
          throw new MailParseException(e);
      }
      mailSender.send(message);
  
  
  }
  
  public void requireApprovalMail(final User user)
  {
  
  
  }
  
  
  }
  </code></pre>
  
  <p>Stack Trace:</p>
  
  <pre><code>4-Sep-2012 7:47:16 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [MVC] in context with path [/MVC] threw         exception [Request processing failed; nested exception is java.lang.NullPointerException]  with root cause
  java.lang.NullPointerException
  at com.wbkit.mvc.util.SendMail.confirmRegistrationMail(SendMail.java:79)
  at com.wbkit.mvc.web.main.RegisterController.save(RegisterController.java:276)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at      org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
  at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
  at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1812)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>java.lang.NullPointerException at com.wbkit.mvc.util.SendMail.confirmRegistrationMail(SendMail.java:79)  points to </p>
  
  <pre><code>MimeMessage message = mailSender.createMimeMessage();
  </code></pre>
  
  <p>in SendMail.java. I removed some of the commented code I had.</p>
  
  <p>Thanks for your suggestions and help.</p>