Java Reference
In-Depth Information
You first open a mail session connecting to an SMTP server by defining the properties. Then you
create a message from this session for constructing your e-mail. After that, you send the e-mail by
making a call to Transport.send() . When dealing with the JavaMail API, you have to handle the checked
exception MessagingException . Note that all these classes, interfaces, and exceptions are defined by
JavaMail.
Next, declare an instance of EmailErrorNotifier in the Spring IoC container for sending e-mail
notifications in case of file replication errors.
<bean id="errorNotifier"
class="com.apress.springenterpriserecipes.replicator.EmailErrorNotifier" />
You can write the following Main class to test EmailErrorNotifier . After running it, you can configure
your e-mail application to receive the e-mail from your James Server via POP3.
package com.apress.springenterpriserecipes.replicator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
ErrorNotifier errorNotifier =
(ErrorNotifier) context.getBean("errorNotifier");
errorNotifier.notifyCopyError(
"c:/documents", "d:/documents", "spring.doc");
}
}
Sending E-mail with Spring's MailSender
Now let's see how to send e-mail with the help of Spring's MailSender interface, which can send
SimpleMailMessage in its send() method. With this interface, your code is no longer JavaMail specific,
and now it's easier to test.
package com.apress.springenterpriserecipes.replicator;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class EmailErrorNotifier implements ErrorNotifier {
private MailSender mailSender;
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
Search WWH ::




Custom Search