Announcement Announcement Module
Collapse
No announcement yet.
Problem with JavaMailSender/VelocityEngineFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with JavaMailSender/VelocityEngineFactoryBean

    Hi

    i am trying to use the mailsender example similar to the one stated in the capter 22:

    Code:
    	private JavaMailSender mailSender = new JavaMailSenderImpl();
    	private VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
    	
    	
    	public void sendTutorConfirmationEmail(final String email, final String lastName, final int userId, final String authenticHash) {
    		final HttpServletRequest curRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    		Properties velocityProperties = new Properties();
    		velocityProperties.put("resource.loader", "class");
    		velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    		velocityEngineFactoryBean.setVelocityProperties(velocityProperties );
    		MimeMessagePreparator preparator = new MimeMessagePreparator() {
    			public void prepare(MimeMessage mimeMessage) throws Exception {
    				MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
    				message.setTo(email);
    				message.setFrom("[email protected]"); 
    				Map model = new HashMap();
    				model.put("lastName", lastName);
    				model.put("hash", authenticHash);
    				model.put("contextPath", curRequest.getContextPath());
    				model.put("userId", userId);
    				String text = VelocityEngineUtils.mergeTemplateIntoString(
    						velocityEngineFactoryBean.getObject(), "email/registration-tutorConfirmation.vm", model);
    				message.setText(text, true);
    			}
    		};
    		System.out.println(mailSender);
    		this.mailSender.send(preparator);
    	}
    and i get this exceptions:
    org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.NullPointerException
    at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:656)
    ...
    Caused by: org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.NullPointerException
    at org.springframework.mail.javamail.JavaMailSenderIm pl.send(JavaMailSenderImpl.java:367)
    ...
    Caused by: java.lang.NullPointerException
    at org.springframework.ui.velocity.VelocityEngineUtil s.mergeTemplate(VelocityEngineUtils.java:58)
    at org.springframework.ui.velocity.VelocityEngineUtil s.mergeTemplateIntoString(VelocityEngineUtils.java :122)
    ..

    Does anybody know whats the problem?

    My vm file:

    <html>
    <body>
    <h3>Dear (${lastName}), <br/> Welcome. </h3>

    <div>
    <p/>
    Please click: <br/>

    <a href="${contextPath}/tutorConfirmation.html?id=${userId}&hash=${hash}"> Confirm</a>
    </div>
    </body>

    </html>

  • #2
    i used DI and it works now

    Comment

    Working...
    X