为什么我不能通过spring发送CSS装饰的html电子邮件?

问题描述 投票:0回答:1

我已经成功发送了文本电子邮件,并且下面的实现发送了非常基本的html电子邮件-我都没有CSS类。

我的MailServiceImpl类的相关部分是:

protected void prepareAndSendMail(Map<String, String> emailMap) {

    try {
        InternetAddress[] parsed;
        try {
            parsed = InternetAddress.parse(emailMap.get("to"));
        } catch (AddressException e) {
            throw new IllegalArgumentException("Not valid email: " + emailMap.get("to)"), e);
        }

        MimeMessage mailMessage = javaMailSender.createMimeMessage();
        mailMessage.setSubject(emailMap.get("subject"), "UTF-8");

        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "UTF-8");
        helper.setFrom(emailMap.get("from"));
        helper.setTo(parsed);
        helper.setText(mailContentBuilderService.buildHTMLTemplate(emailMap.get("text")), true);

        javaMailSender.send(mailMessage);
    } catch (MessagingException ex) {
        throw new RuntimeException(ex);
    }
}

protected String buildConfirmRegistrationButton(String label, String confirmationUrl) {
    StringBuilder content = new StringBuilder();
    content.append("<div class=\"jumbotron\">");
    content.append("<div class=\"container-fluid\">");
    content.append("<div class=\"form-group row justify-content-md-center justify-content-lg-center justify-content-xl-center\">");
    content.append("<label>" + label + "</label> \r\n");
    content.append("<br />");
    content.append("<a href=\"" + confirmationUrl + "\">");

    content.append("<button class=\"btn btn-primary\"> Click here to confirm </button>");

    content.append("</a>");

    content.append("</div>");
    content.append("</div>");
    content.append("</div>");

    return content.toString();
}

@Override
public void sendNewUserRegistrationToken_HTML(OnRegistrationCompleteEvent onRegistrationCompleteEvent, User user, String token) {
    Locale locale = Locale.US;

    final String subject = messageSource.getMessage("mail.registrationConfirmationSubject", null, locale);
    final String label = messageSource.getMessage("mail.registrationConfirmationLabel", new Object[]{user.getEmail()}, locale);
    final String confirmationUrl = onRegistrationCompleteEvent.getAppUrl() + "/admin/user/registration/confirm?token=" + token;

    final String content = buildConfirmRegistrationButton(label, confirmationUrl);

    Map<String, String> emailMap = new HashMap<>();
    emailMap.put("to", user.getEmail());
    emailMap.put("from", environment.getProperty("support.email"));
    emailMap.put("subject", subject);
    emailMap.put("text", content);

    prepareAndSendMail(emailMap);
}

prepareAndSendMail方法调用mailContentBuilderService.buildHTMLTemplate(String text),并定义为:

private TemplateEngine templateEngine;

@Autowired
public MailContentBuilderServiceImpl(TemplateEngine templateEngine) {
    this.templateEngine = templateEngine;
}

@Override
public String buildHTMLTemplate(String message) {
    StringBuilder html = new StringBuilder();
    html.append("<!DOCTYPE html>");
    html.append("<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:th=\"http://www.thymeleaf.org\"> ");
    html.append("<head>");
    html.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">");
    html.append("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\">");
    html.append("<script src=\"https://code.jquery.com/jquery-3.3.1.slim.min.js\" integrity=\"sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo\" crossorigin=\"anonymous\"></script>");
    html.append("<script src=\"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js\" integrity=\"sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1\" crossorigin=\"anonymous\"></script>");
    html.append("<script src=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js\" integrity=\"sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM\" crossorigin=\"anonymous\"></script>");
    html.append("</head>");

    html.append("<body>");
    html.append("<div class=\"container\">");
    html.append("<div class=\"text-center\">");
    html.append(message);
    html.append("</div>");
    html.append("</div>");
    html.append("</body>");
    html.append("</html>");

    return html.toString();
}  

尝试注册新用户时得到的结果是:

enter image description here

如何使它看起来不错,服从应该从CDN继承的CSS?

我尝试使用如下所述的build方法(显然,指向正确的模板),仍然没有乐趣。

public String build(String message) {
    Context context = new Context();
    context.setVariable("message", message);
    return templateEngine.process("mailTemplate", context);
}
spring-boot html-email
1个回答
0
投票

您需要了解电子邮件不符合正常的网络标准。您无法将脚本添加到电子邮件。您无法添加外部样式表(大多数样式表将被剥离;另请参见以下答案:Can you link to a CSS file from an email?)。

您必须将CSS嵌入到<head>中,即:

html.append('<style type="text/css">.container {...}</style>');
© www.soinside.com 2019 - 2024. All rights reserved.