Setting the from name in a javax.mail.MimeMessage?

53,552

Solution 1

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("[email protected]", "Company XYZ"));

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

Solution 2

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 <[email protected]>"));

Solution 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)

Solution 4

ic = new InitialContext();

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

props.put("mail.from", mailFrom); //[email protected]
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);
Share:
53,552

Related videos on Youtube

abeger
Author by

abeger

Updated on March 21, 2020

Comments

  • abeger
    abeger about 4 years

    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("[email protected]"));
    

    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 "[email protected]" 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.

  • brady
    brady over 14 years
    It might be worth testing whether "Company XYZ <[email protected]>" allows you to use the IntenetAddress(String, boolean) constructor to strictly check the address syntax but still have a personal name.
  • Jagger
    Jagger over 7 years
    +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.
  • clinomaniac
    clinomaniac about 6 years
    Please explain a bit about what you are doing as well.