需要要spring包和javax.maill包
配置文件:applicationContext.properties
#mail config
mail.protocol=smtpmail.host=smtp.qq.com
mail.port=465
mail.username=XXXXX@qq.com
mail.name=XXXXX系统
mail.password=********
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.timeout=25000
#mail.smtp.socketFactory.class=javax.Net.ssl.SSLSocketFactory
spring xml文件:
<!-- 读取属性文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:application.properties" />
</bean>
<!--邮件服务器 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="protocol" value="${mail.protocol}" />
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
<prop key="#mail.smtp.socketFactory.class">${#mail.smtp.socketFactory.class}</prop>
</props>
</property>
</bean>
<!-- 异步线程执行器 -->
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="30" />
</bean>
mail服务接口:
import java.io.IOException;
import java.util.List;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
/**
* 邮件
*
* @author Shi Zezhu
* @date 2017年7月14日 上午11:22:52
*/
public interface MailService {
/**
* 发送邮件
*
* @param to 收件
* @param cc 抄送
* @param subject 主题
* @param htmlflag 是否HTLM内容
* @param content 内容
* @author Shi Zezhu
* @date 2017年7月14日 下午4:35:50
*/
void sendMail(InternetAddress[] to, InternetAddress[] cc, String subject, boolean htmlflag,
String content) MessagingException, IOException;
/**
* 发送邮件
*
* @param to 收件
* @param cc 抄送
* @param subject 主题
* @param content 内容
* @param htmlflag 是否HTLM内容
* @author Shi Zezhu
* @date 2017年7月14日 下午4:35:50
*/
void sendMail(String[] to, String[] cc, String subject, String content, boolean htmlflag)
throws MessagingException, IOException;
/**
* 发送邮件
*
* @param to 收件
* @param subject 主题
* @param htmlflag 是否HTLM内容
* @param content 内容
* @throws MessagingException
* @throws IOException
* @author Shi Zezhu
* @date 2017年7月15日 上午11:31:35
*/
void sendMail(List<InternetAddress> to, String subject, boolean htmlflag, String content)
throws MessagingException, IOException;
/**
* 发送邮件
*
* @param to 收件
* @param subject 主题
* @param htmlflag 是否HTLM内容
* @param content 内容
* @throws MessagingException
* @throws IOException
* @author Shi Zezhu
* @date 2017年7月15日 上午11:31:35
*/
void sendMail(InternetAddress to, String subject, boolean htmlflag, String content)
throws AjaxResultException, MessagingException, IOException;
/**
* 发送邮件
*
* @param to 收件
* @param cc 抄送
* @param subject 主题
* @param htmlflag 是否HTLM内容
* @param content 内容
* @author Shi Zezhu
* @date 2017年7月14日 下午4:35:50
*/
void sendMail(List<InternetAddress> to, List<InternetAddress> cc, String subject, boolean htmlflag,
String content) throws MessagingException, IOException;
/**
* 发送邮件
*
* @param to 收件
* @param subject 主题
* @param content 内容
* @param htmlflag 是否HTLM内容
* @throws MessagingException
* @throws IOException
* @author Shi Zezhu
* @date 2017年7月15日 上午11:32:02
*/
void sendMail(List<String> to, String subject, String content, boolean htmlflag)
throws MessagingException, IOException;
/**
* 发送邮件
*
* @param to 收件
* @param subject 主题
* @param content 内容
* @param htmlflag 是否HTLM内容
* @throws MessagingException
* @throws IOException
* @author Shi Zezhu
* @date 2017年7月15日 上午11:32:02
*/
void sendMail(String to, String subject, String content, boolean htmlflag)
throws AjaxResultException, MessagingException, IOException;
/**
* 发送邮件
*
* @param to 收件
* @param cc 抄送
* @param subject 主题
* @param content 内容
* @param htmlflag 是否HTLM内容
* @author Shi Zezhu
* @date 2017年7月14日 下午4:35:50
*/
void sendMail(List<String> to, List<String> cc, String subject, String content,
boolean htmlflag) throws MessagingException, IOException;
}
mail服务实现:
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
;
import cn.szz.security.service.MailService;
import cn.szz.security.utils.ErrorCode;
import cn.szz.security.utils.PropertiesUtils;
@Service
public class MailServiceImpl implements MailService {
@Resource
private JavaMailSender mailSender;
@Resource
private TaskExecutor taskExecutor;
@Override
public void sendMail(InternetAddress[] to, InternetAddress[] cc, String subject, boolean htmlflag,
String content) throws AjaxResultException, MessagingException, IOException {
if (to == null || to.length == 0) {
return;
}
if (cc == null) {
cc = new InternetAddress[]{};
}
if (StringUtils.isBlank(subject)) {
return;
}
if (StringUtils.isBlank(content)) {
return;
}
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mime, true, "UTF-8");
helper.setFrom(new InternetAddress(PropertiesUtils.getProperty("mail.username"), PropertiesUtils.getProperty("mail.name"), "UTF-8"));
helper.setTo(to);
helper.setCc(cc);
helper.setSubject(subject);
helper.setText(content, htmlflag);
this.sendMail(mime);
}
@Override
public void sendMail(String[] to, String[] cc, String subject, String content,
boolean htmlflag) throws AjaxResultException, MessagingException, IOException {
if (to == null || to.length == 0) {
return;
}
if (cc == null) {
cc = new String[]{};
}
if (StringUtils.isBlank(subject)) {
return;
}
if (StringUtils.isBlank(content)) {
return;
}
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mime, true, "UTF-8");
helper.setFrom(PropertiesUtils.getProperty("mail.username"), PropertiesUtils.getProperty("mail.name"));
helper.setTo(to);
helper.setCc(cc);
helper.setSubject(subject);
helper.setText(content, htmlflag);
this.sendMail(mime);
}
@Override
public void sendMail(List<InternetAddress> to, String subject, boolean htmlflag,
String content) throws AjaxResultException, MessagingException, IOException {
this.sendMail(to.toArray(new InternetAddress[to.size()]), null, subject, htmlflag, content);
}
@Override
public void sendMail(InternetAddress to, String subject, boolean htmlflag,
String content) throws AjaxResultException, MessagingException, IOException {
this.sendMail(new InternetAddress[]{to}, null, subject, htmlflag, content);
}
@Override
public void sendMail(List<InternetAddress> to, List<InternetAddress> cc, String subject, boolean htmlflag,
String content) throws AjaxResultException, MessagingException, IOException {
this.sendMail(to.toArray(new InternetAddress[to.size()]), cc.toArray(new InternetAddress[cc.size()]), subject, htmlflag, content);
}
@Override
public void sendMail(List<String> to, String subject, String content,
boolean htmlflag) throws AjaxResultException, MessagingException, IOException {
this.sendMail(to.toArray(new String[to.size()]), null, subject, content, htmlflag);
}
@Override
public void sendMail(String to, String subject, String content,
boolean htmlflag) throws AjaxResultException, MessagingException, IOException {
this.sendMail(new String[]{to}, null, subject, content, htmlflag);
}
@Override
public void sendMail(List<String> to, List<String> cc, String subject, String content,
boolean htmlflag) throws AjaxResultException, MessagingException, IOException {
this.sendMail(to.toArray(new String[to.size()]), cc.toArray(new String[cc.size()]), subject, content, htmlflag);
}
private void sendMail(final MimeMessage mime) {
taskExecutor.execute(new Runnable() {
public void run() {
mailSender.send(mime);
}
});
}
}