Spring Boot:在模板解析期间发生错误

时间:2018-11-30 06:58:48

标签: html email spring-boot parsing thymeleaf

我正在尝试使用百里香叶在电子邮件中附加文件,但在发送文件时发生模板解析错误。

package com.report.tool.services; 
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
@Service("sendmail")
public class MailSendService {

 @Autowired
MailRepository  mailrepository;
@Autowired
UserRepo userRepo;
@Autowired
SpringTemplateEngine templateEngine;
JavaMailSender mailSender;

EmailModel datamodel;
HttpSession session;
//Mail send
 public Object sendMail(String filename) {
datamodel=new EmailModel();
try {
String usermail= "abc@abc.com"; 
//userRepo.findByToken("pPVHDe1ixjIEuNk").getEmail();


List<EmailMetaModel> mailmodel = mailrepository.findAll();
String email = null;
String pas = null;
for (Iterator iterator = mailmodel.iterator(); iterator.hasNext();) {
EmailMetaModel emailModel = (EmailMetaModel) iterator.next();
if (emailModel.getStatus() == true) {
email = emailModel.getEmail();
pas = emailModel.getEmailpassword();
}
}

String host = "smtp.gmail.com";
// Get system properties
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session s = Session.getInstance(props, null);

 MimeMessage message = new MimeMessage(s);

MimeMessageHelper helper = new MimeMessageHelper(message,
     MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
    StandardCharsets.UTF_8.name());

 helper.addAttachment("logo.png", new ClassPathResource("Zeronsec.png"));

Context context = new Context();
//context.setVariable( );
String html = templateEngine.process("email-template", context);

helper.setTo(usermail);
helper.setText(html, true);
helper.setSubject("Any");
helper.setFrom(email);

message.setSubject("Thymleaf mail");
message.setText(html);




Transport tr = s.getTransport("smtps");
tr.connect(host, email, pas);
tr.sendMessage(message, message.getAllRecipients());
System.out.println("Mail Sent Successfully");
tr.close();

 } catch (Exception sfe) {

 System.out.println(sfe);

}

return "Mail sent"; 
} 
}
  • 此文件位于我项目的资源文件夹中。
  • 我尝试了许多可能的情况,还访问了许多网站,但没有访问任何网站 唯一一个错误。
  • 请帮助。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

那只是HTML模板的问题,在春季,您需要正确格式化HTML,并确保所有标记均已关闭。