——————来自一个小白(●'◡'●)
首先,当然是添加<依赖(starter模块…)>啦~
>
>org.springframework.boot >
>spring-boot-starter-mail >
>
>
>org.springframework >
>spring-context-support >
>RELEASE >
>
>
>com.sun.mail >
>javax.mail >
>RELEASE >
>
蓝后,添加配置文件(以163邮箱为例)
spring.application.name=mail
spring.mail.host=smtp.163.com
[email protected]
spring.mail.password="注意这里!你邮箱的客户端授权密码哟"
spring.mail.default-encoding=UTF-8
[email protected]
再蓝后,附一下代码
咳咳…service层
package com.example.mail.service;
public interface MailService {
void sendSimpleMail(String to, String subject, String content);
void sendHtmlMail(String to, String subject, String content);
void sendAttachmentsMail(String to, String subject, String content, String filePath);
void sendInlineResourceMail(String to, String subject, String content, String resPath, String resId);
}
package com.example.mail.service.impl;
import com.example.mail.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
@Service
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JavaMailSender mailSender;
@Value("${mail.fromMail.addr}")
private String from;
/**
* 发送最简单的文本邮件
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
try{
SimpleMailMessage simpleMailMessage= new SimpleMailMessage();
simpleMailMessage.setFrom(from);
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
mailSender.send(simpleMailMessage);
logger.info("邮件已发送成功!");
} catch (Exception e) {
logger.error("发送邮件时发生异常!",e);
}
}
/**
* 发送html邮件
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//这里的true表示需要创建一个multipart message
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(content);
mailSender.send(message);
logger.info("邮件发送成功! ");
} catch (MessagingException e) {
logger.error("发送邮件时发生异常!", e);
}
}
/**
* 发送带有附件的邮件
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath){
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content,true);
FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
mimeMessageHelper.addAttachment(fileName,fileSystemResource);
mailSender.send(message);
logger.info("邮件已经发送成功! ");
} catch (MessagingException e) {
logger.error("发送邮件时发生异常!", e);
}
}
/**
* 发送邮件中带有图片的邮件
*/
public void sendInlineResourceMail(String to, String subject, String content, String resPath, String resId){
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper Helper = new MimeMessageHelper(message, true);
Helper.setFrom(from);
Helper.setTo(to);
Helper.setSubject(subject);
Helper.setText(content,true);
FileSystemResource resource = new FileSystemResource(new File(resPath));
Helper.addAttachment(resId,resource);
mailSender.send(message);
logger.info("邮件已经发送成功! ");
} catch (MessagingException e) {
logger.error("发送邮件时发生异常!", e);
}
}
}
最后,就是测试一下啦(●’◡’●)
package com.example.mail.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@SpringBootTest
public class MailServiceTest {
@Autowired
private MailService mailService;
@Test
public void testSimpleMail() throws Exception {
mailService.sendSimpleMail("[email protected]","测试发送简单邮件"," Hello, this is simple mail!");
}
@Test
public void testHtmlMail() throws Exception {
String content="\n" +
"\n" +
" hello world ! 这是一封html邮件!
\n" +
"\n" +
"";
mailService.sendHtmlMail("[email protected]","测试发送html邮件",content);
}
@Test
public void sendAttachmentsMail() {
String filePath="d:\\BaiduNetdiskDownload\\spring-boot-book.pdf";
mailService.sendAttachmentsMail("[email protected]", "主题: 您有一封邮件待查看", "附上example文件,请查收! ", filePath);
}
@Test
public void sendInlineResourceMail() {
String resId = "dan.J";
String content="\n" +
"\n" +
"这是一封有图片的邮件哦! O(∩_∩)O + resId + "\' >\n" +
"\n" +
"";
String imgPath = "C:\\Users\\Amanda\\Pictures\\Camera Roll\\test.jpg";
mailService.sendInlineResourceMail("[email protected]", "主题: 您有一封邮件待查看", content, imgPath, resId);
}
最最后,附上效果图
Ⅰ.发送简单文本邮件
Ⅱ.发送html邮件
Ⅲ.发送带有附件的邮件
Ⅳ.发送附有图片的邮件(下载即可)
先介绍这里吧,多的也不会了hhhhh…