一、依赖包
<!-- 邮件服务 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
二、配置文件
#邮箱信息
#QQ邮件服务地址
spring.mail.host: smtp.exmail.qq.com
#用户名
spring.mail.username: 邮箱用户名
#密码(授权码)
spring.mail.password: 邮箱密码
#默认编码UTF-8
spring.mail.default: encoding=UTF-8
#端口
spring.mail.port: 465
#邮件发送者
spring.mail.mailFrom: 发件方
#邮件发送者名称
spring.mail.mailFromName: 发送方名称
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: true
spring.mail.properties.mail.smtp.starttls.required: true
#SSL证数
spring.mail.properties.mail.smtp.ssl.enable: true
spring.mail.properties.mail.imap.ssl.socketFactory.fallback: false
spring.mail.properties.mail.smtp.ssl.socketFactory.class: com.fintech.modules.base.util.mail.MailSSLSocketFactory
三、代码
package com.zbjf.common.utils;
import java.io.File;
import java.util.List;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class EmailUtils {
private static Logger log = LoggerFactory.getLogger(EmailUtils.class);
private JavaMailSender mailSender;//spring 提供的邮件发送类
private String from;
/**
* @param mailFrom
* @param mailFromName
*/
public EmailUtils(JavaMailSender mailSender,String mailFrom,String mailFromName)
{
this.from = mailFromName+"<"+mailFrom+">"; //mailFrom;
this.mailSender=mailSender;
}
/**
* 发送简单邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
public void sendSimpleEmail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();//创建简单邮件消息
message.setFrom(from);//设置发送人
message.setTo(to);//设置收件人
/* String[] adds = {"[email protected]","[email protected]"}; //同时发送给多人
message.setTo(adds);*/
message.setSubject(subject);//设置主题
message.setText(content);//设置内容
try {
mailSender.send(message);//执行发送邮件
} catch (Exception e) {
log.error("发送简单邮件异常:"+e.toString());
}
}
/**
* 发送Html邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
public void sendHtmlEmail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();//创建一个MINE消息
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
} catch (MessagingException e) {
log.error("发送Html邮件异常:"+e.toString());
} catch(Exception e)
{
log.error("发送Html邮件异常:"+e.toString());
}
}
/**
* 发送带附件Html邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
public void sendAttachmentsEmail(String to, String subject, String content,List<String> filePathList) {
try {
MimeMessage message = mailSender.createMimeMessage();//创建一个MINE消息
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);// true表示这个邮件是有附件的
if(filePathList != null && filePathList.size() > 0)
{
for(String filePath:filePathList)
{
File f =new File(filePath);
if(f.exists())
{
/*FileSystemResource file = new FileSystemResource(new File(filePath));//创建文件系统资源*/
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
byte[] aa= FileUtils.getBytes(f);
helper.addAttachment(fileName,new ByteArrayResource(aa));//添加附件
}
}
}
mailSender.send(message);
} catch (MessagingException e) {
log.error("发送带附件Html邮件异常1:",e);
} catch(Exception e)
{
log.error("发送带附件Html邮件异常2:",e);
}
}
}