如何设置“名称”?电子邮件中的属性 [英] How do I set the "name" attribute in an email

查看:158
本文介绍了如何设置“名称”?电子邮件中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java邮件和SMTP服务器发送邮件。我希望能够更改收件人在收到电子邮件时看到的名称 - 而不仅仅是电子邮件地址的前缀(@之前的位)。

I am sending mail with Java mail and an SMTP server. I want to be able to change the "name" that the recipient sees when they get an email message - not simply the prefix of the email address (the bit before @).

我怀疑我需要更改或添加'props.put();'设置之一,但我无法确定哪一个。

I suspect I need to change or add one of the 'props.put();' settings but I can't work out which one.

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个人姓名< user@host.domain>

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

Typical address syntax is of the form "user@host.domain" or "Personal Name <user@host.domain>".
You can use the same syntax for both FROM and TO field addresses.

示例

更改以下声明:

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

to

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

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

示例

You can also construct InternetAddress objects passing respective e-mailID and personal names as arguments.
Example:

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

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

Recipient will see sender name for display as "Ravinder" instead of "my.email.id@my.server.domain"

参考: javax.mail.internet .InternetAddress

这篇关于如何设置“名称”?电子邮件中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆