本文介绍了Java邮件编码非英文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 使用下面的代码,我可以发送非英语的电子邮件,虽然主题显示正确,身体看起来像乱七八糟。 任何想法? 谢谢 //设置主机smtp地址属性props = new Properties(); props.put(mail.smtp.host,mail.infodim.gr); //创建一些属性并获取默认会话会话session = Session.getDefaultInstance(props,null); //创建消息消息msg = new MimeMessage(session); //设置地址 InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress addressTo = new InternetAddress(收件人); msg.setRecipient(Message.RecipientType.TO,addressTo); //设置主题和内容类型 msg.setSubject(subject); msg.setContent(message,text / plain); Transport.send(msg); } 解决方案 msg.setContent(message,text / plain; charset = UTF-8); 修改已更改为 text / plain 。 Using the code below i can send an email written in non-english and although the subject appears correctly the body appears as gibberish.Any ideas?Thank youpublic 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); } 解决方案 Try:msg.setContent(message, "text/plain; charset=UTF-8");Edit Changed to text/plain. 这篇关于Java邮件编码非英文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 17:20
查看更多