栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

创建SpringBoot项目导入依赖


      org.springframework.boot
      spring-boot-starter-web
    
    
    
      org.springframework.boot
      spring-boot-starter-mail
    

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;
 

@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";
  }
}

 结果:

发送方:

接收方:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134924.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号