老妖的博客
现实的中没有几个人能够真为对方去死,甚至山盟海誓很快就会在金钱面前变的微不足道,这才是生活。没有永远的爱,除了你的父母对你,当然也就没有永远的恨,更没有永远的痛,时间是最好的治疗大师,它会很快抚平你心灵上累累的伤痕。很多年以后你想起来时,那些在你生命中汹涌来往的人群至多是个模糊的影子或者毫无意义的名字
posts - 105,  comments - 171,  trackbacks - 0
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 老妖 阅读(3669) 评论(0)  编辑  收藏 所属分类: spring

只有注册用户登录后才能发表评论。


网站导航:
 

<2005年10月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

随笔分类(48)

随笔档案(104)

好友链接

我的豆瓣

积分与排名

  • 积分 - 218728
  • 排名 - 256

最新评论

阅读排行榜