4

Java Mail API に問題があります。

メールを正常に送信できますが、一部の特殊文字 (チェコ語、スロバキア語などの ISO-8859-2 言語から) がメールに表示されません。IDE出力でも壊れています。

私は何を間違っていますか?

Message msg = new MimeMessage(session);
msg.setContent(message, "text/plain; charset=iso-8859-2")
4

4 に答える 4

2

msg.setContent(message、 "text / plain; charset = UTF-8");

あなたが与えた文字セットの代わりに?

于 2010-05-10T10:13:25.930 に答える
2

マルチパートを使用して解決策を見つけました。ここにコードがあります:

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
MimeMultipart multipart = new MimeMultipart();
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
MimeBodyPart tmpBp = new MimeBodyPart();
tmpBp.setContent(message,"text/plain; charset=utf-8");
multipart.addBodyPart(tmpBp);
msg.setContent(multipart);
Transport.send(msg);
于 2010-01-17T18:41:39.573 に答える
0

むしろUTF-8、文字セットとして使用し、まったく同じ文字セットを使用するように IDE コンソールを構成してください。教えてくれなかったのでどのIDEを使っているかわかりませんが、EclipseならWindow > Preferences > General > Workspace > Text file encoding > Other > UTF-8で変更できます。

それでも問題が解決しない場合、問題は別の場所にあります。間違ったエンコーディングを使用してファイルからメッセージを読み取っている可能性があります。そのためInputStreamReaderには、charset を 2 番目のコンストラクター引数として使用する必要があります。

于 2010-01-17T18:20:51.680 に答える
0

代わりにsetTextクラスのメソッドを使用する必要がありますMimeMessagesetContent

/**
     * Convenience method that sets the given String as this part's
     * content, with a MIME type of "text/plain" and the specified
     * charset. The given Unicode string will be charset-encoded
     * using the specified charset. The charset is also used to set
     * the "charset" parameter.
     *
     * @param   text    the text content to set
     * @param   charset the charset to use for the text
     * @exception   MessagingException  if an error occurs
     */
    public void setText(String text, String charset)
            throws MessagingException {
于 2010-01-18T14:18:52.083 に答える