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

后端 未结 2 2087
谎友^
谎友^ 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

提交回复
热议问题