Java邮件编码非英文字符

Arg*_*ros 8 java email encoding internationalization

使用下面的代码,我可以发送一个非英语的电子邮件,虽然主题正确显示,但正文显示为乱码.
有任何想法吗?
谢谢

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

        }
Run Code Online (Sandbox Code Playgroud)

Rob*_*ser 18

尝试:

msg.setContent(message, "text/plain; charset=UTF-8");
Run Code Online (Sandbox Code Playgroud)

编辑已更改为text/plain.

  • 应该是"text/plain; charset = \"UTF-8 \"" (2认同)

blu*_*ish 7

代替

msg.setContent(message, "text/plain");
Run Code Online (Sandbox Code Playgroud)

我会写的

Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(message, "text/plain; charset=ISO-8859-7");
mp.addBodyPart(mbp);

msg.setContent(mp);
Run Code Online (Sandbox Code Playgroud)

ISO-8859-7从你的名字中猜到了,因为这个字符集是为了希腊语,但也许你可以更恰当地选择它.或者也可能UTF-8适用于您的情况.