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

[color=red]使用springMail发送带附件的emai[/color]l:[url]http://kukuqiu.iteye.com/blog/161771[/url]
[color=red]Spring邮件发送(可带附件,模板,群发,异步发送等功能)[/color]:[url]http://mengqingyu.iteye.com/blog/389273[/url]
使用腾讯企业邮箱 [url]http://blog.csdn.net/liuluoboliu/article/details/41118753[/url]

例子一:[url]http://kukuqiu.iteye.com/blog/161762[/url]
需要的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" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" 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>



例子二:[url]http://www.blogjava.net/rocky/archive/2005/10/29/17375.html[/url]
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) {
t his.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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值