Feb 26 2009

Sending email with Spring and Velocity

Published by Gilles at 9:20 pm under J2EE, Java, Programming, Spring

ApplicationContext-service

It's always a good idea to externalize variables in a properties file.So create a my_config.properties accessible from your classpath. Put in it the variables needed for your mail server.

CODE:
  1. #Mail Config
  2. host=localhost
  3. port=25
  4. username=system
  5. password=12345

Create a new application context file:

XML:
  1. <beans xmlns="http://www.springframework.org/schema/beans">
  2. </beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/-beans-2.5.xsd
  6. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/-tx-2.5.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/-aop-2.5.xsd&gt;
  8.  
  9. <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"></bean>
  10.  
  11. <property name="location" value="classpath:my_config.properties"></property> <!-- Services -->
  12. <bean id="emailservice" class="com.xxx.service.impl.EmailServiceImpl"></bean>
  13.  
  14. <property name="velocityEngine" ref="velocityEngine"></property>
  15. <property name="javaMailSender" ref="mailsender"></property><!-- Emails templates and config -->
  16. <bean id="mailsender" class="org.springframework.mail.javamail.JavaMailSenderImpl"></bean>
  17.  
  18. <property name="host"><value>${host}</value></property>
  19. <property name="port"><value>${port}</value></property>
  20. <property name="username"><value>${username}</value></property>
  21. <property name="password"><value>${password}</value></property><bean id="validationMessage" class="org.springframework.mail.SimpleMailMessage"></bean>
  22.  
  23. <property name="from"> <value><!--[CDATA[My <${myemail}>]]--></value></property>
  24. <property name="subject" value="My subject"></property>
  25. <property name="text" value="com/xxx//my-message.vm"></property><bean id="velocityEngine" class="org.springframework.ui..VelocityEngineFactoryBean"></bean>
  26.  
  27. <property name="velocityProperties"> <value>
  28. resource.loader=org.apache..runtime.resource.loader.ClasspathResourceLoader
  29. </value></property> <bean>

Pages: 1 2 3 4 5 6 7

One Response to “Sending email with Spring and Velocity”

  1. asdfon 30 May 2010 at 5:46 pm

    reading this article is like pain in the ass. please next time split such article into 20 pages, 7 is not enought

Trackback URI | Comments RSS

Leave a Reply