spring 配置 java mail 发送邮件

配置文件 mail-context.xml






	
	
		
		
		
		
		
		
		
		
		
			
				true
				
				true
				javax.net.ssl.SSLSocketFactory
			
		
	
	
	
		
		
	
	
	
		
		
	




配置文件 servlet-context.xml





	
	
	
	
	
	
		
	


java 代码:


Email.java


package com.zse.ecmclient.ui.web.common.models;

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;
	}
}



MaliServiceImpl.java

package com.zse.ecmclient.ui.service.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 com.zse.ecmclient.ui.service.MailService;
import com.zse.ecmclient.ui.web.common.models.Email;

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("收件人过多,正在采用异步方式发送...
"); } else {*/ sendMailBySynchronizationMode(email); //this.message.append("正在同步方式发送邮件...
"); //} } @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; } }

MailController.java


package com.zse.ecmclient.ui.web.controller;

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 com.zse.ecmclient.ui.service.MailService;
import com.zse.ecmclient.ui.web.common.models.Email;

@Controller
public class MailController {

	private MailService mailService;

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

		this.postSendMail(model, request, response);
	}
	
	@RequestMapping(value="/ui/sendmail.jhtml", 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("******@54cook.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;
	}
}

原文地址:http://howsun.blog.sohu.com/129043957.html

你可能感兴趣的:(Java)