首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

应用springMail发送普通邮件的两个例子

2012-06-26 
使用springMail发送普通邮件的两个例子使用springMail发送带附件的email:http://kukuqiu.iteye.com/blog/1

使用springMail发送普通邮件的两个例子
使用springMail发送带附件的email:http://kukuqiu.iteye.com/blog/161771
Spring邮件发送(可带附件,模板,群发,异步发送等功能):http://mengqingyu.iteye.com/blog/389273

例子一:http://kukuqiu.iteye.com/blog/161762
需要的spring的jar包有:spring.jar,mail.jar,commons-logging.jar,activation.jar
Java代码 

package mail;    import org.springframework.context.ApplicationContext;  import org.springframework.context.support.ClassPathXmlApplicationContext;  import org.springframework.mail.SimpleMailMessage;  import org.springframework.mail.javamail.JavaMailSender;    public class Main {        /**      * @param args      */      public static void main(String[] args) {          // TODO Auto-generated method stub          ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");          JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");          SimpleMailMessage mail = new SimpleMailMessage();          mail.setFrom("abcd@163.com");          mail.setTo("abcd@gmail.com");          mail.setSubject(" 测试spring Mail");          mail.setText("hello,java");          mailSender.send(mail);      }    }
 

config.xml配置文件:
Xml代码 
<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd"  >        <bean id="mailSender" value="smtp.163.com" />          <property name="port" value="25" />          <property name="username" value="abcd@163.com" />          <property name="password" value="你的密码" />          <property name="javaMailProperties">              <props>                  <prop key="mail.smtp.auth">true</prop>              </props>          </property>      </bean>  </beans> 






例子二:http://www.blogjava.net/rocky/archive/2005/10/29/17375.html
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" > <property name="mailSender"> <ref bean="mailSender" /> </property> <property name="message"> <ref bean="mailMessage" /> </property> </bean>  



在对应的action配置文件中加入:
<bean id="SendMailAction" singleton="false" > <property name="mailManager"> <ref bean="mailManager" /> </property> <property name="orderDao"> <ref bean="orderDao"/> </property> </bean> 


在xwork配置文件中:
<action name="sendMailBG" /> <result name="success" type="freemarker">success.ftl</result> <result name="error" type="freemarker">error.ftl</result> </action> 

热点排行