Arabic is displayed as ????? in velocity template - spring

Greetings all
i am using velocity templates in sending emails
and in the template i have some arabic texts
and when sending the email, the text appears like ??????????
i don't know why:
encoding is set to utf-8 before sending the email here:
VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,templateName, "UTF-8",newModel);
i tried to add the charset in the vm, but with no luck:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
any ideas why such problem occurs ?

I was able to produce Arabic text (تجاوز سعة مكدس) on a plain-text email sent from a Spring app. The text displayed properly on GMail as well as Thunderbird. Here's my mail sending logic:
public void send(String fromAddress, String fromName,
String toAddress, String subject,
String template, Map<String, Object> model) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(
mimeMessage, "UTF-8");
message.setTo(toAddress);
message.setFrom(new InternetAddress(fromAddress, fromName));
message.setSubject(subject);
message.setText(VelocityEngineUtils
.mergeTemplateIntoString(velocityEngine, template, "UTF-8",
model));
}
};
mailSender.send(preparator);
}

Do you have any velocity.properties set? (particularly input.encoding or output.encoding) If not, try setting those both to UTF-8 also.

Related

Spring MessageSource.getMessage with arguments containing International characters showing up as ISO-8859 encoded characters

I am using Spring Message with placeholders to create an email message. The arguments for the placeholders contain International Characters. Once the email body text is set using the getMessage() method with message, arguments and Locale and email is sent, the email received has the placeholder values with ISO-8859 chars. How to ensure that the UTF-8 encoded Arguments are preserved while getting replaced in the message property.
Return property bundle:
#Bean
public MessageSource emailProps() {
ReloadableResourceBundleMessageSource messageSource = new
ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:props/Email");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
MailSender.java
#Autowired
private MessageSource emailProps;
public void send(String to, String language,String name, String org_name) throws MessagingException {
//org_name has values like "GmbH (Süd) München"
//some code here
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(message,
MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, "UTF-8");
//helper.setFrom, helper.setTo....
helper.setText(emailProps.getMessage("security.email.body", new Object[] {name,org_name},
Locale.GERMAN)+emailProps.getMessage("email.footer", null, Locale.GERMAN), true);
//remaining part of the code
}
In the Email org_name comes up as "GmbH (Süd) München".
Question: What should I do so that the UTF-8 encoded argument values are retained as-is? I have the argument values coming from DB and have verified that they do retain the encoding till the step before they are written into the message body.

Send image in thymeleaf

I want to send a message via email in which the alert and image will be sent using thymeleaf, but I do not want to send it in the implementation method of sending the message, since I cannot make changes to this image in any way, can I add an image to the html code? I try to add an image to the html code, the image does not open and I can't do anything with it, I want the image to appear and I could transfer text to it
public void send(String emailTo, String subject, String message,String content) throws MessagingException, IOException {
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);
// FileSystemResource file = new FileSystemResource(new File("src/main/resources/templates/image/valayev.jpg"));
// helper.addInline("valyaev", file);
mailSender.send(mimeMessage);

send mail through spring boot application

I am creating spring boot application where I have to send newly generated response string for each transaction to user as a text file attachment
so what will be the proper way to do this
any help would be appreciated
Try this below code:
#Override
public void sendMessageWithAttachment(
String to, String subject, String text, String pathToAttachment) {
// ...
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(text);
FileSystemResource file
= new FileSystemResource(new File(pathToAttachment));
helper.addAttachment("Invoice", file);
emailSender.send(message);
// ...
}

Spring + Thymeleaf mail template - why are inline images attached to email?

I'm using spring + thymeleaf to send emails. My mail template has few images which are displayed properly in the email, however i see that unused images are attached to the bottom of the email. I have business logic in the html, which decides what images to show and what not, so sometimes some images are not used but are attached. How do i get rid of them?
Any ideas?
Here is how i load the image in html:
<img src="cid:change-password" alt="">
Here is how i send the email:
public void sendUsingTemplate(String fromEmail, String toEmail, String subject, String templateName, Map<String, Object> variables) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject(subject);
final Context context = new Context(Locale.getDefault());
variables.forEach((name, value) -> context.setVariable(name, value));
String text = templateEngine.process(templateName, context);
helper.setText(text, true);
helper.setFrom(fromEmail);
helper.setTo(toEmail);
Map<String, String> inlineResources = mailTemplateResourcesProvider.getInlineResources(templateName);
inlineResources.forEach((contentId, file) -> addInlineWithoutCheckedException(helper, contentId, file));
mailSender.send(message);
} catch (Exception e) {
logger.error("Error sending mail using template", e);
}

JavaMail API - Send Emoji/Smiley with Freemarker Template?

I developed rest API in spring.
I have to send invitation mail to user with Emoji.
I developed .ftl and .java configuration file is as follows:
invitation.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
//Content
</body>
</html>
webAppConfi.java
#Bean
public FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean() {
FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
freeMarkerConfigurationFactoryBean
.setTemplateLoaderPath("/WEB-INF/templates/mail/");
freeMarkerConfigurationFactoryBean.setDefaultEncoding("UTF-8");
return freeMarkerConfigurationFactoryBean;
}
sendMail.java
MimeMessage message = null;
MimeMessageHelper objHelper = null;
String strBody = null;
try {
strBody = FreeMarkerTemplateUtils.processTemplateIntoString(
freemarkerMailConfiguration.getTemplate(templetName),
templateProp);
message = this.mailSender.createMimeMessage();
objHelper = new MimeMessageHelper(message, true);
objHelper.setTo(to);
objHelper.setText(strBody, true);
objHelper.setSubject(subject);
mailSender.send(message);
}
finally{}
I get the string like that from iPhone \ud83d\ude14\ud83d\ude17.
So how could i send this emoji value to email body part?
Thank you!

Resources