需要的Jar包

<!--邮件发送-->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

工具类

package cn.lineon.reggie.utils;

import lombok.extern.slf4j.Slf4j;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.Properties;

@Slf4j
public  class EmailUtils {
    private static final String USEREMAIL = "system@lineon.cn"; // 发件人称号,同邮箱地址
    private static final String PASSWORD = ""; // 可以使户端授权码

    /**
     * 发送邮件
     * @param to
     * @param text
     * @param title
     * @return
     */
    public static boolean sendMail(String to, String text, String title){
        try {
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.exmail.qq.com");//发送服务器,根据实际填写
            props.put("mail.user", USEREMAIL);
            props.put("mail.password", PASSWORD);

            // 构建授权信息,用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息,创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 小小的测试下
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //加载邮件html模板
        String fileName = "emailTemplate/validate.html";
        InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
        BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder emailTemplate = new StringBuilder();
        String line = "";
        try {
            while ((line = fileReader.readLine()) != null) {
                emailTemplate.append(line);
            }
        } catch (Exception e) {
            log.error("读取文件失败,fileName:{}", fileName, e);
        } finally {
            inputStream.close();
            fileReader.close();
        }
        //格式换填充数据
        String formatTemplate = MessageFormat.format(emailTemplate.toString(), 1010);
        EmailUtils.sendMail("2801941655@qq.com",formatTemplate,"蓝溪-验证码");
    }
}

邮件模板

<div style="background:#ecf1f3;padding-top:20px; min-width:820px;">
    <div style="width:801px;height:auto; margin:0px auto;">
        <div style="width:778px;height:auto;margin:0px 11px;background:#fff;box-shadow: 6px 3px 5px rgba(0,0,0,0.05);-webkit-box-shadow: 6px 3px 5px rgba(0,0,0,0.05);-moz-box-shadow: 6px 3px 5px rgba(0,0,0,0.05);-ms-box-shadow: 6px 3px 5px rgba(0,0,0,0.05);-o-box-shadow: 6px 3px 5px rgba(0,0,0,0.05);">
            <div style="width:781px;height:160px; background:#fff;">
                <div style="width:200px;height:160px;background:url(https://blog.lineon.cn/wp-content/uploads/2022/06/1654689591-96d6f2e7e1f705a.png) 0px 60px no-repeat; margin:0px auto;background-size: contain;"></div>
            </div>
            <div style="width:627px;margin:0 auto; padding-left:77px; background:#fff;font-size:14px;color:#55798d;padding-right:77px;">
                <br>
                <div style="overflow-wrap:break-word;line-height:30px;">
                    验证码:{0}
                </div>
                <br><br><br>
            </div>
        </div>
        <div style="position:relative;top:-15px;width:800px;height: 360px;background:url(https://blog.lineon.cn/wp-content/themes/ripro-v2/assets/img/mail-bg.jpg) 0px 0px no-repeat;">
            <div style="height:200px;color:#507383;font-size:14px;line-height: 1.4;padding: 20px 92px;">
                <div style="font-size: 22px;font-weight: bold;"><a href="https://blog.lineon.cn">蓝上小栈</a></div>
                <div style="margin:20px 0;color: #6a8895;min-height:4.2em;white-space: pre-wrap;">一个吐槽、偶尔提供一些源码分享、发布教程文章。致力于为广大小白站长、编程爱好者提供学习交流的资源,以减轻学习的难度。</div>
                <div style="">此邮件为系统通知邮件,切勿直接回复</div>
            </div>
            <div style="clear:both;"></div>
        </div>
    </div>
</div>

发表回复

后才能评论

本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。

最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或联络我们。

对于会员专享、整站源码、程序插件、网站模板、网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。

如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理

源码素材属于虚拟商品,具有可复制性,可传播性,一旦授予,不接受任何形式的退款、换货要求。请您在购买获取之前确认好 是您所需要的资源