'Java replacing characters in email with? [closed]
Here is my email implementation:
private void send(Transport transport, Message message) throws MessagingException
{
// bunch of fluff
transport.sendMessage(message, message.getAllRecipients());
}
But when my message body contains chars like ş and “ are replaced with a ?. I assume this has something to do with a char-set issue, but im not sure what to do about it.
Any help would be great.
Solution 1:[1]
What I've had to do is to set the charset for the part. I do something like:
Session session = Session.getDefaultInstance(new Properties());
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(fromEmail);
mimeMessage.setSubject(subject, StandardCharsets.UTF_8.name());
MimeBodyPart plainTextMimeBodyPart = new MimeBodyPart();
plainTextMimeBodyPart.setText(plainTextMessage, StandardCharsets.UTF_8.name());
Multipart messageMultiPart = new MimeMultipart("alternative");
messageMultiPart.addBodyPart(plainTextMimeBodyPart);
MimeBodyPart htmlBodyPart = new MimeBodyPart();
Multipart relatedMultiPart = new MimeMultipart("related");
htmlBodyPart.setContent(htmlMessage, "text/html; charset=utf-8");
relatedMultiPart.addBodyPart(htmlBodyPart);
Solution 2:[2]
You could do something like this:
MimeMessage message = new MimeMessage();
message.setText("? and “", "UTF-8");
// set recipients, etc
send(transport, message);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stdunbar |
| Solution 2 | Delta George |
