gpt4 book ai didi

Java : Java mail garbling the email-subject.

转载 作者:行者123 更新时间:2023-11-30 08:08:51 25 4
gpt4 key购买 nike

我正在以编程方式发送电子邮件,但电子邮件中的主题出现乱码(如下所示)。谁能告诉我我做错了什么。非常感谢。

发送邮件的代码:

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?=

我在邮件客户端和浏览器中都得到了上面的输出。我究竟做错了什么?

最佳答案

这似乎是使用的字符集的问题。要对此进行测试,您可以尝试在源代码中设置字符编码:

    System.setProperty("mail.mime.charset","Cp1252");

我认为 JavaMail API 会检查一些属性,但我不记得是哪些。

关于Java : Java mail garbling the email-subject.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32883615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com