使用 Velocity 模板在电子邮件发送中编码
Encoding in email send using Velocity template
我正在使用俄语语言的速度模板发送电子邮件,收到的邮件带有垃圾字符。
邮件模板。
<html>
<body>
<h3>Dear ${name},
<br/>Greetings from ${companyName}.</h3>
<span>Your Subscription will expire on : ${expiryDate}</span><br/>
<span>Нажмите <a href=${homePageUrl}>здесь</a>, чтобы войти.</span><br/>
<span>Click <a href=${homePageUrl}>here</a> to login.</span><br/>
<h3>Regards, <br/>
${companyName}</h3>
</body>
</html>
接收邮件:-
Dear Deepesh, Greetings from ..... Your Subscription will expire on : 23-04-2029 Ru ??????? ?????, ?????
?????. Click here to login. Regards, Budbeed Learning
俄语部分是垃圾。
现在我的电子邮件发送代码。
SendEmailResponse response = null;
try {
VelocityContext velocityContext = new VelocityContext();
final Map<String, String> contextMap = request.getContextMap();
for(Map.Entry<String,String> entry : contextMap.entrySet()) {
velocityContext.put(entry.getKey(), entry.getValue());
}
StringWriter stringWriter = new StringWriter();
velocityEngine.mergeTemplate("/templates/" + contextMap.get("templateName"),"UTF-8", velocityContext, stringWriter);
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(KP_DEV_MAIL_RU);
mimeMessageHelper.setTo(contextMap.get("email"));
mimeMessageHelper.setSubject(contextMap.get("subject"));
mimeMessageHelper.setText(stringWriter.toString(), true);
/*FileSystemResource file = new FileSystemResource(new File("banner.jpg"));
mimeMessageHelper.addInline("banner", file);
FileSystemResource fileSystemResource = new FileSystemResource(new File("Attachment.jpg"));
mimeMessageHelper.addAttachment("Attachment.jpg", fileSystemResource);*/
InputStreamSource source = new ByteArrayResource(request.getFile().toByteArray());
mimeMessageHelper.addAttachment("Invoice.pdf", source );
if(LMSCommonUtils.matchEmail(contextMap.get("email"))) {
javaMailSender.send(mimeMessage);
}
}catch (Exception e) {
LOGGER.error("Error while sending invoice",e);
res
}
我相信 javamailsender 也应该配置 UTF8
。像下面这样尝试
javaMailSender.setDefaultEncoding("UTF-8");
我正在使用俄语语言的速度模板发送电子邮件,收到的邮件带有垃圾字符。
邮件模板。
<html>
<body>
<h3>Dear ${name},
<br/>Greetings from ${companyName}.</h3>
<span>Your Subscription will expire on : ${expiryDate}</span><br/>
<span>Нажмите <a href=${homePageUrl}>здесь</a>, чтобы войти.</span><br/>
<span>Click <a href=${homePageUrl}>here</a> to login.</span><br/>
<h3>Regards, <br/>
${companyName}</h3>
</body>
</html>
接收邮件:-
Dear Deepesh, Greetings from ..... Your Subscription will expire on : 23-04-2029 Ru ??????? ?????, ????? ?????. Click here to login. Regards, Budbeed Learning
俄语部分是垃圾。
现在我的电子邮件发送代码。
SendEmailResponse response = null;
try {
VelocityContext velocityContext = new VelocityContext();
final Map<String, String> contextMap = request.getContextMap();
for(Map.Entry<String,String> entry : contextMap.entrySet()) {
velocityContext.put(entry.getKey(), entry.getValue());
}
StringWriter stringWriter = new StringWriter();
velocityEngine.mergeTemplate("/templates/" + contextMap.get("templateName"),"UTF-8", velocityContext, stringWriter);
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(KP_DEV_MAIL_RU);
mimeMessageHelper.setTo(contextMap.get("email"));
mimeMessageHelper.setSubject(contextMap.get("subject"));
mimeMessageHelper.setText(stringWriter.toString(), true);
/*FileSystemResource file = new FileSystemResource(new File("banner.jpg"));
mimeMessageHelper.addInline("banner", file);
FileSystemResource fileSystemResource = new FileSystemResource(new File("Attachment.jpg"));
mimeMessageHelper.addAttachment("Attachment.jpg", fileSystemResource);*/
InputStreamSource source = new ByteArrayResource(request.getFile().toByteArray());
mimeMessageHelper.addAttachment("Invoice.pdf", source );
if(LMSCommonUtils.matchEmail(contextMap.get("email"))) {
javaMailSender.send(mimeMessage);
}
}catch (Exception e) {
LOGGER.error("Error while sending invoice",e);
res
}
我相信 javamailsender 也应该配置 UTF8
。像下面这样尝试
javaMailSender.setDefaultEncoding("UTF-8");