项目地址
- 码云
https://gitee.com/txiaoqiang/springboot_mail.git
(发送邮件)
- 码云
https://gitee.com/txiaoqiang/javamails.git
(读取邮件,发送邮件)
邮件发送关键点
- 使用spring boot 项目,咱们能够节约不少的工做,spring boot将一些基础的配置都帮咱们约束好了,咱们只须要按照标准去配置便可。
- 项目主配置文件配置(application.yml ),这里我将演示三种不一样类型的邮箱帐号发送邮件:
spring:
mail:
host: smtp.exmail.qq.com
username: tangz***g@wei***ada.net
password: ETQpK***gxYGd
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
port: 465
protocol: smtps
mail:
fromMail:
addr: tang***ng@w***.net
receptionMail:
addr: 332***4141@qq.com
复制代码
- QQ 普通邮箱
复制代码
spring:
mail:
host: smtp.qq.com
username: 191***933@qq.com
password: twuhvbfgndkwbgbf
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
mail:
fromMail:
addr: 191****33@qq.com
复制代码
- 网易 163 邮箱
复制代码
spring:
mail:
host: smtp.163.com
username: 1****61@163.com
password: ****.
mail:
fromMail:
addr: 1****61@163.com
复制代码
QQ企业邮箱客户端专用码申请(关于申请 QQ企业邮箱不作多的介绍)
- 第一步:登陆企业邮箱后点击
设置
并找到 客户端设置
肯定开启了 POP/SMTP
服务。
QQ普通邮箱开启受权码
- 第一步:点击
设置
> 帐户
,向下拉,找到 POOP/SMTP
服务并开启。
- 第二步:点击
生成受权码
,按照官方的提示完成验证便可生成 受权码
。
项目pom.xml文件准备
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
复制代码
mail 服务接口定义
public interface MailService {
public void sendTextMail(String toAddr, String title, String content);
public void sendHtmlMail(String toAddr, String title, String content);
public void sendAttachmentsMail(String toAddr, String title, String content, String filePath);
public void sendInlineResourceMail(String toAddr, String title, String content, String rscPath, String rscId);
}
复制代码
服务接口的实现
@Component
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 sendTextMail(String toAddr, String title, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(toAddr);
message.setSubject(title);
message.setText(content);
try {
mailSender.send(message);
logger.info("Text邮件已经发送。");
} catch (Exception e) {
logger.error("发送Text邮件时发生异常!", e);
}
}
@Override
public void sendHtmlMail(String toAddr, String title, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(toAddr);
helper.setSubject(title);
helper.setText(content, true);
mailSender.send(message);
logger.info("html邮件发送成功");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}
public void sendAttachmentsMail(String toAddr, String title, String content, String filePath){
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(toAddr);
helper.setSubject(title);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
mailSender.send(message);
logger.info("带附件的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送带附件的邮件时发生异常!", e);
}
}
public void sendInlineResourceMail(String toAddr, String title, String content, String rscPath, String rscId){
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(toAddr);
helper.setSubject(title);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
logger.info("嵌入静态资源的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送嵌入静态资源的邮件时发生异常!", e);
}
}
}
复制代码
测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class BasemailApplicationTests {
@Value("${mail.receptionMail.addr}")
private String receptionMailAddr;
@Autowired
private MailService mailService;
@Autowired
private TemplateEngine templateEngine;
@Test
public void testSimpleMail() throws Exception {
mailService.sendTextMail(receptionMailAddr,"测试文本邮箱发送","你好你好!");
}
@Test
public void testHtmlMail() throws Exception {
String content="<html>\n" +
"<body>\n" +
" <h3>hello world ! 这是一封html邮件!</h3>\n" +
"</body>\n" +
"</html>";
mailService.sendHtmlMail(receptionMailAddr,"test simple mail",content);
}
@Test
public void sendAttachmentsMail() {
String filePath="C:\\\\Users\\\\Administrator\\\\Desktop\\\\java并发学习.txt";
mailService.sendAttachmentsMail(receptionMailAddr, "主题:带附件的邮件", "有附件,请查收!", filePath);
}
@Test
public void sendInlineResourceMail() {
String rscId = "neo006";
String content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";
String imgPath = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\testMail.png";
mailService.sendInlineResourceMail(receptionMailAddr, "主题:这是有图片的邮件", content, imgPath, rscId);
}
@Test
public void sendTemplateMail() {
Context context = new Context();
context.setVariable("id", "006");
String emailContent = templateEngine.process("emailTemplate", context);
mailService.sendHtmlMail(receptionMailAddr,"主题:这是模板邮件",emailContent);
}
}
复制代码