前提准备:
QQ邮箱
账号和密码
1、登录邮箱
2、点击“设置”,进入“账户”
3、获取授权码
4、 后端代码
maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
bootstrap.yml:
spring:
mail:
host: smtp.qq.com
port: 587
username: 1050100468@qq.com
password: ************
注:password 为 上面获取的授权码!!!?
MailService.java:
package application.service;
import lombok.RequiredArgsConstructor;
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.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
/**
* @author: wtl
* @License: (C) Copyright 2021, wtl Corporation Limited.
* @Contact: 1050100468@qq.com
* @Date: 2021/10/29 9:35 上午
* @Version: 1.0
* @Description:
*/
@Service
@RequiredArgsConstructor
public class MailService {
private final JavaMailSender mailSender;
public void sendMail(String subject,String from,String to) throws Exception{
String contentPage = "/Users/tlzs/Desktop/working/IdeaProjects/SpringBoot/src/main/resources/static/htmls/index.html";
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(contentPage));
int length = -1;
byte [] buffer = new byte[10240];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((length = bufferedInputStream.read(buffer))!= -1){
byteArrayOutputStream.write(buffer,0,length);
}
bufferedInputStream.close();
String emailContent = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = null;
try {
mimeMessageHelper = new MimeMessageHelper(message, true);
//设置发送来源账号
mimeMessageHelper.setFrom(from);
// 发往目的的账号
mimeMessageHelper.setTo(to);
//设置抄送人员账号
mimeMessageHelper.setCc(new String[]{"123@qq.com","456@qq.com"});
// 设置主题
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(emailContent, true);
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(message);
}
}
注意:邮箱是支持html代码和html代码时段的。
index.html:
<h1>Hello,world!!!</h1>
<a href="https://www.baidu.com" style="color: blue;">我的百度</a>
或者:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index页面</title>
</head>
<body>
<h1>Hello,world!!!</h1>
<a href="https://www.baidu.com" style="color: blue;">我的百度</a>
</body>
</html>
?如果以工具类的方式来进行初始化,会出现编码问题:
package cn.com.yusys.aiops.alarm.util;
import cn.com.yusys.aiops.alarm.notify.AlarmNotifyReceiver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
public class MailUtil {
private static final Logger logger = LoggerFactory.getLogger(MailUtil.class);
private static final JavaMailSenderImpl javaMailSender;
static {
javaMailSender = new JavaMailSenderImpl();
javaMailSender.setProtocol("smtp");
javaMailSender.setHost("mail.yusys.com.cn");
javaMailSender.setPort(25);
javaMailSender.setUsername("yucc-aiops-admin@yusys.com.cn");
javaMailSender.setPassword("Aiops123");
}
public static boolean mailSender(String sendTo,String subject,String msg) throws Exception {
try {
javaMailSender.setDefaultEncoding("utf-8");
// 创一个邮件对象
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = null;
try {
mimeMessageHelper = new MimeMessageHelper(message, true);
mimeMessageHelper.setFrom("yucc-aiops-admin@yusys.com.cn");
mimeMessageHelper.setTo(sendTo);
// mimeMessageHelper.setCc(new String[]{"123@qq.com","456@qq.com"});
//邮件主题.
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(msg, true);
} catch (MessagingException e) {
e.printStackTrace();
}
javaMailSender.send(message);//发送邮件
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public static void main(String[] args) throws Exception {
mailSender("yucc-aiops-admin@yusys.com.cn","中文测试","<!DOCTYPE html>\n" +
"<html lang=\"zh\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>告警内容</title>\n" +
"</head>\n" +
"<body>\n" +
"<p>你好</p>" +
"</body>\n" +
"</html>");
}
}
解决办法为:
javaMailSender.setDefaultEncoding("utf-8"); // 创一个邮件对象 MimeMessage message = javaMailSender.createMimeMessage();
在 javaMailSender.createMimeMessage();之前设置编码格式。
|