Search code examples
htmlspringthymeleaf

How to get data from html with thymeleaf?


I use thymeleaf to send a message by mail, I use Contex () to set variables that have user fields. What is the problem? The problem is that when you send data from a template from the mail, the entire html code is sent, but you just need a message. I think looking at the code will be clearer

 Context context = new Context();
    context.setVariable("name", user.getUsername());
    context.setVariable("activationCode", user.getActivationCode());
    MvcConfig mvcConfig = new MvcConfig();
    TemplateEngine templateEngine = mvcConfig.templateEngine();
    String text = templateEngine.process("email-template",context);
    if (!org.springframework.util.StringUtils.isEmpty(user.getEmail())) {
        //TODO: move to template

        mailSenderService.send(user.getEmail(), "Activation code", text);
    }




@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(new UrlTemplateResolver());
    templateEngine.setTemplateResolver(templateResolver());
    return templateEngine;
}

enter image description here

in this form comes in the mail, but it should be something like this Hello Dear, laza Welcome.Please visit http://aa.ru/activate-account/blalblalbla


Solution

  • public void send(String emailTo, String subject, String message,String content) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        mimeMessage.setSubject(subject);
        mimeMessage.setContent(content, "HTML5");
        MimeMessageHelper helper;
        helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(username);
        helper.setTo(emailTo);
        helper.setText(message,true);
        mailSender.send(mimeMessage);}
    

    i add Content in method for sending mail, it solve problem