Java邮件编码非英文字符 [英] Java mail encoding non english characters

查看:148
本文介绍了Java邮件编码非英文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我可以发送非英语的电子邮件,虽然主题显示正确,身体看起来像乱七八糟。

任何想法?

谢谢< public void postMail(String recipient,String subject,String message,String from)throws MessagingException,UnsupportedEncodingException {

//设置主机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 you

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);

        }

解决方案

Try:

msg.setContent(message, "text/plain; charset=UTF-8");

Edit Changed to text/plain.

这篇关于Java邮件编码非英文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆