Question

We use a GlassFish server (JavaEE 7) with JavaMail. Afaik, the official E-Mail RFC states that mail addresses may look something like this:

Tom Tester <tom.tester@test.com>

which would include a nicer representation than using only the email address. The Glassfish server is able to use this when configuring it on the admin console, clients like the GMail web client then display "Tom Tester" as sender. However, I'd like to specify the mail resource in the glassfish-resources.xml within our project, the configuration file doesn't allow < or >, because it's xml. I tried

<mail-resource 
    from="Tom Tester &lt;tom.tester@test.com&gt;"
    ... 

and

<mail-resource 
    from="Tom Tester tom.tester@test.com"
        ... 

, but these configurations won't work. Both approaches end up in sending only "tom.tester@test.com" as sender. I also didn't find any specification details from the GlassFish docs. Does somebody know if the desired behaviour is possible?

Was it helpful?

Solution

In case you want to explicitly set the personal name for the sender, you need to do it while creating the email message.

Let's say you have the session mailSession from the GlassFish Resource and you are creating a message mailMessage

Now you can set the from attribute of the message:

mailMessage.setFrom(new InternetAddress(mailSession.getProperty("mail.from"), "Tom Tester"));

Read more here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top