如何使用 Java 中的消息 class 在电子邮件 header 中添加自定义字段

How to add custom field in email header using Message class in Java

我想发送一封包含以下字段的自定义 header 电子邮件,

发件人:

收件人:

转发:(我的自定义字段)

下面是我的代码

public Message createHeader(InternetAddress from, Address to, Address rt) throws MessagingException {
    Message m = new MimeMessage(emailSession);
    m.setFrom(from);
    m.setRecipient(Message.RecipientType.TO, to);
    // add my custom filed "RT:"+rt
    return m;
}

使用setHeader().

您可以阅读说明:

Set the value for this header_name. Replaces all existing header values with this new value. Note that RFC 822 headers must contain only US-ASCII characters, so a header that contains non US-ASCII characters must have been encoded by the caller as per the rules of RFC 2047.

您的代码将类似于:

public Message createHeader(InternetAddress from, Address to, Address rt) throws MessagingException {
    Message m = new MimeMessage(emailSession);
    m.setFrom(from);
    m.setRecipient(Message.RecipientType.TO, to);
    m.setHeader("RT", rt.toString());
    return m;
}