Home > front end >  Java replacing characters in email with?
Java replacing characters in email with?

Time:04-29

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.

CodePudding user response:

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);

CodePudding user response:

You could do something like this:

  MimeMessage message = new MimeMessage();
  message.setText("ş and “", "UTF-8");
  // set recipients, etc

  send(transport, message);
  •  Tags:  
  • java
  • Related