一分钟集成qq邮箱

一分钟集成qq邮箱

在这里插入图片描述

maven坐标

    <!-- Jakarta Mail API -->
        <dependency>
            <groupId>jakarta.mail</groupId>
            <artifactId>jakarta.mail-api</artifactId>
            <version>2.1.2</version>
        </dependency>

qq邮箱授权码申请地址
在这里插入图片描述
在这里插入图片描述

EmailUtils

import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.time.Year;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.stream.Collectors;

public class EmailUtil {

    private static final String HOST = "smtp.qq.com";
    private static final String PORT = "587";
    private static final String FROM_EMAIL = "你的QQ@qq.com";
    private static final String PASSWORD = "邮箱授权码";

    // 读取HTML模板文件
    private static String loadTemplate(String templatePath) throws Exception {
        try (InputStream is = EmailUtil.class.getClassLoader().getResourceAsStream(templatePath)) {
            if (is == null) {
                throw new Exception("Template file not found: " + templatePath);
            }
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
                return reader.lines().collect(Collectors.joining("\n"));
            }
        }
    }

    // 替换模板中的变量
    private static String processTemplate(String template, Map<String, String> variables) {
        String result = template;
        for (Map.Entry<String, String> entry : variables.entrySet()) {
            result = result.replace("${" + entry.getKey() + "}", entry.getValue());
        }
        return result;
    }

    public static void sendHtmlMail(String toEmail, String subject, String title, String content) throws Exception {
        // 加载模板
        String template = loadTemplate("templates/email-template.html");

        // 准备变量
        Map<String, String> variables = new HashMap<>();
        variables.put("title", title);
        variables.put("content", content);
        variables.put("year", String.valueOf(Year.now().getValue()));

        // 处理模板
        String htmlContent = processTemplate(template, variables);

        // 发送邮件配置
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(FROM_EMAIL, PASSWORD);
            }
        });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(FROM_EMAIL));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject(subject);

        // 创建HTML内容
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(htmlContent, "text/html; charset=utf-8");

        // 创建Multipart
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(htmlPart);

        message.setContent(multipart);
        Transport.send(message);
    }

    public static void main(String[] args) throws Exception {
        // 发送HTML格式邮件
        EmailUtil.sendHtmlMail(
                "321395678@qq.com",
                "<<邮箱demo>>",
                "玩个鸡腿🍗",
                "验证码:" + UUID.randomUUID().toString()
        );
        System.out.println("邮件发送成功!");
    }
}

resource目录下templates中的模板文件:email-template.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #f8f8f8;
        }

        .container {
            width: 100%;
            max-width: 680px;
            margin: 0 auto;
            font-family: 'Arial', sans-serif;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            background-color: #ffffff;
        }

        .header {
            background: linear-gradient(135deg, #1a237e 0%, #283593 100%);
            color: white;
            padding: 30px 20px;
            text-align: center;
            border-top-left-radius: 8px;
            border-top-right-radius: 8px;
        }

        .logo {
            margin-bottom: 20px;
        }

        .header h1 {
            margin: 0;
            font-size: 28px;
            font-weight: 600;
            letter-spacing: 1px;
            text-transform: uppercase;
        }

        .content {
            padding: 40px;
            background-color: #ffffff;
            color: #333333;
            line-height: 1.6;
            font-size: 16px;
        }

        .divider {
            height: 2px;
            background: linear-gradient(to right, #1a237e 0%, #283593 100%);
            margin: 30px 0;
        }

        .footer {
            text-align: center;
            padding: 30px 20px;
            background-color: #f8f9fa;
            border-bottom-left-radius: 8px;
            border-bottom-right-radius: 8px;
        }

        .footer p {
            margin: 5px 0;
            font-size: 13px;
            color: #666666;
        }

        .social-links {
            margin: 20px 0;
        }

        .social-links a {
            margin: 0 10px;
            color: #1a237e;
            text-decoration: none;
        }

        .button {
            display: inline-block;
            padding: 12px 30px;
            background: linear-gradient(135deg, #1a237e 0%, #283593 100%);
            color: white;
            text-decoration: none;
            border-radius: 25px;
            font-weight: bold;
            margin: 20px 0;
            transition: transform 0.2s;
        }

        .button:hover {
            transform: translateY(-2px);
        }

        .info-box {
            background-color: #f8f9fa;
            border-left: 4px solid #1a237e;
            padding: 15px;
            margin: 20px 0;
        }

        .signature {
            font-style: italic;
            color: #666666;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #eeeeee;
        }

        ul, ol {
            padding-left: 20px;
        }

        li {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>${title}</h1>
    </div>

    <div class="content">
        ${content}
        <div class="info-box">
            如有任何问题,请随时联系我们的客户服务团队。
            <br>
            邮箱:321395678@qq.com
        </div>
    </div>

    <div class="divider"></div>

    <div class="footer">
        <p>此邮件为系统自动发送,请勿直接回复</p>
    </div>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值