也可参考文章—>
1.邮件发送的工具类
package com.yxb.makerhomesync.service.impl;
import com.yxb.base.util.StringUtil;
import com.yxb.makerhomesync.common.config.MyJavaMailSender;
import com.yxb.makerhomesync.service.EmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
/**
* 发送带附件的邮件
* @param to 邮件接收人的邮箱
* @param subject 邮件主题名称
* @param content 邮件内容
* @param filePath 文件的服务器路径
*/
@Service
public class EmailServiceImpl implements EmailService {
private static Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);
@Autowired
private MyJavaMailSender sender;
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
if(StringUtil.hasEmptyPara(to)){
return ;
}
logger.info("send accachments mail start");
MimeMessage message = sender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
helper.setFrom(sender.getUsername());
helper.setTo(to.split(","));
helper.setSubject(subject);
helper.setText(content, true);
File file = new File(filePath);
if(file.exists()){
FileSystemResource tempFile = new FileSystemResource(new File(filePath));
String fileName = file.getName();
helper.addAttachment(fileName, tempFile);
}
sender.send(message);
logger.info("send accachments mail end");
return;
} catch (MessagingException e) {
logger.error("系统报错,错误信息:{}",e);
return;
}
}
}
2.邮件发送具体实现类
package com.yxb.makerhomesync.common.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;
/**
* 实现多账号,轮询发送
*/
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class MyJavaMailSender extends JavaMailSenderImpl implements JavaMailSender {
private static Logger logger = LoggerFactory.getLogger(MyJavaMailSender.class);
private ArrayList<String> usernameList;
private ArrayList<String> passwordList;
private int currentMailId = 0;
private int currentMailId2 = 0;
private final MailProperties properties;
public MyJavaMailSender(MailProperties properties) {
this.properties = properties;
// 初始化账号
if (usernameList == null)
usernameList = new ArrayList<String>();
String[] userNames = this.properties.getUsername().split(",");
if (userNames != null) {
for (String user : userNames) {
usernameList.add(user);
}
}
// 初始化密码
if (passwordList == null)
passwordList = new ArrayList<String>();
String[] passwords = this.properties.getPassword().split(",");
if (passwords != null) {
for (String pw : passwords) {
passwordList.add(pw);
}
}
}
@Override
protected void doSend(MimeMessage[] mimeMessage, Object[] object) throws MailException {
super.setUsername(usernameList.get(currentMailId));
super.setPassword(passwordList.get(currentMailId));
logger.info("正在使用邮箱账号:"+super.getUsername()+"发送邮件.............");
currentMailId2 = (currentMailId == (usernameList.size()-1)? 0 : (currentMailId+1));
// 设置编码和各种参数
// super.setHost(this.properties.getHost());
super.setDefaultEncoding(this.properties.getDefaultEncoding().name());
super.setJavaMailProperties(asProperties(this.properties.getProperties()));
try {
super.doSend(mimeMessage, object);
} catch (MailException e) {
e.printStackTrace();
} finally {
currentMailId = currentMailId2;
}
}
private Properties asProperties(Map<String, String> source) {
Properties properties = new Properties();
properties.putAll(source);
return properties;
}
@Override
public String getUsername() {
return usernameList.get(currentMailId);
}
public int getCurrentMailId() {
return currentMailId;
}
public int getCurrentMailId2() {
return currentMailId2;
}
}
3.邮件发送配置文件:
spring.mail.username=xxxxx@xxxx.com
spring.mail.password=xxxxx
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.host=smtp.exmail.qq.com
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.port=465
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false