gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:26:00 25 4
gpt4 key购买 nike

我已经成功地发送了文本电子邮件,下面的实现发送了非常基本的 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);
}

最佳答案

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

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

html.append('<style type="text/css">.container {...}</style>');

关于spring-boot - 为什么我不能通过 spring 发送 CSS 装饰的 html 电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56498830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com