javaemailsmtpapache-commonsapache-commons-email

Substitute sender email address with a name in Apache commons email


Is there a way to show sender's name instead of an e-mail address when sending an e-mail?

This is what I'm currently doing :

Email email = new HtmlEmail().setStartTLSRequired(true);
    email.setDebug(false);
    email.setHostName("smtp.test.ch");
    email.setSmtpPort(587);
    email.setAuthentication("***", "***");
    email.setFrom("testemail@test.ch");
    email.setSubject(subject);
    email.setMsg(message);
    email.addTo(to);
    email.send();

So I'd like to substitute "testemail@test.ch" with the name.

Thanks,


Solution

  • Try setting sender's email as below.

     email.setFrom("John Doe<testemail@test.ch>");
    

    This will show the name of sender as John Doe in receiver's inbox.