1.配置maven依赖
<!-- javax.mail -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置 application.properties
#e-mail
mail.smtp.auth=true
mail.transport.protocol=smtp
mail.send.charset=UTF-8
mail.smtp.port=465
mail.is.ssl=true
mail.host=smtp.163.com
#发送者的邮箱账户
mail.auth.name=11111111111@163.com
#发送者的邮箱密码
mail.auth.password=*******
#发送超时设置
mail.smtp.timeout=5000
3.工具类 EmailUtils
import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author lk
* @Description
*/
@Component
public class SendEmailUtils {
private static final Logger logger = LoggerFactory.getLogger(SendEmailUtils.class);
@Autowired
private Environment envEmail;
private static String auth;
private static String host;
private static String protocol;
private static int port;
private static String authName;
private static String password;
private static boolean isSSL;
private static String charset ;
private static String timeout;
@PostConstruct
public void initParam () {
auth = envEmail.getProperty("mail.smtp.auth");
host = envEmail.getProperty("mail.host");
protocol = envEmail.getProperty("mail.transport.protocol");
port = envEmail.getProperty("mail.smtp.port", Integer.class);
authName = envEmail.getProperty("mail.auth.name");
password = envEmail.getProperty("mail.auth.password");
charset = envEmail.getProperty("mail.send.charset");
isSSL = envEmail.getProperty("mail.is.ssl", Boolean.class);
timeout = envEmail.getProperty("mail.smtp.timeout");
}
/**
* 发送邮件
* @param subject 主题
* @param toUsers 收件人
* @param ccUsers 抄送
* @param content 邮件内容
* @param attachfiles 附件列表 List<Map<String, String>> key: name && file
*/
public static boolean sendEmail(String subject, String[] toUsers, String[] ccUsers, String content, List<Map<String, String>> attachfiles) {
boolean flag = true;
try {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(host);
javaMailSender.setUsername(authName);
javaMailSender.setPassword(password);
javaMailSender.setDefaultEncoding(charset);
javaMailSender.setProtocol(protocol);
javaMailSender.setPort(port);
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", auth);
properties.setProperty("mail.smtp.timeout", timeout);
if(isSSL){
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}
javaMailSender.setJavaMailProperties(properties);
MimeMessage mailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
messageHelper.setTo(toUsers);
if (ccUsers != null && ccUsers.length > 0) {
messageHelper.setCc(ccUsers);
}
messageHelper.setFrom(authName);
messageHelper.setSubject(subject);
messageHelper.setText(content, true);
if (attachfiles != null && attachfiles.size() > 0) {
for (Map<String, String> attachfile : attachfiles) {
String attachfileName = attachfile.get("name");
File file = new File(attachfile.get("file"));
messageHelper.addAttachment(attachfileName, file);
}
}
javaMailSender.send(mailMessage);
} catch (Exception e) {
logger.error("发送邮件失败!", e);
flag = false;
}
return flag;
}
}
4.测试
package com.demo.springboot;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootApplicationTests {
@Test
public void contextLoads() {
boolean isSend = EmailUtils.sendEmail("我是一封测试邮件", new String[]{"接收者邮箱11111111111@163.com"}, null, "我是邮件的内容", null);
}
}