原文地址:http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc
Table of contents:
1.Spring framework’s support for e-mail
3.Creating e-mail sending form
4.Configuring SMTP server settings and Spring MVC
5.Creating Spring MVC controller class
6.Creating result page and error page
8.Download Eclipse project/WAR file
This tutorial provides a sample spring MVC application that allows user sending an e-mail message by filling a web form. The e-mail form looks like following screenshot:
In this tutorial, you are supposed to familiar with Java EE development as well as developing Spring MVC-based applications.
1. Spring framework’s support for e-mail
Based on JavaMail, Spring framework provides high-level abstraction API which greatly simplifies e-mail sending process. Let’s take a brief look at this API in the following class diagram:
To send e-mail messages, we can use an implementation of interface MailSender – the JavaMailSenderImpl class which is built upon on JavaMail. It’s convenient to configure this implementation as a bean in Spring’s context:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
This bean holds properties for SMTP and JavaMail and can be injected to a business/service class which needs to send an e-mail, for example:
1 |
|
In which email is an object of a type that implements MailMessage interface, such as SimpleMailMessage class. We can construct the email object as follows:
1 2 3 4 |
|
That’s for a simple mail message (plain text). In case if we want to send HTML e-mail or attach files to the e-mail, we can use MimeMailMessage class with the help of MimeMessagePreparator class and MimeMessageHelper class. For example, sending an e-mail in HTML format with an attachment:
1 2 3 4 5 6 7 8 9 10 |
|
The following table summarizes the interfaces and classes provided in org.springframework.mail package:
org.springframework.mail |
|
MailSender |
MailMessage |
JavaMailSender |
SimpleMailMessage |
JavaMailSenderImpl |
MimeMailMessage |
MimeMessagePreparator |
MimeMessageHelper |
Click on a link in the table to see API documentation for the corresponding interface/class.
2. Required jar files
The application requires the following jar files copied to its WEB-INF\lib directory:
Required jar files |
|
JavaMail |
mail.jar |
Spring framework |
spring-beans-3.2.0.RELEASE.jar spring-context-3.2.0.RELEASE.jar spring-context-support-3.2.0.RELEASE.jar spring-core-3.2.0.RELEASE.jar spring-expression-3.2.0.RELEASE.jar spring-web-3.2.0.RELEASE.jar spring-webmvc-3.2.0.RELEASE.jar |
Apache Commons Logging |
commons-logging-1.1.1.jar |
NOTE: Click on a hyperlink in the table above to download the corresponding software.
The sample application we are going to build contains the following key files:
-
- EmailForm.jsp: displays an e-mail form.
- Result.jsp: shows successful message after the e-mail has been sent.
- Error.jsp: shows error message in case of an exception is thrown.
- SendEmailController.java: the Spring controller class that takes input from e-mail form, calls Spring’s mailSender to send the e-mail, and redirects user to either successful page or error page.
- spring-mvc.xml: Spring’s context configuration file. Here we will configure SMTP server settings and various properties for JavaMail.
- web.xml: web deployment descriptor file.
3. Creating e-mail sending form
Create a JSP file called EmailForm.jsp with the following HTML code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
This is a simple form with three fields: To, Subject and Message – which are necessary attributes for a simple outgoing e-mail message. On submitting this form, the action named “sendEmail.do” will be called, as specified by the form’s actionattribute. We will implement a Spring controller class for handling this action in the next section.
4. Configuring SMTP server settings and Spring MVC
Create a Spring context configuration file called spring-mvc.xml with the following XML code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
This configuration is pretty straightforward:
-
- <context:component-scan ... />: tells Spring to scan the package net.codejava.spring for initializing components which are annotated by Spring annotations (It’s a Spring controller class, in case of this application).
- Bean mailSender: this is the important part because it declares a Spring bean for e-mail implementation – theJavaMailSenderImpl class and configures SMTP server settings, which is for a Gmail account in this case. This bean will be injected to a Spring controller class which will be covered in the next section.
- Bean viewResolver: maps logical view names to real JSP file names.
- Bean SimpleMappingExceptionResolver: maps all exceptions of type java.lang.Exception to be handled byError.jsp page.
The web deployment descriptor file (web.xml) is configured as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
It declares Spring controller servlet with its context configuration file (/WEB-INF/spring-mvc.xml). The controller is configured to handle all requests whose URL end with pattern: *.do. And the default page when accessing the application is the email form (EmailForm.jsp).
5. Creating Spring MVC controller class
In order to handle submission from the e-mail form, we need to create a Spring controller class as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
This controller class is quite simple. It is declared as a Spring MVC controller by the annotation @Controller, and is mapped to the e-mail form’s action by the @RequestMapping annotation. We inject the mailSender bean declared inspring-mvc.xml file into this controller through the private field also named mailSender. The injection is done automatically by Spring as we use the @Autowired annotation.
The method doSendEmail()is responsible for capturing input from e-mail form, creating a SimpleMailMessage object and sending the e-mail by invoking the send() method on the mailSender bean. The e-mail is in plain text format. Finally, it returns a view named “Result” which causes Spring to use the viewResolver to find and load appropriate JSP file (Result.jsp).
6. Creating result page and error page
Code the Result.jsp file as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
And code the Error.jsp as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
As we can see, the result page simply tells the user that the e-mail has been sent, while the error page displays an error message if any exception thrown during the process of sending e-mail.
7. Run the application
So far we have created all the key pieces of the application. Let’s deploy it on a servlet container like Tomcat, and access the application by typing the following URL into browser’s address bar (your host name and port number maybe different, depending on server configuration):
http://localhost:8080/EmailSpringMVC
The e-mail form is displayed, type in required information:
Hit Send E-mail button, it may take a while for the e-mail to be sent. A successful message comes from the result page in case everything is going well:
In case of error (such as network failure or the SMTP server could not be reached), the error page displays:
You can download the sample application as an Eclipse project or deployable WAR file in the attachment section, and remember to update SMTP settings to match your e-mail account.