Spring提供了一个发送电子邮件的高级抽象层,它向用户屏蔽了底层邮件系统的一些细节,同时负责低层次的代表客户端的资源处理。Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender和 封装了简单邮件的属性如from, to,cc, subject, text的值对象叫做SimpleMailMessage。
首先:我们定义一个发送邮件的接口:IMailManager.java
/*
* IMailManager.java
* Copyright 2005, All rights reserved.
*/
package test.mail.manager;
import test.common.logic.IManager;
import test.model.Order;
/**
* Note:this interface mainly deal with the sendOrder
*/
public interface IMailManager extends IManager{
void sendOrder(Order order);
}
然后实现这个接口:MailManager.java
/*
* MailManager.java
* Copyright 2005, All rights reserved.
*/
package test.mail.manager;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import test.common.logic.impl.Manager;
import test.model.Order;
/**
* Note:the implements of IMailManager
*/
public class MailManager extends Manager implements IMailManager {
private MailSender mailSender;
private SimpleMailMessage message;
public void sendOrder(Order order) {
SimpleMailMessage mailMessage = new SimpleMailMessage(this.message);
mailMessage.setTo(order.getUser().getEmail());
mailMessage.setText("Dear"
+ order.getUser().getFirstName()
+ order.getUser().getLastName()
+ ", thank you for placing order. Your order code is "
+ order.getCode());
try{
mailSender.send(mailMessage);
}catch(MailException ex) {
System.err.println(ex.getMessage());
}
}
/**
* @param mailSender The mailSender to set.
*/
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
/**
* @param message The message to set.
*/
public void setMessage(SimpleMailMessage message) {
this.message = message;
}
}
然后我们在Action 里面调用: SendMailAction.java
/*
* SendMail.java
* Copyright 2005, All rights reserved.
*/
package test.mail.action;
import test.common.action.BaseAction;
import test.mail.manager.IMailManager;
import test.order.dao.IOrderDao;
import test.model.Order;
/**
* Note: SendMailAction
*/
public class SendMailAction extends BaseAction {
private IMailManager mailManager;
private IOrderDao orderDao;
private long orderId;
public String execute() throws Exception {
Order order = orderDao.getOrder(orderId);
mailManager.sendOrder(order);
return SUCCESS;
}
/**
* @return Returns the mailManager.
*/
public IMailManager getMailManager() {
return mailManager;
}
/**
* @param mailManager The mailManager to set.
*/
public void setMailManager(IMailManager mailManager) {
this.mailManager = mailManager;
}
/**
* @return Returns the orderDao.
*/
public IOrderDao getOrderDao() {
return orderDao;
}
/**
* @param orderDao The orderDao to set.
*/
public void setOrderDao(IOrderDao orderDao) {
this.orderDao = orderDao;
}
/**
* @return Returns the orderId.
*/
public long getOrderId() {
return orderId;
}
/**
* @param orderId The orderId to set.
*/
public void setOrderId(long orderId) {
this.orderId = orderId;
}
}
最后的就是配置了.在ApplicationContext.xml文件里加上如下的内容:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host"><value>smtp服务器</value></property>
<property name="username"><value>用户名</value></property>
<property name="password"><value>密码</value></property>
/**如果服务器要求验证,加上此**/
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
<bean id="mailMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<value>你的电子邮件地址</value>
</property>
<property name="subject">
<value>邮件标题</value>
</property>
</bean>
<bean id="mailManager" class=" test.mail.manager.MailManager" >
<property name="mailSender">
<ref bean="mailSender" />
</property>
<property name="message">
<ref bean="mailMessage" />
</property>
</bean>
在对应的action配置文件中加入:
<bean id="SendMailAction"
class=" test.mail.action.SendMailAction" singleton="false" >
<property name="mailManager">
<ref bean="mailManager" />
</property>
<property name="orderDao">
<ref bean="orderDao"/>
</property>
</bean>
在xwork配置文件中:
<action name="sendMailBG" class="SendMailAction">
<interceptor-ref name="defaultStack" />
<result name="success" type="freemarker">success.ftl</result>
<result name="error" type="freemarker">error.ftl</result>
</action>
posted on 2005-10-29 20:58
老妖 阅读(3676)
评论(0) 编辑 收藏 所属分类:
spring