51

Currently, our application uses a javax.mail to send email, using javax.mail.MailMessage. We set the From headers of the email this way:

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress("mail@companyxyz.com"));

This works just fine, but we'd like to make the "From" section a little more user-friendly. Currently, someone receiving an email will see "mail@companyxyz.com" in the "From" section of their inbox. Instead, we'd like them to see "Company XYZ" there. I figure this is probably done with the addHeader() method, but I'm not sure what the header name would be.

abeger
  • 6,766
  • 7
  • 41
  • 58

4 Answers4

126

OK, reading documentation about ALL the classes involved would have been helpful. The correct syntax should be

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress("mail@companyxyz.com", "Company XYZ"));

Source: https://javamail.java.net/nonav/docs/api/javax/mail/internet/InternetAddress.html

Milan
  • 1,903
  • 17
  • 16
abeger
  • 6,766
  • 7
  • 41
  • 58
  • 2
    It might be worth testing whether "Company XYZ " allows you to use the IntenetAddress(String, boolean) constructor to strictly check the address syntax but still have a personal name. – erickson Oct 14 '09 at 16:43
23

If you want to store the email + the name in one string (easier than keeping two string):

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress("Company XYZ <mail@companyxyz.com>"));
checklist
  • 12,340
  • 15
  • 58
  • 102
  • 2
    +1 because this solution works also with `SimpleMimeMessage` of Spring Mail, where no instance of `MimeMessage` can be accessed and no `InternetAddress` is used directly. It operates only on strings. – Jagger Nov 29 '16 at 18:44
3

In case when I used localized text with special characters like \u00FA I had problems with encoding email address alias for some pop3 clients if I'd used just

MimeMessage m = new MimeMessage(session);
m.setFrom();

It can be resolved by separate email address and alias by invoke:

MimeMessage m = new MimeMessage(session);
            m.setFrom(new InternetAddress(session.getProperty("mail.from"), session.getProperty("mail.from.alias"),"UTF8"));

ref: https://javamail.java.net/nonav/docs/api/javax/mail/internet/InternetAddress.html#InternetAddress(java.lang.String,%20java.lang.String,%20java.lang.String)

2
ic = new InitialContext();

final Session session = (Session) ic.lookupLink(snName);
final Properties props = session.getProperties();

props.put("mail.from", mailFrom); //blabla@mail.com
props.put("mail.from.alias", mailName);//"joao Ninguem"

// Create a message with the specified information.
final MimeMessage msg = new MimeMessage(session);
msg.setSubject(subject);
msg.setSentDate(new Date());

msg.setFrom(new InternetAddress(session.getProperty("mail.from"), session.getProperty("mail.from.alias"), "UTF8"));


msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo, false));
msg.setContent(body, "text/html");

// Create a transport.
Transport.send(msg);
clinomaniac
  • 2,200
  • 2
  • 17
  • 22