springboot smtp 邮件

2 篇文章 0 订阅
1 篇文章 0 订阅

以下例子用foxmail发送 , QQ和163等其他都一样

1.pom.xml 添加maven坐标(thymeleaf模板. mail邮件)

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

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

2.application.yml  (host是 smtp.看foxmail图)

spring: 
 mail:
  host: mail.test.com
  password: paddy168
  username: paddy@test.com
  #port: 端口号
  default-encoding: UTF-8
  properties:
    mail.smtp.socketFactory.fallback : true
    mail.smtp.starttls.enable: true

 

3.整个逻辑.

a)test类

package com.dome;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Dome1 {

    @Autowired
    private JavaMailSender mailSender; //自动注入的Bean

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String Sender; //读取配置文件中的参数

    <!-- 简单文字-->
    @Test
    public void sendSimpleMail1() throws Exception {
       SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(Sender);
        message.setTo("john@qq.com"); //发送邮
        message.setSubject("主题:来自雯雯的问候"+i);
        message.setText("小志是个帅哥"+i);
        mailSender.send(message);
        System.out.println("ok");
    }

    <!-- 多个收件人, 多个秒抄人 , 内容下带图片 , 带附件-->
    @Test
    public void sendSimpleMail2()throws Exception{
        String[] filePath = new String[]{"D:\\fox.png"};
        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("to", new String[]{"john@qq.com", "john1@qq.com"});
        valueMap.put("cc",new String[]{"john2@qq.com"});
        valueMap.put("title", "标题");
        valueMap.put("content", "Dear admin,");
        valueMap.put("content1", "这是测试内容...");
        valueMap.put("filePathList", filePath);

        MimeMessage mimeMessage = null;
        try {
            mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            // 设置发件人邮箱
            helper.setFrom(Sender);
            // 设置收件人邮箱
            helper.setTo((String[])valueMap.get("to"));
            // 抄送邮件接收人
            helper.setCc((String[])valueMap.get("cc"));
            // 设置邮件标题
            helper.setSubject(valueMap.get("title").toString());

            // 添加正文(使用thymeleaf模板)mail为名
            Context context = new Context();
            context.setVariables(valueMap);
            String content = this.templateEngine.process("mail", context);
            helper.setText(content, true);      

            if (valueMap.get("filePathList") != null) {
                String[] filePathList = (String[]) valueMap.get("filePathList");
                for(String f: filePathList) {
                    FileSystemResource fileSystemResource = new FileSystemResource(new File(f));
                    // 添加附件
                    //String fileName = f.substring(f.lastIndexOf(File.separator));
                    //helper.addAttachment(fileName, fileSystemResource);

                    // 模板html
                    // 添加图片(注意代码是 img src="cid:test001")
                    FileSystemResource file = new FileSystemResource(new File(f));
                    helper.addInline("test001", file);
                }
            }

            // 发送邮件
            mailSender.send(mimeMessage);
            System.out.println("发送成功.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

b) mail.html(这是内容模板,参数自己带进来)  ,放在 thymeleaf

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${content}"></div>
<div th:text="${content1}"></div>
<div>
    Thanks
    <img src="cid:test001">
</div>
</body>
</html>

 

-------以上就完成啦. 如有问题 ,请留言哦.谢谢.

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值