Java邮件编码非英语字符
使用下面的代码,我可以发送一封用非英语书写的电子邮件,尽管主题显示正确,但正文显示为胡言乱语。Java邮件编码非英语字符,java,email,encoding,internationalization,Java,Email,Encoding,Internationalization,使用下面的代码,我可以发送一封用非英语书写的电子邮件,尽管主题显示正确,但正文显示为胡言乱语。 有什么想法吗? 多谢各位 public void postMail(String recipient, String subject, String message, String from) throws MessagingException, UnsupportedEncodingException { //Set the host smtp address
有什么想法吗?
多谢各位
public void postMail(String recipient, String subject, String message, String from) throws MessagingException, UnsupportedEncodingException {
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "mail.infodim.gr");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress addressTo=new InternetAddress(recipient);
msg.setRecipient(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
尝试:
Edit更改为text/plain
而不是
msg.setContent(message, "text/plain");
我会写
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(message, "text/plain; charset=ISO-8859-7");
mp.addBodyPart(mbp);
msg.setContent(mp);
我从你的名字中猜到了
ISO-8859-7
,因为这个字符集是希腊语的,但也许你可以更恰当地选择它。或者,UTF-8也可能适用于您的情况。如果没有其他帮助,请尝试将源文件(包括.java文件)的编码更改为UTF8。
在Eclipse中,它是通过窗口->首选项->常规->工作区:文本文件编码完成的
我的文本文件默认使用CP1252
我正在从.properties文件获取文本。将它们更改为UTF8没有帮助。
这太疯狂了,但将我的.java文件切换到UTF8解决了我的问题 应该是“text/plain;charset=\“UTF-8\”,这是一个非常好的猜测,可能接近正确的解决方案。我们只能猜测您的电子邮件使用的字符集。如果您也不知道,也许您可以在问题中添加几行十六进制转储示例。@wds:Edited to say
text/plain
。字符集标识符周围的引号是完全可选的,所以我没有更改它;message.setContent(正文,“text/plain;charset=utf-8”);,但是我的gmail收件箱看起来还是乱七八糟的。为什么你需要一个多部分来包装一个身体部位?这太傻了。可能是因为我从一个同时发送附件的应用程序中获取了代码片段?。。我是Java邮件的新手。那么你是如何将主题设置为utf-8编码的呢?
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(message, "text/plain; charset=ISO-8859-7");
mp.addBodyPart(mbp);
msg.setContent(mp);