当前位置:   article > 正文

SpringBoot实现发送邮件功能_springboot邮箱发送

springboot邮箱发送

SpringBoot使用JavaMailSender发送邮件

在学习过程中,竟然发现springboot可以发送邮件!!于是记录下来,方便以后翻阅!

首先spring提供了一个非常好用的接口----JavaMailSender,用于发送邮件,本文就讲解一下如何快速上手JavaMailSender

这里我是利用网易163邮箱给qq邮箱发送邮件

包括普通文本邮件、附件邮件、图片展示在表面的邮件

1、导入依赖

使用springboot发送邮箱,需要在pom.xml中导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4

2.在yml中配置属性

这一步需要我们先给邮箱开通SMTP服务,这里我使用的是网易的163邮箱,

因为我已经开通就不给大家演示了,这一步大家可以网上搜索:“如何开通网易163的SMTP服务器”,按照步骤开通即可,非常简单,两三步就能完成。
假设我们已经开通完毕,我们还需要在springboot的application.yml文件中写明配置信息,如下:

 #邮箱
spring:
  mail:
    default-encoding: utf-8
    host: smtp.163.com
    username: *******@163.com #开通smtp的邮箱
    password: PIMQXJZBRCULUSXE #开通时显示的授权密码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.编写控制类

这里展示了三种发送邮件的方法

  • sendMail() :发送文本邮件
  • sendMail2():发送文本+附件
  • sendMail3() :发送文本+

其中值得一提的是JavaMailSender,是我们上边导入依赖时,spring中的自动注入的接口,发送邮件我们需要用到这个接口中的send(message)方法

/**
 * Description: 控制层web方法
 * @author ltc
 * @version 1.0
 * @date 2023/3/31 14:18
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private JavaMailSender mailSender;
    
    /**
    *发送邮件(简单的文本邮件)
    **/
    @PostMapping("/sendmail")
    public void sendMail(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("13331755335@163.com");
        message.setTo("1771967998@qq.com");
        message.setSubject("这是个测试");
        message.setText("hello,这是文本内容);
        try{
            mailSender.send(message);//发送邮件
            System.out.println("发送成功");
        }
        catch (Exception e){
             System.out.println("发送失败");
        }
    }
                        
    /**
    *将发送的图片添加到邮件的附件中
    **/                    
    @PostMapping("/sendmail2")
    public void sendMail2() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        helper.setFrom("13331755335@163.com"); //设置发送者
        helper.setTo("1771967998@qq.com"); //设置接收者
        helper.setSubject("主题:送你一张图片"); //设置邮件的主题
        helper.setText("你好,这是文本,送你一张图片"); //设置邮件的内容

        FileSystemResource file = new FileSystemResource(new File("D:\\imgs\\图片2.jpg"));//获取本地图片
        helper.addAttachment("附件1",file); //给邮件添加附件
        try{
            mailSender.send(mimeMessage);
             System.out.println("发送成功");
        }catch (Exception e){
            System.out.println("发送失败");
        }
    }
                        
    /**
    *将发送的图片展示在表面
    **/
    @PostMapping("/sendmail3")
    public void sendMail3() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("13331755335@163.com");
        helper.setTo("1771967998@qq.com");
        helper.setSubject("主题:图片显示在文本上");
        helper.setText("<html><body><img src=\"cid:hello\" ></body></html>",true);
        FileSystemResource file = new FileSystemResource(new File("D:\\imgs\\图片2.jpg"));
        //addInline的第一个参数一定要和上边的src一致
        helper.addInline("hello",file);
        try{
            mailSender.send(mimeMessage);
            System.out.println("发送成功");
        }catch (Exception e){
            System.out.println("发送失败");
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

最后,大家就可以启动服务,访问自己的controller方法测试一下。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/523985
推荐阅读
相关标签
  

闽ICP备14008679号