在 from.as 内的发送邮件中添加标题 like "Stack Overflow <do-not-reply@stackoverflow.email>"

Add title in send mail inside from.as like "Stack Overflow <do-not-reply@stackoverflow.email>"

我无法发送邮件以使用“来自”选项卡添加标题。 我需要在邮件中添加标题和文本,例如“Stack Overflow <do-not-reply@Whosebug.email>” 我将如何添加 Stack Overflow 邮件 ID 的标题字体。

我的代码在下面添加

            String to = "rabin.samanta@xxx.com";
        String from = "rabin.samanta@xxx.com";
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp-mail.outlook.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username", "password");
            }
        });
        String msgBody = "test............";
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from, "NoReply"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, "Mr. Recipient"));
        msg.setSubject("Welcome To Java Mail API");
        msg.setText(msgBody);
        msg.setHeader("header_name", "header_value");
        Transport.send(msg);
        System.out.println("Email sent successfully...");
    }

您可以简单地使用 String from = "Rabin Samanta <rabin.samanta@xxx.com>";

来自 InternetAddress class 文档

This class represents an Internet email address using the syntax of RFC822. Typical address syntax is of the form "user@host.domain" or "Personal Name <user@host.domain>".

https://docs.oracle.com/javaee/6/api/index.html?javax/mail/internet/InternetAddress.html


当你尝试这个时会发生什么

public static void main(String... args) throws Exception {
    Properties props = new Properties();
    props.put("mail.debug", "true");
    props.put("mail.smtp.host", "mail.smtpbucket.com");
    props.put("mail.smtp.port", "8025");
    props.put("mail.smtp.starttls.enable", "false");
    props.put("mail.smtp.auth", "false");

    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setJavaMailProperties(props);

    MimeMessage msg1 = javaMailSender.createMimeMessage();
    MimeMessageHelper msg = new MimeMessageHelper(msg1, true);
    String from= "Me <me@example.com>";
    msg.setFrom(new InternetAddress(from));
    msg.setTo("Me <me@example.com>");
    msg.setText("Hello");

    javaMailSender.send(msg1);
}

并检查 SMTP 存储桶 https://www.smtpbucket.com/emails?sender=me@example.com