Spring Boot 菜鸟教程 26 集成邮件发送

GitHub

src="//ghbtns.com/github-btn.html?user=je-ge&repo=spring-boot&type=watch&count=true" scrolling="0" width="110" height="20">

需求产生

发送邮件的需求比较常见,如找回密码、事件通知等

添加Maven email依赖包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

修改配置文件application.properties

#Spring集成邮件发送的相关配置

#使用163邮箱,修改自己的邮箱帐号和密码
#spring.mail.host=smtp.163.com
#spring.mail.username=username@163.com
#spring.mail.password=password

#如果使用qq邮箱,修改自己的邮箱帐号和授权码
spring.mail.host=smtp.qq.com
spring.mail.username=1272434821@qq.com
#授权码是QQ邮箱推出的,用于登录第三方客户端的专用密码。
spring.mail.password=axfbjuksoiovoooo

spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

需要将username@163.com和password修改成自己的163的邮箱帐号和密码
或者使用qq邮箱,这个需要授权码
http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

发送简单邮件

package com.jege.spring.boot.email;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:邮件发送测试类
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SendMailTest {

  @Autowired
  private JavaMailSender javaMailSender;

  @Value("${spring.mail.username}")
  private String username;

  @Test
  public void testSendSimple() {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(username);
    message.setTo("1272434821@qq.com");
    message.setSubject("标题");
    message.setText("内容部份");
    javaMailSender.send(message);
  }
}

发送附件

发送模板邮件

其他关联项目

源码地址

https://github.com/je-ge/spring-boot

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
微信打赏
支付宝打赏

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值