Springboot 使用JavaEmail发送HTML邮件模板

网上看了很多教程,讲得各有所云,自己总结了一篇完整版

1、引入所需要maven

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

2、创建连接

    public static Session createSession(String username, String password, String AddressType, String port){
        //创建配置文件
        Properties props = new Properties();
        //设置发送时遵从SMTP协议
        props.setProperty("mail.transport.protocol", "SMTP");
        /*
         * 发送邮件的域名
         * smtp.xx.com
         * smtp.qq.com则代表发送邮件时使用的邮箱域名来自qq
         * smtp.163.com则代表发送邮件时使用的邮箱域名来自163
         */
//        props.setProperty("mail.host", "smtp.qq.com");
        //发送服务器地址
        props.setProperty("mail.host", AddressType);
        //设置用户的认证方式auth
        props.setProperty("mail.smtp.auth", "true");
//        props.put("mail.smtp.port", "587");
        //发送地址端口
        props.put("mail.smtp.port", port);
        // 创建会话
        return Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
    }

3、编写html模板

<!DOCTYPE html>
<html lang="en">
<body>
                                        <h1 style="color: #000; font-size: 26px; font-weight: bold;">自己有房子想出租,联系不上租客?</h1>
						    <h1 style="color: #000; font-size: 26px; font-weight: bold;">想租房子,不知道哪里有房源?</h1>
                                        <h1 style="color: #000; font-size: 26px; font-weight: bold;">来来来,我们帮你解决,我们有房源、有租客,还怕帮你解决不了问题?????</h1>
                                        <hr style="border: 1px solid #ddd; margin-top: 30px; margin-bottom: 30px;">
                                        <p style="font-size: 20px; line-height: 1.5; color: #666;">🔴微信搜小程序</p>
						    <p style="font-size: 16px; line-height: 1.5; color: #666;">👇👇👇</p>
                                        <p style="font-size: 16px; line-height: 1.5; color: #666;">✅ 免费发布出租房源</p>
                                        <p style="font-size: 16px; line-height: 1.5; color: #666;">✅ 免费发布转租房源</p>
                                        <p style="font-size: 16px; line-height: 1.5; color: #666;">✅ 无中介费求租、找室友</p>

                                        <p style="font-size: 16px; line-height: 1.5; color: #666;">🟢 希望可以帮到你,如有打扰请见谅哈~</p>
                                        <hr style="border: 1px solid #ddd; margin-top: 30px; margin-bottom: 30px;">
                                        <p style="font-size: 16px; line-height: 1.5; color: #666;">杭州租房群:<a href="https://docs.qq.com/doc/DYldIZUdBdkNzTnRm" target="_blank" style="color: #3388ff;">https://docs.qq.com/doc/DYldIZUdBdkNzTnRm</a></p>
                                        <p style="font-size: 16px; line-height: 1.5; color: #666;">提醒:租房群请按需添加,不能全部添加~</p>
                                        <p style="font-size: 16px; line-height: 1.5; color: #666;">淘宝天猫京东拼多多优惠券领取:<a href="https://www.shengqianboss.com/" target="_blank" style="color: #3388ff;">https://www.shengqianboss.com/</a></p>
                                        <hr style="border: 1px solid #ddd; margin-top: 30px; margin-bottom: 30px;">
						    <p style="font-size: 16px; line-height: 1.5; color: #666;">快来一探究竟吧~</p>

</body>
</html>

4、springboot读取html文件

    public static String readHTML() throws IOException {
        StringBuffer buf = new StringBuffer();
        Resource resource = new ClassPathResource("/template/EmailTemplate.html");
        InputStream is = resource.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        try {
            String data = null;
            while((data = br.readLine()) != null) {
                buf.append(data);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            br.close();
            isr.close();
            is.close();
        }
        return buf.toString();

    }

文件放置位置

5、编写发送邮件方法

    public static void sendMail(Session session,String username, String email, String subject,String emailMsg)
            throws MessagingException {
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));  //发件人
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); //收件人
            //设置邮件主题
            message.setSubject(subject,"utf-8");
            //设置邮件内容
            BodyPart text = new MimeBodyPart();
            text.setContent(emailMsg, "text/html;charset=utf-8");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(text);
            //设置邮件内容
            message.setContent(multipart);
            //发送邮件
            Transport.send(message);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

 6、测试方法

    public static void main(String[] args) throws MessagingException, IOException {
        final String username = "cdshccsd@163.com";
        final String password = "ddddsdsvsdv";
        String email = "116516852@qq.com";
        String subject = "【重要】有房出租怎么办?想找租房怎么办?进来看看~";
        String emailMsg = readHTML();
        String type = "smtp.163.com";
        String port = "25";
        Session session = createSession(username, password, type, port);
        sendMail(session, username, email, subject, emailMsg);

    }

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值