我创建了一个以希伯来语发送电子邮件的Java应用程序。
当我通过Eclipse运行它时,一切都很好,
,但
当我通过导出的Runnable JAR文件运行它时,一切都不再好了!
当我收到关于outlook的信息时,它是完美的:
מק"ט:19התקבלב:2014-09-16בשעה:13:00:24שםהפרוייקט:שםמזמין העבודה:
但是,当我在Gmail上收到按摩时,一切都被打乱了:
מק“×〜:19התק×'ל×':2014-09-16×'שעד:13:00:24××× ×“×¤×¨×•×™×™×§×〜:×××××××××××××××××××××××
我尝试使用浏览器编码设置,但没有运气,它现在设置为Unicode-UTF 8.我在eclipse中的项目设置为UTF-8。
答案 0 :(得分:0)
我终于开始工作了,
使用此代码:
// -- Create a new message --
final MimeMessage msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title, "utf-8");
msg.setText(message, "utf-8");
msg.setHeader("Content-Type", "text/html; charset=UTF-8");
msg.setSentDate(new Date());
MimeBodyPart mbp1 = new MimeBodyPart();
try {
mbp1.setDataHandler(new DataHandler(
new ByteArrayDataSource(message.toString(), "text/html")));
} catch (IOException e1) {
e1.printStackTrace();
}
mbp1.setHeader("Content-Type","text/plain; charset=\"utf-8\"");
mbp1.setContent( message, "text/html; charset=utf-8" );
mbp1.setHeader("Content-Transfer-Encoding", "quoted-printable");
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
try {
mbp2.attachFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp, "text/html");