Java 将电子邮件中的字符替换为?

Java replacing characters in email with?

这是我的电子邮件实现:

    private void send(Transport transport, Message message) throws MessagingException
    {
        // bunch of fluff
        transport.sendMessage(message, message.getAllRecipients());
    }

但是当我的消息正文包含 ş 之类的字符时,它们会被替换为 ?。我认为这与字符集问题有关,但我不确定该怎么做。

任何帮助都会很棒。

我必须做的是为该部分设置字符集。我做了类似的事情:

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

你可以这样做:

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

  send(transport, message);