通过使用 Spring Boot,我成功发送了电子邮件,但我无法将参数传递到模板中 ftl
邮件服务程序
@Service
public class MailService {
@Autowired
private EmailService emailService;
public void sendEmailWithTemplating(String userEmail, String username) throws UnsupportedEncodingException, CannotSendEmailException {
InlinePicture inlinePicture = createGalaxyInlinePicture();
final Email email = DefaultEmail.builder()
.from(new InternetAddress("[email protected]",
"Smart Reservation System"))
.to(newArrayList(
new InternetAddress(userEmail,
username)))
.subject("registration of a new user")
.body("")
.encoding("UTF-8").build();
String template = "emailTemplate.ftl";
Map<String, Object> modelObject = ImmutableMap.of(
"title", "Emperor",
"name", "Cleon I"
);
emailService.send(email, template, modelObject, inlinePicture);
}
private InlinePicture createGalaxyInlinePicture() {
ClassLoader classLoader = getClass().getClassLoader();
File pictureFile = new File(
classLoader.getResource(
"images/parcking.jpg")
.getFile());
Preconditions.checkState(pictureFile.exists(),
"There is not picture %s", pictureFile.getName());
return DefaultInlinePicture.builder()
.file(pictureFile)
.imageType(ImageType.JPG)
.templateName("parcking.jpg").build();
}
}
例如,我想在sendEmailWithTemplating()
我的模板中显示作为方法参数的用户名参数emailTemplate.ftl
我发现我们可以使用像这样的地图
Map<String, Object> modelObject = ImmutableMap.of(
"username", username,
"useremail", userEmail
);
然后显示如下参数:
电子邮件模板.ftl
<!doctype html>
<html>
<body>
<p>
Dear<em>{{username}}</em>,//but it display it like {{username}} it coundn't display the value of username
</p>
<p>
<strong>Congratulations!</strong> You’re Now Signed Up.
</p>
<p style="align: center">
<img src="parcking.jpg" />
</p>
<p>
--<br /> Regards<br />
<em>SRS Team</em><br />
</p>
</body>
</html>
FreeMarker 的语法是${username}
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句