spring mvc javamail配置

1.Spring的集成功能 

    在Spring的spring-context-support-3.2.4.RELASE.jar包中提供了Spring对cache,mail, schedule,ui的集成。

    Spring对JavaMail的支持的类在:org.springframework.mail和org.springframework.mail.javamail包中。

 

  2.Spring对JavaMail集成的实现

     主要有两个接口和接口的实现类

    接口:org.springframework.mail.MailMessage

    实现类:org.springframework.mail.SimpleMailMessage

    接口:org.springframework.mail.javamail.JavaMailSender

    实现类:org.springframework.mail.javamail.JavaMailSender.JavaMailSenderImpl


本个小源码是spring 和spring mvc 分开管理IOC容器, springmvc.xml 只负责管理Controller注解 其他注解则由spring.xml 管理

springmvc.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	
	<!-- 配置spring mvc扫描的注解 -->
	<context:component-scan base-package="baozi.mail" use-default-filters="false">
		<context:include-filter type="annotation" 
		expression="org.springframework.stereotype.Controller"/>
		<context:include-filter type="annotation" 
		expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置mailService 到controller -->
	<bean id="mailController" class="baozi.mail.hander.MailController">  
        <property name="mailService" ref="mailService" />  
    </bean> 
    
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

spring.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<context:component-scan base-package="baozi.mail">
		<context:exclude-filter type="annotation" 
			expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" 
			expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>

	<!-- 配置邮箱服务器 -->
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
			scope="singleton">
		<property name="host" value="smtp.163.com"></property>
		<property name="port" value="25"></property>		
        <property name="protocol" value="smtp" />
        <property name="defaultEncoding" value="utf-8"></property>                
        <property name="username" value="*****@163.com"></property>
        <property name="password" value="*****"></property>
	</bean>
	
	<!--②异步线程执行器 ,这里配置这个暂时用不到,代码在service里有但是注释了没有用-->  
    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
        <property name="corePoolSize" value="10" />  
        <property name="maxPoolSize" value="30" />  
    </bean> 
    
	<bean id="mailService" class="baozi.mail.mailservcice.impl.MailServiceImpl">  
        <property name="mailSender" ref="mailSender" />  
        <property name="taskExecutor" ref="taskExecutor" />  
    </bean>  

</beans>
package baozi.mail.beans;

public class Email {

	/** 发件人 **/
	private String fromAddress;
	
	/** 收件人 **/
	private String toAddress;

	/** 邮件主题 **/
	private String subject;

	/** 邮件内容 **/
	private String content;

	
	
	public String getFromAddress() {
		return fromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
}

package baozi.mail.hander;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import baozi.mail.beans.Email;
import baozi.mail.mailservcice.MailService;



@Controller
public class MailController {

	private MailService mailService;

	@RequestMapping(value="/hello", method=RequestMethod.GET)
	public void getSendMail(final Model model, final HttpServletRequest request, 
			final HttpServletResponse response) {

		this.postSendMail(model, request, response);
	}
	
	@RequestMapping(value="/hello", method=RequestMethod.POST)
	public void postSendMail(final Model model, final HttpServletRequest request, 
			final HttpServletResponse response) {
		
		Email email = new Email();
		//String toAddress = request.getParameter("email");
		if(true) {//toAddress != null) {
			email.setToAddress("****@qq.com");
			email.setFromAddress("****@163.com");
			email.setSubject("正在注册账户");
			email.setContent("正在注册账户,您的激活码为xxxxxx");
			try {
				mailService.sendMail(email);
				System.out.println("发送邮件");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public MailService getMailService() {
		return mailService;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}
}

package baozi.mail.mailservcice.impl;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import baozi.mail.beans.Email;
import baozi.mail.mailservcice.MailService;


public class MailServiceImpl implements MailService {

	private JavaMailSender mailSender;// 注入Spring封装的javamail,Spring的xml中已让框架装配
	private TaskExecutor taskExecutor;// 注入Spring封装的异步执行器

	private Log log = LogFactory.getLog(getClass());
	private StringBuffer message = new StringBuffer();

	@Override
	public void sendMail(Email email) throws MessagingException, IOException {
		/*if (email.getAddress().length > 5) {// 收件人大于5封时,采用异步发送
			sendMailByAsynchronousMode(email);
			this.message.append("收件人过多,正在采用异步方式发送...<br/>");
		} else {*/
		sendMailBySynchronizationMode(email);
		//this.message.append("正在同步方式发送邮件...<br/>");
		//}
	}

	
	@Override
	public void sendMailByAsynchronousMode(final Email email) {
		taskExecutor.execute(new Runnable() {
			public void run() {
				try {
					sendMailBySynchronizationMode(email);
				} catch (Exception e) {
					log.info(e);
				}
			}
		});
	}

	@Override
	public void sendMailBySynchronizationMode(Email email)
			throws MessagingException, IOException {
		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mime, true, "utf-8");
		
		helper.setFrom(email.getFromAddress());// 发件人
		helper.setTo(email.getToAddress());// 收件人
		helper.setReplyTo(email.getFromAddress());// 回复到
		helper.setSubject(email.getSubject());// 邮件主题
		helper.setText(email.getContent(), true);// true表示设定html格式
		mailSender.send(mime);
	}

	
	
	
	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public TaskExecutor getTaskExecutor() {
		return taskExecutor;
	}

	public void setTaskExecutor(TaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}

	public Log getLog() {
		return log;
	}

	public void setLog(Log log) {
		this.log = log;
	}

	public StringBuffer getMessage() {
		return message;
	}

	public void setMessage(StringBuffer message) {
		this.message = message;
	}

}

package baozi.mail.mailservcice;

import java.io.IOException;

import javax.mail.MessagingException;

import baozi.mail.beans.Email;

public interface MailService {

	void sendMail(Email email) throws MessagingException, IOException;

	void sendMailByAsynchronousMode(Email email);

	void sendMailBySynchronizationMode(Email email) throws MessagingException, IOException;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值