使用下面的代码,我可以发送一封非英语的电子邮件,尽管主题显示正确,但正文显示为乱码。
有什么想法吗?
谢谢
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);
}
最佳答案
尝试:
msg.setContent(message, "text/plain; charset=UTF-8");
编辑更改为text/plain
。
关于Java邮件编码非英文字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2543589/