【问题标题】:Spring boot java mail with template html带有模板 html 的 Spring Boot java 邮件
【发布时间】:2021-03-16 02:44:09
【问题描述】:

我正在使用一个 html 模板,该模板用作我的 JavaMail 中的内容。问题是 font-family 不起作用。如果我在浏览器上运行 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());
        }
    }

如何解决这个问题?

【问题讨论】:

  • 您尚未检查 HTML 消息 按交付方式 是否正确。如果是这样(我怀疑是这样),这完全是关于 HTML 引擎而不是关于 Spring。

标签: java spring-boot html-email


【解决方案1】:

我像这样更改了字体获取方法。它起作用了

已删除

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

&lt;style&gt;标签内添加这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多