发送电子邮件时将参数显示到 FreeMarker 模板中
Display arguments into a FreeMarker template when send email
通过使用 Spring 引导,我发送电子邮件成功,但我无法将参数传递到模板 ftl
MailService.java
@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("xxx@gmail.com",
"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
);
然后显示如下参数:
emailTemplate.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}
。
通过使用 Spring 引导,我发送电子邮件成功,但我无法将参数传递到模板 ftl
MailService.java
@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("xxx@gmail.com",
"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
);
然后显示如下参数:
emailTemplate.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}
。