spring 配置 java mail 发送邮件

时间:2022-09-24 09:09:01

配置文件 mail-context.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


<!--①邮件服务器-->
<beans:bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<beans:property name="protocol" value="smtp" />
<beans:property name="host" value="smtp.exmail.qq.com" />
<beans:property name="port" value="465" />
<beans:property name="username" value="*****" />
<beans:property name="password" value="*****" />


<!-- <beans:property name="protocol" value="smtp" />
<beans:property name="host" value="smtp.163.com" />
<beans:property name="port" value="465" />
<beans:property name="username" value="*****@163.com" />
<beans:property name="password" value="*****" /> -->
<beans:property name="javaMailProperties">
<beans:props>
<beans:prop key="mail.smtp.auth">true</beans:prop>
<!-- 如果是网易邮箱, mail.smtp.starttls.enable 设置为 false-->
<beans:prop key="mail.smtp.starttls.enable">true</beans:prop>
<beans:prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!--②异步线程执行器 -->
<beans:bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<beans:property name="corePoolSize" value="10" />
<beans:property name="maxPoolSize" value="30" />
</beans:bean>

<beans:bean id="mailService" class="com.zse.ecmclient.ui.service.impl.MailServiceImpl">
<beans:property name="mailSender" ref="mailSender" />
<beans:property name="taskExecutor" ref="taskExecutor" />
</beans:bean>

</beans:beans>




配置文件 servlet-context.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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.xsd">

<!-- Enables the Spring MVC @Controller programming model -->
<context:component-scan base-package="com.zse.ecmclient.ui.web" />

<mvc:annotation-driven />

<beans:bean id="mailController" class="com.zse.ecmclient.ui.web.controller.MailController">
<beans:property name="mailService" ref="mailService" />
</beans:bean>
</beans:beans>


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("收件人过多,正在采用异步方式发送...<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;
}

}

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