My registration is successful. a notification is sent to the mail with the text about the request to follow the link, after which the account is activated. Everything is fine, but it is impossible to translate this text into a template so that the variables should be stored in it, which should be sent to the post office, but I don’t understand how to place thymeleaf from the java code
user.getUsername() user.getActivationCode()
in my case they are returned in the same form, they do not give out the value of the user to which they correspond
public void addUser(User user) throws Exception {
User userFromDb = repository.findByUsername(user.getUsername());
if (userFromDb != null) {
throw new Exception("User exist");
}
user.setRoles(new String[]{"USER"});
user.setActive(false);
user.setActivationCode(UUID.randomUUID().toString());
repository.save(user);
if (!org.springframework.util.StringUtils.isEmpty(user.getEmail())) {
//TODO: move to template
String message = String.format(
"Hello," + user.getUsername() + "!\n" + "Welcome. Please, visit http://aa/activate-account/%s",
user.getActivationCode());
mailSenderService.send(user.getEmail(), "Activation code", message);
}
}
Hello Dear ${user.getUsername}
Welcome. Please, visit http://aa.ru/activate-account/%s
%{user.getActivationCode()}
Thanks
my thymeleaf
I am trying to read this file through Context and submit as message in the send method
public void send(String emailTo, String subject, String message){
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(username);
mailMessage.setTo(emailTo);
mailMessage.setSubject(subject);
mailMessage.setText(message);
mailSender.send(mailMessage);
}
i get
Hello Dear ${user.getName()}
Welcome. Please, visit http://aa.ru/activate-account/%s %{user.getActivationCode()} Thanks
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<p>Hello Dear,laza</p><p>Welcome.Please visit, http://aa.ru/activate-account/199572cc-17fe-4412-be7b-e54568b0cdb1</p><p> Thanks</p>
</body>
</html>
this is what comes to me in the mail with tags
I get something, but I get all the html code because I use Contex, how can I get around this?
What you are trying to is explained in here Please follow this link and apply the idea to your situation. Sample HTML template is as follows:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:remove="all">Template for HTML email</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="${'Hello Dear,' + user.getUsername}">
Hello Dear John Doe
</p>
<p th:text="${'Welcome.Please visit, http://aa.ru/activate-account/' + user.getActivationCode}">
Welcome. Please visit http://aa.ru/activate-account/blahblah
</p>
<p>
Thanks
</p>
</body>
</html>