Feb 26 2009
Sending email with Spring and Velocity
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:
-
#Mail Config
-
host=localhost
-
port=25
-
username=system
-
password=12345
Create a new application context file:
XML:
-
<beans xmlns="http://www.springframework.org/schema/beans">
-
</beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>
-
-
<bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"></bean>
-
-
<property name="location" value="classpath:my_config.properties"></property> <!-- Services -->
-
<bean id="emailservice" class="com.xxx.service.impl.EmailServiceImpl"></bean>
-
-
<property name="velocityEngine" ref="velocityEngine"></property>
-
<property name="javaMailSender" ref="mailsender"></property><!-- Emails templates and config -->
-
<bean id="mailsender" class="org.springframework.mail.javamail.JavaMailSenderImpl"></bean>
-
-
<property name="host"><value>${host}</value></property>
-
<property name="port"><value>${port}</value></property>
-
<property name="username"><value>${username}</value></property>
-
<property name="password"><value>${password}</value></property><bean id="validationMessage" class="org.springframework.mail.SimpleMailMessage"></bean>
-
-
<property name="from"> <value><!--[CDATA[My <${myemail}>]]--></value></property>
-
<property name="subject" value="My subject"></property>
-
-
<property name="velocityProperties"> <value>
-
resource.loader=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
-
</value></property> <bean>



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