Java Reference
In-Depth Information
public void notifyCopyError(String srcDir, String destDir, String filename) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("system@localhost");
message.setTo("admin@localhost");
message.setSubject("File Copy Error");
message.setText(
"Dear Administrator,\n\n" +
"An error occurred when copying the following file :\n" +
"Source directory : " + srcDir + "\n" +
"Destination directory : " + destDir + "\n" +
"Filename : " + filename);
mailSender.send(message);
}
}
Next, you have to configure a MailSender implementation in the bean configuration file and inject it
into EmailErrorNotifier . In Spring, the unique implementation of this interface is JavaMailSenderImpl ,
which uses JavaMail to send e-mail.
<beans ...>
...
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="localhost" />
<property name="port" value="25" />
<property name="username" value="system" />
<property name="password" value="12345" />
</bean>
<bean id="errorNotifier"
class="com.apress.springenterpriserecipes.replicator.EmailErrorNotifier">
<property name="mailSender" ref="mailSender" />
</bean>
</beans>
The default port used by JavaMailSenderImpl is the standard SMTP port 25, so if your e-mail server
listens on this port for SMTP, you can simply omit this property. Also, if your SMTP server doesn't
require user authentication, you needn't set the username and password.
If you have a JavaMail session configured in your Java EE application server, you can first look it up
with the help of JndiObjectFactoryBean .
<bean id="mailSession"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="mail/Session" />
</bean>
Or you can look up a JavaMail session through the <jee:jndi-lookup> element if you are using
Spring 2.0 or later.
<jee:jndi-lookup id="mailSession" jndi-name="mail/Session" />
You can inject the JavaMail session into JavaMailSenderImpl for its use. In this case, you no longer
need to set the host, port, username, or password.
Search WWH ::




Custom Search