SpringBoot集成邮件发送——oh-my-email

SpringBoot集成邮件发送,选取oh-my-email这个库。
GitHub链接:https://github.com/biezhi/oh-my-email
GitHub上有作者的详细步骤,很详细,跟着步骤走,就能实现功能。
这里做一下笔记记录。

1.新建项目

新建SpringBoot项目
添加maven依赖

<!--添加on my email 依赖-->
<dependency>
   <groupId>io.github.biezhi</groupId>
    <artifactId>oh-my-email</artifactId>
    <version>0.0.4</version>
</dependency>

2.新建实现类EmailController

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import io.github.biezhi.ome.OhMyEmail;
import io.github.biezhi.ome.SendMailException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import static io.github.biezhi.ome.OhMyEmail.SMTP_QQ;

/**
 * @author: 
 * Date: 2021/1/3 11:30
 * @version:
 * @Description: 邮件控制层
 */
@RestController
public class EmailController {

    /**
     * 该邮箱修改为你需要测试的邮箱地址 即接收方的邮箱地址
     */
    private static final String TO_EMAIL = "xxxx@126.com";

    @PostConstruct
    public void init() {
        // 配置,一次即可 your@password不是指QQ邮箱的密码,是指QQ邮箱的授权码
        OhMyEmail.config(SMTP_QQ(false), "xxxx@qq.com", "your@password");
    }

    @GetMapping("testSendText")
    public String testSendText() throws SendMailException {
        OhMyEmail.subject("这是一封测试TEXT邮件")
                .from("小姐姐的邮箱")
                .to(TO_EMAIL)
                .text("信件内容")
                .send();
        return "发送成功";
    }

    @GetMapping("testSendHtml")
    public String testSendHtml() throws SendMailException {
        OhMyEmail.subject("这是一封测试HTML邮件")
                .from("小姐姐的邮箱")
                .to(TO_EMAIL)
                .html("<h1 font=red>信件内容</h1>")
                .send();
        return "发送成功";
    }

    @GetMapping("testSendAttach")
    public String testSendAttach() throws SendMailException {
        OhMyEmail.subject("这是一封测试附件邮件")
                .from("小姐姐的邮箱")
                .to(TO_EMAIL)
                .html("<h1 font=red>信件内容</h1>")
                .attach(new File("/Users/biezhi/Downloads/hello.jpeg"), "测试图片.jpeg")
                .send();
        return "发送成功";
    }

    @GetMapping("testSendAttachURL")
    public String testSendAttachURL() throws SendMailException {
        try {
            OhMyEmail.subject("这是一封测试网络资源作为附件的邮件")
                    .from("小姐姐的邮箱")
                    .to(TO_EMAIL)
                    .html("<h1 font=red>信件内容</h1>")
                    .attachURL(new URL("https://avatars1.githubusercontent.com/u/2784452?s=40&v=4"), "测试图片.jpeg")
                    .send();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return "发送成功";
    }

    @GetMapping("testPebble")
    public String testPebble() throws IOException, PebbleException, SendMailException {
        PebbleEngine engine = new PebbleEngine.Builder().build();
        PebbleTemplate compiledTemplate = engine.getTemplate("register.html");

        Map<String, Object> context = new HashMap<String, Object>();
        context.put("username", "biezhi");
        context.put("email", "admin@biezhi.me");

        Writer writer = new StringWriter();
        compiledTemplate.evaluate(writer, context);

        String output = writer.toString();
        System.out.println(output);

        OhMyEmail.subject("这是一封测试Pebble模板邮件")
                .from("小姐姐的邮箱")
                .to(TO_EMAIL)
                .html(output)
                .send();
        return "发送成功";
    }
}

3.启动SpringBoot项目

用Postman进行测试。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值