Friday, July 30, 2010

Sending e-mails in Java with Spring – GMail SMTP server example Read more: Java Code Geeks: Sending e-mails in Java with Spring – GMail SMTP-Part 1

For e-mail sending in Java, the JavaMail API is the standard solution. As the official web page states, "The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications". The necessary classes are included in the JavaEE platform, but to use it in a standalone JavaSE application you will have to download the corresponding JAR from here.

Note: Unless you're using Java SE 6, you will also need the JavaBeans Activation Framework (JAF) extension that provides the javax.activation package. We suggest you use version 1.1.1 of JAF, the latest release. JAF is included with Java SE 6.

JavaMail can unfortunately be a little cumbersome and difficult to configure and use. In case you have embraced the Spring framework for your applications, you will be glad to find out that Spring provides a mail abstraction layer. As the reference documentation states, "The Spring Framework provides a helpful utility library for sending email that shields the user from the specifics of the underlying mailing system and is responsible for low level resource handling on behalf of the client." Great, let's see now how to leverage this library.

First, create a new Eclipse project named "SpringMailProject". In the project's classpath, make sure to include the following libraries (note that the 3.0.2.RELEASE Spring version was used):

  • mail.jar (the core JavaMail classes)
  • org.springframework.asm-3.0.2.RELEASE.jar
  • org.springframework.beans-3.0.2.RELEASE.jar
  • org.springframework.context.support-3.0.2.RELEASE.jar
  • org.springframework.core-3.0.2.RELEASE.jar
  • org.springframework.expression-3.0.2.RELEASE.jar
  • com.springsource.org.apache.commons.logging-1.1.1.jar

Note1: The commons-logging library from Apache is needed and was included in the classpath
Note2: You will also need the Java Activation Framework if you are using Java 1.5 or earlier

We will use MailSender, a Spring interface that defines a strategy for sending simple mails. Since this is just an interface, we need a concrete implementation and that comes in the form of JavaMailSenderImpl. This class supports both JavaMail MimeMessages and Spring SimpleMailMessages.

We will create a simple Spring service that will be used for sending mail. One method will create a SimpleMailMessage on the spot, while the other will use a preconfigured default message. The source code for the service is the following:

01package com.javacodegeeks.spring.mail;
02
03import org.springframework.beans.factory.annotation.Autowired;
04import org.springframework.mail.MailSender;
05import org.springframework.mail.SimpleMailMessage;
06import org.springframework.stereotype.Service;
07
08@Service("mailService")
09public class MailService {
10
11 @Autowired
12 private MailSender mailSender;
13 @Autowired
14 private SimpleMailMessage alertMailMessage;
15
16 public void sendMail(String from, String to, String subject, String body) {
17
18 SimpleMailMessage message = new SimpleMailMessage();
19
20 message.setFrom(from);
21 message.setTo(to);
22 message.setSubject(subject);
23 message.setText(body);
24 mailSender.send(message);
25
26 }
27
28 public void sendAlertMail(String alert) {
29
30 SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);
31 mailMessage.setText(alert);
32 mailSender.send(mailMessage);
33
34 }
35
36}

The class is just a POJO and its service name is "mailService", marked by the stereotype Service annotation. The beans wiring strategy used is the one of autowiring, thus the Autowired annotation is used. Note that the autowiring can be performed using both the name and the type of a bean.


Read more: Java Code Geeks: Sending e-mails in Java with Spring – GMail SMTP server example http://www.javacodegeeks.com/2010/07/java-mail-spring-gmail-smtp.html#ixzz0vF1bg0aR

No comments:

Post a Comment