Java邮件API - 编码的问题 [英] Java Mail API - Encoding problems
问题描述
我使用Java邮件API,我试图通过Gmail的SMTP发送电子邮件。
我如何工作的程序:
java.util.Scanner中的类用于获取用户输入 - 我要求用户在邮件发送类中使用的各种参数;其中执行以下操作:
I'm using Java Mail API and I'm trying to send an email through Gmail's SMTP. How my program works: java.util.Scanner class is used to get user input - I'm asking user for various parameters to be used in mail sending class; which does the following:
Message mailMessage = new MimeMessage(session);
mailMessage.setFrom(new InternetAddress("example@example.com"));
mailMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse(mail.getTo()));
mailMessage.setSubject(mail.getSubject());
mailMessage.setText(mail.getMessage());
Transport.send(mailMessage);
一切正常,只要我使用的ASCII符号/字符。但每当我想用特定国家的人物 - 如[õäöü] - 我得到一堆看起来怪怪的符号...
Everything works as long as I use ASCII symbols/ chars. But whenever I want to use "country-specific" characters - like [õäöü] - I get bunch of weird-looking symbols...
技术到目前为止,我已经使用(不工作对我来说):
Techniques I've used so far(which don't work for me):
setHeader("Content-Type", "text/plain; charset=UTF-8");
setHeader("Content-Encoding","ISO-8859-9");
setContent(message, "text/plain; charset=iso-8859-2");
请注意:是当执行的System.out.println()显示消息发送一个IDE中显示一切正常。
Note: everything is displayed correctly inside an IDE when System.out.println() is performed to display the message to be sent.
编辑:e.x.当发送消息体[õäöü]
它显示[A,?]在Gmail中。
e.x. when sent message body is [õäöü] It's displayed [ä„"?] in Gmail.
编辑:当 mailMessage.setText(MimeUtility.en codeTEXT(mail.getMessage(),UTF-8,Q));
是使用,那么在Gmail输出以下内容:
When mailMessage.setText(MimeUtility.encodeText(mail.getMessage(), "UTF-8", "Q"));
is used, then the output in Gmail is following:
"=?UTF-8?Q?=C3=A4=E2=80=9E=E2=80=9D=EF=BF=BD;=0D=0A?="
另一个编辑:有趣的是,当我这样做: mailMessage.setText(strVar +õäöü,ISO-8859-1);
它实际上追加õäöü很好地在我的电子邮件(但第一部分[strVar]字符串仍然充满了?'S和[]的)。
ANOTHER Interestingly, when I do: mailMessage.setText(strVar + "õäöü", "ISO-8859-1");
It actually appends "õäöü" nicely in my email (but the first part[strVar] of the string is still full of ?'s and []'s).
推荐答案
基本上,我的code工作得很好,因为它应该。这是CMD,也无法处理非ASCII字符。我用一个bat文件来访问一个罐子。我想,我只是要做一个小的GUI程序,然后......谢谢大家回答。
Basically, my code works just fine, as its supposed to. It was the cmd, that could not handle non-ascii letters. I used a bat file to access a jar. I think I'm just going to make a little GUI then... Thanks everyone for answering.
这篇关于Java邮件API - 编码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!