適切な文字セットを使用してファイルを読み書きしていることを確認する必要があります。
私。プラットフォームのデフォルトの文字セットを使用するそうではありません:
Reader reader = new FileReader("/testfile.txt");
// ...
しかし、それ以上に、 InputStreamReader
あなたが明示的に適切な文字セットを指定するところ:
Reader reader = new InputStreamReader(new FileInputStream("/testfile.txt"), "UTF-8");
// ...
また、電子メール添付ファイルの Content-Type
ヘッダーでは、charset属性を設定する必要があり、UTF-8を使用して添付ファイルを書き込む必要があります。あなたが使用しているメールAPIが不明なので、詳細を提示することはできません。代わりに、 InputStream
/ OutputStream
を使用するだけでコンテンツを純粋なバイトとしてストリーミングすることができ、バイトが表す文字セットには影響しません。
Update: you're using Javamail's MimeBodyPart
without explicitly specifying the content type with the charset attribute. Now you're dependent on the mail client whether it treats the content as UTF-8 or not. Fix it as follows:
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(attachment);
attachmentPart.setHeader("Content-Type", "text/plain;charset=utf-8");
multipart.addBodyPart(attachmentPart);