当前位置:   article > 正文

java spring 邮箱_使用Spring邮件发送Email

spring.mail.exception.to

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文件里加上如下的内容:

smtp服务器

用户名

密码

/**如果服务器要求验证,加上此**/

true

25000

class="org.springframework.mail.SimpleMailMessage">

你的电子邮件地址

邮件标题

在对应的action配置文件中加入:

class=" test.mail.action.SendMailAction" singleton="false" >

在xwork配置文件中:

success.ftl

error.ftl

posted on 2005-10-29 20:58 老妖 阅读(3523) 评论(0)  编辑  收藏 所属分类: spring

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/546050
推荐阅读
相关标签
  

闽ICP备14008679号