13

我正在使用 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();
        }
    }
}
4

2 回答 2

32

典型的地址语法格式为"user@host.domain"or "Personal Name <user@host.domain>"
您可以对 FROM 和 TO 字段地址使用相同的语法。

示例:将
以下语句更改
String[] to = {"mygmail@gmail.com","me@myservercom"};
为:
String[] to = {"Recipient1 Name <mygmail@gmail.com>","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

于 2012-04-16T09:26:54.483 回答
12

你需要改变:

message.setFrom(new InternetAddress(FROM));

message.setFrom(new InternetAddress(FROM, "Company XYZ"));

文档:类 InternetAddress

互联网地址

public InternetAddress(String address,
                   String personal)
                   throws UnsupportedEncodingException 

在给定地址和个人姓名的情况下构造一个 InternetAddress。该地址被假定为语法上有效的 RFC822 地址。

参数:

address - RFC822 格式的地址

个人 - 个人姓名

抛出: UnsupportedEncodingException

于 2012-04-16T08:22:34.037 回答