以下是我的电子邮件设置代码:
String bodyMessage="Dear Renavçilçleç Françoisç InCites™";
String subject = "Your new InCites™ subscription";
Properties _sessionProperties = new Properties();
_sessionProperties.put("mail.transport.protocol", "smtp");
_sessionProperties.put("mail.smtp.host", "hostname");
_sessionProperties.put("mail.smtp.port", "25");
Session session = Session.getInstance(_sessionProperties, null);
MimeMessage mimemsg = new MimeMessage(session);
mimemsg.addRecipients(Message.RecipientType.TO, "xxx@gmail.com");
mimemsg.setSubject(subject, "UTF-8");
// Create a multi-part message
MimeMultipart multipart = new MimeMultipart();
// Set the subType
multipart.setSubType("alternative");
BodyPart part = new MimeBodyPart();
part.setContent(bodyMessage, "charset=UTF-8");
// Set the emailBody and emailType to MIME BodyPart
part.setDataHandler(new DataHandler(new ByteArrayDataSource(
bodyMessage, "text/html;")));
// Add the MIME BodyPart to MIME multiPart
multipart.addBodyPart(part);
// Put parts in message
mimemsg.setContent(multipart);
// Send message
Transport.send(mimemsg);
Run Code Online (Sandbox Code Playgroud)
但仍然在电子邮件主题中它仍然显示为 "Your new InCites™ subscription"
Rol*_*lig 14
你在这里提到的主题完全由ASCII字符组成.这包括有趣的特殊字符™.如果您希望它是Unicode,只需使用Unicode而不是HTML转义.邮件与HTML没有任何关系.
mimemsg.setSubject("Your new InCites\u2122 subscription", "UTF-8");
Run Code Online (Sandbox Code Playgroud)
这应该像=?UTF-8?Q?Your...subscription?=在RFC 2047中指定的那样对主题进行编码.
完整的示例代码:
package so4406538;
import java.io.IOException;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
public class MailDemo {
public static void main(String[] args) throws MessagingException, IOException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
message.setSubject("Your new InCites\u2122 subscription", "UTF-8");
message.setContent("hello", "text/plain");
message.writeTo(System.out);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Message-ID: <7888229.0.1291967222281.JavaMail.roland@bacc>
Subject: =?UTF-8?Q?Your_new_InCites=E2=84=A2_subscription?=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
hello
Run Code Online (Sandbox Code Playgroud)
您可以看到主题标题已编码,这是必要且正确的.
[ 更新:我修复了Unicode转义序列,如我的一条评论所示.]
| 归档时间: |
|
| 查看次数: |
26810 次 |
| 最近记录: |