邮件传输协议:
springboot
请自行参考其他博客
1.使用maven创建springboot项目/注册邮箱开通pop3stmp
2.添加,导入主要依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
3.添加相关配置参数application
spring.mail.host=smtp.126.com
spring.mail.username=********@126.com
#注意:需要126开通授权码服务填入下面
spring.mail.password=*****
spring.mail.default-encoding=UTF-8
4.发送邮件服务示例
import com.honghe.managerTool.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
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;
/**
* @author: zhaojianyu
* @create: 2018-09-10 10:43
**/
@Service
public class SendMailServiceImpl implements SendMailService{
@Value("${spring.mail.username}")
private String whoAmI;
@Autowired
private JavaMailSender mailSender;
/**
* 发送简单邮件
* @param to 发送给谁
* @param subject 邮件主题
* @param content 邮件内容
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(content);
message.setFrom(whoAmI);
mailSender.send(message);
}
/**
* 发送html邮件
* @param to
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setFrom(whoAmI);
helper.setSubject(subject);
helper.setText(content,true);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送附件邮件
* @param to
* @param subject
* @param content
* @param filePath
*/
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setFrom(whoAmI);
helper.setSubject(subject);
helper.setText(content,true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
//此处可以添加多个附件 zjy0910
helper.addAttachment(fileName,file);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送图片邮件
* @param to
* @param subject
* @param content
* @param rscPath 图片路径
* @param rscId
*/
@Override
public void sendInlinResourceMail(String to, String subject, String content,
String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setFrom(whoAmI);
helper.setSubject(subject);
helper.setText(content,true);
//可以添加多个图片
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId,res);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
使用方法
//发送简单邮件
sendMailService.sendSimpleMail("ai10*****[email protected]","标题","内容");
//发送html邮件
String content = "\n" +
"\n"
+"html邮件内容
\n"
+"";
sendMailService.sendHtmlMail("1******[email protected]","给你的",content);
//发送附件邮件示例
sendMailService.sendAttachmentsMail("117***[email protected]","给你的",content,"C:\\Users\\Administrator\\Pictures\\999.jpg");
//发送图片邮件示例
String resId ="id001";
String content = "\n" +
"html邮件内容
+resId+"\'>" +
"";
//发图片邮件
sendMailService.sendInlinResourceMail("ai*****[email protected]","标题",
content,"C:\\Users\\Administrator\\Pictures\\\\999.jpg",resId);
1.添加依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
3.html文件编写(采用thymeleaf)
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>告警邮件title>
head>
<body>
您好,您的服务器磁盘或内存达到危险值,请检查服务器内存和磁盘情况!<br>
<a href="#" th:href="@{https://www.baidu.com/}">进入查看a>
<a href="#" th:href="@{https://www.baidu.com/{id}(id=${id})}">进入查看2a>
body>
html>
4.服务层编写(采用发送html邮件的方法)
/**
* 发送html邮件
* @param to
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setFrom(whoAmI);
helper.setSubject(subject);
helper.setText(content,true);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
5.控制层代码展示
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
{
@Autowired
private SendMailService sendMailService;
@Autowired
//注入模板引擎
private TemplateEngine templateEngine;
@RequestMapping("/test")
public Object test(){
Context context = new Context();
//给模板html内注入值
context.setVariable("id","006");
//获取html内容
String emailContent = templateEngine.process("emailTemplate",context);
//发送邮件
sendMailService.sendHtmlMail("ai******[email protected]","模板邮件-邮件主题",emailContent);
return "ok";
}
}
与本文类似的相关参考