五、springboot实现发送带图片的邮件

目录

1.引入依赖

2.发件人账号信息写入配置文件

3.demo

4.调用示例


1.引入依赖

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

2.发件人账号信息写入配置文件

spring:
    mail:
        default-encoding: utf-8  #设置编码格式
        host: smtp.qq.com        #这里用的是QQ邮箱,所以是qq.com
        password: *************  #注意此密码不是邮箱登陆密码,而是邮箱的授权码
        username: *******@qq.com #邮箱账号

3.demo

  /**
     * 功能描述:发送带图片的邮件
     *
     * @param to      发送目标邮箱
     * @param subject 邮件标题
     * @param content 邮件内容
     * @param imgPath 图片路径
     * @param imgId   图片id,在img标签里使用
     * @throws MessagingException the messaging exception
     * @author: ***
     * @createTime: 2018 /11/8 9:18
     */
    public void sendImageMail(String to, String subject, String content, String imgPath, String imgId) throws MessagingException {
        //创建message
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        //发件人
        helper.setFrom(from);
        //收件人
        helper.setTo(to);
        //标题
        helper.setSubject(subject);
        //true指的是html邮件,false指的是普通文本
        helper.setText(content, true);
        //添加图片
        FileSystemResource file = new FileSystemResource(new File(imgPath));
        helper.addInline(imgId, file);
        //发送邮件
        javaMailSender.send(message);
    }

4.调用示例

mailService.sendImageMail("5***17@qq.com", "helloWorld", "<h1 style='color:red'>helloWorld</h1><img src='cid:test001'/>","C:\\Users\\LFG\\Desktop\\test.png","test001");

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值