Email From with name

F

francan00

When I send mail in my Java it always outputs the from address with
the email address as it should where the email goes to the person and
the person can reply back to the sender email address:

From: (e-mail address removed)
To: (e-mail address removed)
Subject: Hello World



I would like to display the name and also have their email address
available so the person can reply back to it:

From: John Smith <[email protected]>
To: (e-mail address removed)
Subject: Hello World



I was wondering if I could use this: to="John Smith <[email protected]>"

My current email program:


String to = "(e-mail address removed)";
String from = "(e-mail address removed)";
String host = "smtp.mycompany.net";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
Session session = Session.getInstance(props);

try {
// Instantiatee a message
Message msg = new MimeMessage(session);
....



Please advise.
 
A

Arne Vajhøj

When I send mail in my Java it always outputs the from address with
the email address as it should where the email goes to the person and
the person can reply back to the sender email address:

From: (e-mail address removed)
To: (e-mail address removed)
Subject: Hello World

I would like to display the name and also have their email address
available so the person can reply back to it:

From: John Smith <[email protected]>
To: (e-mail address removed)
Subject: Hello World

A quick check in the docs for javax.mail.internet.InternetAddress
shows that it has multiple constructors.

Try the constructor with 2 arguments !

Arne
 
N

Nigel Wade

When I send mail in my Java it always outputs the from address with
the email address as it should where the email goes to the person and
the person can reply back to the sender email address:

From: (e-mail address removed)
To: (e-mail address removed)
Subject: Hello World



I would like to display the name and also have their email address
available so the person can reply back to it:

From: John Smith <[email protected]>
To: (e-mail address removed)
Subject: Hello World



I was wondering if I could use this: to="John Smith <[email protected]>"

My current email program:


String to = "(e-mail address removed)";
String from = "(e-mail address removed)";
String host = "smtp.mycompany.net";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
Session session = Session.getInstance(props);

try {
// Instantiatee a message
Message msg = new MimeMessage(session);
....



Please advise.

If the personal name part of an email has spaces then the name must be enclosed
in quotes (""). So the correct format of the email address is:
"John Smith" <[email protected]>
This entire address has to be placed in quotes to make it into a Java String so
the internal quotes have to be escaped:
"\"John Smith\" <[email protected]>"

Or, you can follow Arne's advice and use the two String constructor for
InternetAddress. Either method works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top