【发布时间】:2015-10-01 17:22:09
【问题描述】:
我正在以编程方式发送一封电子邮件,但电子邮件中的主题变得乱码(发布在下面)。谁能告诉我我做错了什么。非常感谢。
发送电子邮件的代码:
final String from = "from@gmail.com";
final String emailPassword = "password";
final String to = "somemail@gmail.com";
final String ccMail = "ccmail@gmail.com";
String[] mailAddressTo = new String[2];
mailAddressTo[0] = to;
mailAddressTo[1] = ccMail;
InternetAddress[] mailAddress_TO = new InternetAddress[mailAddressTo.length];
for (int i = 0; i < mailAddressTo.length; i++)
{
try {
mailAddress_TO[i] = new InternetAddress(mailAddressTo[i]);
} catch (AddressException ignored) { }
}
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
from, emailPassword);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.addRecipients(Message.RecipientType.TO, mailAddress_TO);
message.setSubject("Es hat sich jemand für einen Kurs eingeschrieben");
String messageText = "some text";
message.setContent(messageText,"text/html;charset=UTF-8");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
主题输出:
=?ANSI_X3.4-1968?Q?Es_hat_sich_jemand_f=3Fr_einen_Kurs_eingeschrieben?=
上面的输出我在邮件客户端和浏览器中也得到了。我究竟做错了什么?
【问题讨论】:
-
@Rob : 你能告诉我该链接中的哪一部分或者我可以改变什么吗?
-
看来这个问题也有同样的问题,但也有答案stackoverflow.com/questions/3451256/…
-
@Rob :下面的答案解决了它。