Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot MimeMessageHelper Spring启动以发送电子邮件_Spring Boot_Jakarta Mail - Fatal编程技术网

Spring boot MimeMessageHelper Spring启动以发送电子邮件

Spring boot MimeMessageHelper Spring启动以发送电子邮件,spring-boot,jakarta-mail,Spring Boot,Jakarta Mail,我正在使用spring boot发送电子邮件。我的电子邮件服务中的代码片段 private @Autowired JavaMailSender mailSender; 及 首先我设置htmlBody,然后设置textBody helper.setText(htmlBody, true); helper.setText(textBody, false); 它将htmlBody重写为textBody。如何使用org.springframework.mail.javamail.MimeMe

我正在使用spring boot发送电子邮件。我的电子邮件服务中的代码片段

private @Autowired JavaMailSender mailSender;

首先我设置htmlBody,然后设置textBody

    helper.setText(htmlBody, true);
helper.setText(textBody, false);

它将htmlBody重写为textBody。如何使用
org.springframework.mail.javamail.MimeMessageHelper发送文本和html正文任何更新?

您可以使用
thymeleaf
作为HTML模板引擎

示例HTML代码:

MySampleHTML.html


示例电子邮件
示例Java代码:

公共类电子邮件示例{
@自动连线
私有JavaMailSender;
@自动连线
私有TemplateEngine TemplateEngine;//来自Thymeleaf
public void initiateEmailSend(){
String processedHTMLTemplate=this.constructHTMLTemplate();
//开始准备电子邮件
mimessagepreparator preparator=消息->{
mimessagehelper=新的mimessagehelper(消息,mimessagehelper.MULTIPART_MODE_MIXED,“UTF-8”);
helper.setFrom(“样本”);
helper.setTo(“recipient@example.com");
helper.setSubject(“样本主体”);
setText(processedHTMLTemplate,true);
};
mailSender.send(准备者);//发送电子邮件
}
//填充HTML文件
私有字符串构造函数HtmlTemplate(){
上下文=新上下文();
setVariable(“sampleText”,“我的文本样本在此”);
返回templateEngine.process(“MySampleHTML”,上下文);
}
}
并在您的
pom.xml


org.springframework.boot
弹簧启动装置
注意:将
MySampleHTML.html
文件放在
resources/templates/
文件夹中,供
thymeleaf
查看。

而不是

helper.setText(htmlBody, true);
helper.setText(textBody, false);
使用

helper.setText(htmlBody, true);
helper.setText(textBody, false);
helper.setText(htmlBody, textBody);