Java Reference
In-Depth Information
Sending E-mail Using the JavaMail API
Now let's take a look at how to send e-mail using the JavaMail API. You can implement the
ErrorNotifier interface to send e-mail notifications in case of errors.
Note To use JavaMail in your application, you have to include mail.jar and activation.jar (located in the
lib/j2ee directory of the Spring installation) in your classpath.
package com.apress.springenterpriserecipes.replicator;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailErrorNotifier implements ErrorNotifier {
public void notifyCopyError(String srcDir, String destDir, String filename) {
Properties props = new Properties();
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.username", "system");
props.put("mail.smtp.password", "12345");
Session session = Session.getDefaultInstance(props, null);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("system@localhost"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("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);
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Search WWH ::




Custom Search