How do I set the “name” attribute in an email

后端 未结 2 2089
谎友^
谎友^ 2021-02-20 11:14

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 t

相关标签:
2条回答
  • 2021-02-20 11:42

    You need to change:

    message.setFrom(new InternetAddress(FROM));
    

    to

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

    Documentation: Class InternetAddress

    InternetAddress

    public InternetAddress(String address,
                       String personal)
                       throws UnsupportedEncodingException 
    

    Construct an InternetAddress given the address and personal name. The address is assumed to be a syntactically valid RFC822 address.

    Parameters:

    address - the address in RFC822 format

    personal - the personal name

    Throws: UnsupportedEncodingException

    0 讨论(0)
  • 2021-02-20 11:44

    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.

    Example:
    Change following statement:
    String[] to = {"mygmail@gmail.com","me@myservercom"};
    to
    String[] to = {"Recipient1 Name <mygmail@gmail.com>","My Name <me@myservercom>"};

    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" );  
    

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

    Reference: javax.mail.internet.InternetAddress

    0 讨论(0)
提交回复
热议问题