java – 如何在电子邮件中设置“name”属性

我发送邮件与 Java

邮件和SMTP服务器.我希望能够更改收件人在收到电子邮件时看到的“名称” – 而不仅仅是电子邮件地址的前缀(@之前的位).

我怀疑我需要更改或添加’props.put();’设置,但我无法解决哪一个.

public class Email {

    private final String HOST = "mail.myserverr.com";
    private final String USER = "me+myserver.com";
    private final String FROM = "me@myserver.com";
    private final String PASS = "mypass";
    private final String PORT = "25";
    private final String AUTH = "true";

    @Test
    public void sendMail(){
        String[] to = {"mygmail@gmail.com","me@myservercom"};
        sendMessage(to,"Let's go","What's up");

    }

    public void sendMessage(String[] to, String subject, String msg) {

        Properties props = System.getProperties();
            props.put("mail.smtp.starttls.enable", "true"); // added this line
            props.put("mail.smtp.host", HOST);
            props.put("mail.smtp.user", USER);
            props.put("mail.smtp.password", PASS);
            props.put("mail.smtp.port", PORT);
            props.put("mail.smtp.auth", AUTH);
            props.put("mail.smtp.socketFactory.port", PORT);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");


        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(FROM));

        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i=0; i < to.length; i++ ) { // changed from a while loop
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i=0; i < toAddress.length; i++) { // changed from a while loop
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }

        message.setSubject(subject);
        message.setText(msg);

        Transport transport = session.getTransport("smtps");
        transport.connect(HOST, USER, PASS);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

典型的地址语法格式为“user@host.domain”或“Personal Name<user@host.domain/u0026gt;”.

您可以对FROM和TO字段地址使用相同的语法.

例:

更改以下声明:

String [] to = {“mygmail@gmail.com”,“me @ myservercom”};

String [] to = {“Recipient1 Name<mygmail@gmail.com/u0026gt;”,“My Name<me @ myservercom>”};

您还可以构造InternetAddress对象,将相应的电子邮件ID和个人名称作为参数传递.

例:

String FROM = "my.email.id@my.server.domain";  
InternetAddress from = new InternetAddress( FROM, "Ravinder" );

收件人将看到发件人姓名显示为“Ravinder”而不是“my.email.id@my.server.domain”

参考:
javax.mail.internet.InternetAddress

翻译自:https://stackoverflow.com/questions/10170687/how-do-i-set-the-name-attribute-in-an-email

原文 

https://codeday.me/bug/20190111/511211.html

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » java – 如何在电子邮件中设置“name”属性

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址