当前位置:   article > 正文

SpringBoot发送邮件(SpringBoot整合JavaMail)_springboot mail发邮件

springboot mail发邮件

QQ邮箱为例。

创建一个springboot的Module

在这里插入图片描述

导入maven坐标

导入JavaMail都maven坐标。

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

在这里插入图片描述

设置QQ邮箱

进入自己(发送邮件方)QQ邮箱。
在这里插入图片描述
在这里插入图片描述
找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务区域,如下:
在这里插入图片描述
点击开启POP3/SMTP服务,并按要求发送短信。
在这里插入图片描述
在这里插入图片描述
发送成功后,点击“我已发送”会得到如下授权码(保存好密码配置application.yml时会用),然后点击确定。
在这里插入图片描述

做配置

在application中做如下配置。

host处如果是qq邮箱就写smtp.qq.com如果是126邮箱就写smtp.126.com依此类推。
在这里插入图片描述

代码编写

发送简单邮件(文字类邮件)

整体目录结构

在这里插入图片描述

service接口(发送邮件)

package com.xxx.springboot_mail.service;

public interface SendMailService {

    //发送邮件
    void sendMail();

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

接口的实现

1、发送人,配置文件中的username写的什么,这里就写什么。
在这里插入图片描述
2、接收人,给谁发就写谁的。
3、可根据具体要求完善代码,这里只做简单测试。

package com.xxx.springboot_mail.service.impl;

import com.xxx.springboot_mail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class SendMailServiceImpl implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;//注入JavaMailSender

    //发送人
    private String from="xxxxxx@qq.com";
    //接收人
    private String to="yyyyyy@qq.com";
    //标题
    private String subject = "测试邮件";
    //正文
    private String context="测试邮件正文内容";


    @Override
    public void sendMail() {

        SimpleMailMessage message = new SimpleMailMessage();

        //谁发的
        message.setFrom(from);
        //message.setFrom(from+"(abc)");abc将代替from
        //发给谁
        message.setTo(to);
        //邮件标题
        message.setSubject(subject);
        //邮件内容
        message.setText(context);

        //调用JavaMailSender的send方法即可发送邮件
        javaMailSender.send(message);

    }
}

  • 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

测试

package com.xxx.springboot_mail;

import com.xxx.springboot_mail.service.SendMailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootMailApplicationTests {

    @Autowired
    private SendMailService sendMailService;

    @Test
    void contextLoads() {
        sendMailService.sendMail();
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

发送复杂邮件

发送图片与链接

正文内容支持html
如下:
发送链接
在这里插入图片描述
发送图片(src中为网络文件链接)
private String context="<img src='https://img.zcool.cn/community/01775d57c3d1cd0000018c1bcb4ade.jpg@1280w_1l_2o_100sh.jpg'>百度</a>";//发送图片

注意设置setText参数为true
在这里插入图片描述

package com.xxx.springboot_mail.service.impl;

import com.xxx.springboot_mail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

@Service
public class SendMailServiceImpl02 implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;//注入JavaMailSender

    //发送人
    private String from="xxx@qq.com";
    //接收人
    private String to="yyy@qq.com";
    //标题
    private String subject = "测试邮件";
    //正文(支持html)setText设置参数true
    private String context="测试邮件正文内容";
    //private String context="<a href='http://www.baidu.com'>百度</a>";发送链接
    //private String context="<img src='https://img.zcool.cn/community/01775d57c3d1cd0000018c1bcb4ade.jpg@1280w_1l_2o_100sh.jpg'>百度</a>";发送图片


    @Override
    public void sendMail() {

        try{

            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message);

            //谁发的
            helper.setFrom(from);
            //message.setFrom(from+"(abc)");abc将代替from
            //发给谁
            helper.setTo(to);
            //邮件标题
            helper.setSubject(subject);
            //邮件内容
            helper.setText(context,true);

            javaMailSender.send(message);

        }catch(Exception e){

            e.printStackTrace();

        }

    }
}

  • 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

发送附件

在上面基础上设置如下。

1、创建MimeMessageHelper对象时增加true参数。
在这里插入图片描述
2、添加附件
在这里插入图片描述
注意addAttachment第一个参数的后缀一定要与你原文件的后缀相同。

package com.xxx.springboot_mail.service.impl;

import com.xxx.springboot_mail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class SendMailServiceImpl02 implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;//注入JavaMailSender

    //发送人
    private String from="xxxxxx@qq.com";
    //接收人
    private String to="yyyyyy@qq.com";
    //标题
    private String subject = "测试邮件";
    //正文(支持html)setText设置参数true
    //private String context="测试邮件正文内容";
    //private String context="<a href='http://www.baidu.com'>百度</a>";//发送链接
    private String context="<img src='https://img.zcool.cn/community/01775d57c3d1cd0000018c1bcb4ade.jpg@1280w_1l_2o_100sh.jpg'>百度</a>";//发送图片


    @Override
    public void sendMail() {

        try{

            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,true);

            //谁发的
            helper.setFrom(from);
            //message.setFrom(from+"(abc)");abc将代替from
            //发给谁
            helper.setTo(to);
            //邮件标题
            helper.setSubject(subject);
            //邮件内容
            helper.setText(context,true);

            File f1 = new File("E:\\xxxx\\a2.png");
            File f2 = new File("E:\\xxxx\\b1.jar");
            helper.addAttachment("文件名1.png",f1);//文件名叫什么(注意后缀),文件
            helper.addAttachment("文件名2.jar",f2);//文件名叫什么(注意后缀),文件

            javaMailSender.send(message);

        }catch(Exception e){

            e.printStackTrace();

        }
    }
}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/95397
推荐阅读
相关标签
  

闽ICP备14008679号