带html模板的spring boot java邮件

nwlqm0z1  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(394)

我使用的是一个html模板,它在javamail中用作内容。问题是字体系列不起作用。如果我在浏览器上运行html程序,它就会工作。但当作为电子邮件内容发送时,此字体不起作用。系统改用默认字体。
内容html:

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title></title>
    <link rel="icon" href="#"/>
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;600;700;800;900&display=swap" rel="stylesheet" />

    <style>
        body {
          font-family: 'Nunito', sans-serif;
          margin: 0;
        }
        .wrapper {
          width: 100%;
          max-width: 800px;
          margin: 0 auto;
          background-color: #ffffff;
          font-size: 14px;
        }
        .email-body {
          padding: 20px;
        }
    </style>
</head>
<body>
<div class="wrapper">
    <div class="email-body">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tempus at velit ac vehicula. 
           Sed ut porta augue, id cursus massa. Cras aliquam tempus lacus nec eleifend. Fusce enim odio,
           finibus eu ligula et, imperdiet placerat lorem. Fusce feugiat neque tincidunt, 
           placerat urna ullamcorper, laoreet tortor. Nulla congue ante eget odio egestas, ut eleifend justo volutpat. 
           Aliquam malesuada, quam nec pharetra ultrices, metus lacus elementum nisl, sed viverra eros nulla vitae nunc. Donec aliquet luctus mauris, sit amet dignissim risus lacinia id. Aliquam suscipit, turpis eu scelerisque tristique, mi turpis consequat sem, ac euismod elit turpis ac diam.</p>
    </div>
</div>
</body>
</html>

javamail.class类

public void sendEmail(String host, String port, String username, String password, List<String> to, String subject, String content) throws MessagingException {

        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username,password);
            }
        });
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            InternetAddress[] toAddress = new InternetAddress[to.size()];

            for( int i = 0; i < to.size(); i++ )
                toAddress[i] = new InternetAddress(to.get(i));

            for( int i = 0; i < toAddress.length; i++)
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);

            message.setSubject(subject);
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(content, "utf-8", "html"); //here the email template is inputted
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);

            Transport transport = session.getTransport("smtp");
            transport.connect(host, username, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

            logger.info("Send Email Success To : " + to.get(0));
        } catch (MessagingException e) {
            logger.info("Send Email Error Caused By : " + e.getMessage());
        }
    }

如何解决这个问题?

hsvhsicv

hsvhsicv1#

我这样改变了字体获取方法。而且成功了
此项已删除

<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;600;700;800;900&display=swap" rel="stylesheet" />

将此添加到 <style> 标签

@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;600;700;800;900&display=swap');

相关问题