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

☆樱花仙子☆ 提交于 2019-12-05 01:52:55

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!