如何更改要在 Java 中回复的电子邮件地址

How to change email address to reply to in Java

我有发送邮件的方法:

public static void sendMail(InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, String subject, String body, String priority, String type) throws MessagingException {
    String host = "127.0.0.1";

    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);

    Session session = Session.getInstance(properties);

    MimeMessage msg = new MimeMessage(session);
    msg.setSubject(subject);
    msg.addHeader("X-Priority", priority);
    msg.setFrom("noreply@mydomain.com");
    msg.addRecipients(Message.RecipientType.TO, to);

    if (cc != null) {
        msg.addRecipients(Message.RecipientType.CC, cc);
    }

    if (bcc != null) {
        msg.addRecipients(Message.RecipientType.BCC, bcc);
    }

    if (type == null) {
        msg.setText(body);
    } else {
        msg.setText(body, "utf-8", type);
    }

    Transport.send(msg);
}

我希望,如果某些用户回复此类电子邮件,他的电子邮件将被重定向到其他电子邮件(例如 support@mydomain.com)。

尝试 msg.setReplyTo(replyTo); 请注意 replyTo 与 From Address

不同

通过设置 属性 mail.smtp.from, or by using the SMTPMessage.setEnvelopeFrom 方法设置 "envelope from" 地址。