Send a simple message using the Spring message abstraction Layer

Source: Internet
Author: User
Tags contains final interface key mail mailmessage
Spring provides a high-level abstraction layer for sending e-mail messages that shields the user from the details of the underlying messaging system and is responsible for low-level resource processing on behalf of the client. The main package for the spring message Abstraction layer is org.springframework.mail. It includes the primary interface for sending e-mail MailSender and the properties that encapsulate simple messages such as from, TO,CC, subject, and the value object of text called Simplemailmessage.

1. We define an interface for sending mail: Ordermanager.java

1 public interface OrderManager extends basemanager{
2/**
3 *email, email address to send;
4 *code: Activation code
5 */
6 public void PlaceOrder (String email);
7}


2, we need to implement the method of this interface: Ordermanagerimpl.java

1 Import javax.mail.Message;
2 Import javax.mail.MessagingException;
3 Import javax.mail.internet.InternetAddress;
4 Import Javax.mail.internet.MimeMessage;
5 Import org.springframework.mail.MailException;
6 Import Org.springframework.mail.javamail.JavaMailSender;
7 Import Org.springframework.mail.javamail.MimeMessagePreparator;
8 Import Service. OrderManager;
9
One public class Ordermanagerimpl extends Basemanagerimpl implements OrderManager {
12
Private Javamailsender MailSender;
Private MyMailMessage message;
15
16
Setmessage public void (Citymailmessage message)
18 {
this.message = message;
20}
public void Setmailsender (Javamailsender mailsender) {
This.mailsender = MailSender;
23}
public void PlaceOrder (final String email) {
25
26
Mimemessagepreparator preparator = new Mimemessagepreparator () {
public void Prepare (MimeMessage mimemessage) throws Messagingexception {
Mimemessage.setrecipient (Message.RecipientType.TO,
New internetaddress (email));
To Mimemessage.setfrom (New InternetAddress (Message.getfrom ()));
32/** Conversion code for gbk*/
Mimemessage.setsubject (Message.getsubject (), "GBK");
Mimemessage.settext (email+ "<br>" +message.getsubject () +message.gettext (), "GBK");
37
38}
39};
try{
Mailsender.send (Preparator);
42}
A catch (Mailexception ex) {
//log it and go on
System.err.println (Ex.getmessage ());
46}
47}
48}


3. Spring Configuration Send Email applicationcontext-email.xml

1 <?xml version= "1.0" encoding= "UTF-8"?
2! DOCTYPE beans Public "-//spring//dtd bean//en"
3 "HTTP://WWW.SPRINGFRAMEWORK.ORG/DTD/SPRING-BEANS.DTD" >
4
5 <beans>
6 <bean id= "MailSender" class= "Org.springframework.mail.javamail.JavaMailSenderImpl"
7 <property name= "host" >
8 <value> smtp.163.com </value>
9 </property>
<property name= "username" >
<value> username </value>
</property>
<property name= "Password"
<value> Password </value>
</property>
<property name= "Javamailproperties" >
<props>
<prop key= "Mail.smtp.auth" >true </prop>
<prop key= "Mail.smtp.timeout" >25000 </prop>
</props>
</property>
</bean>
23
<bean id= "MailMessage" class= "Org.springframework.mail.SimpleMailMessage"
<property name= "from"
<value> Email </value>
</property>
<property name= "Subject"
<value> title </value>
</property>
<property name= "text" >
<value> content </value>
</property>
+ </bean>
47
<bean id= "OrderManager" class= "Cn.cityyouth.service.impl.OrderManagerImpl"
<property name= "MailSender"
<ref bean= "MailSender"/>
Wuyi </property>
<property name= "message" >
<ref bean= "MailMessage"/>
</property>
</bean>
56
</beans>


4, the final configuration of their own JSP page and action

1 package cn.cityyouth.web.action;
2
3 Import javax.servlet.http.HttpServletRequest;
4 Import Javax.servlet.http.HttpServletResponse;
5 Import Org.apache.struts.action.ActionForm;
6 Import Org.apache.struts.action.ActionForward;
7 Import org.apache.struts.action.ActionMapping;
8 Import Org.apache.struts.action.ActionMessage;
9 Import org.apache.struts.action.ActionMessages;
Import Com.test.service.OrderManager;
11
public class Sendmailaction extends Baseaction {
13
14/**
* Method Execute
16 *
* @param mapping
* @param form
* @param request
* @param response
* @return Actionforward
22 */
Public Actionforward Execute (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response) {
OrderManager omi= (OrderManager) This.getbean ("OrderManager");
String useremail= "123@163.com";
Omi.placeorder (UserEmail);
28}
29}


To this all the development to end.

The main package for the Sring message Abstraction Layer is: Org.springframework.mail package. It contains a core interface called MailSender for sending messages and a value object that contains simple message properties such as From,to,cc,subject,text called Simplemailmessage. This package also contains a hierarchy to check for exceptions that support a higher level of abstraction than the low-level mail system exception with root exception exists mailexception. Please refer to Javadocs for more information on the abnormal levels of miscellaneous messages.

Spring also supports a mailsender dedicated to JavaMail features such as MIME message sub-interfaces, named Org.springframework.javamail.JavaMailerSener. It also supports a ready callback interface for JavaMail MIME information, named Org.springframework.mail.JavaMail.MimeMessagePreparator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.