温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot实现发送邮件功能过程图解

发布时间:2020-09-10 15:10:46 来源:脚本之家 阅读:120 作者:chenjiahao 栏目:编程语言

首先创建一个邮箱账号,建议@126.com,@163.com,@qq.com 都可以

开启smtp,以下是使用图解:

SpringBoot实现发送邮件功能过程图解

SpringBoot实现发送邮件功能过程图解

创建SpringBoot项目导入依赖

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 支持发送邮件 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>

application.properties文件中配置:

spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.163.com
#发送者的邮箱密码
spring.mail.password=xxxxx
#端口
spring.mail.port=25
#协议
spring.mail.protocol=smtp
#发送者的邮箱账号
spring.mail.username=xxxxxxx@163.com
server.port=8081

 以文本的形式发送:

package com.example.demo;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author
 * @site
 * @company
 * @create 2020-03-07 1:06
 */
@RestController
public class MailController {
 
  @Autowired
  JavaMailSender jsm;
 
  @Value("${spring.mail.username}")
  private String username;
 
  @GetMapping("/send")
  public String send(){
    //建立邮箱消息
    SimpleMailMessage message = new SimpleMailMessage();
    //发送者
    message.setFrom(username);
    //接收者
    message.setTo("1352192872@qq.com");
    //发送标题
    message.setSubject("测试");
    //发送内容
    message.setText("测试数据");
    jsm.send(message);
    return "1";
  }
}

 结果:

发送方:

SpringBoot实现发送邮件功能过程图解

接收方:

SpringBoot实现发送邮件功能过程图解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI