Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 446
abeger Avatar asked Oct 14 '09 16:10

abeger


People also ask

How do I get content from MimeMessage?

You can use Simple Java Mail (Open Source) to convert a MimeMessage to an Email object and then access everything, including attachments and embedded images, recipients, html, text and headers: Email email = EmailConverter. mimeMessageToEmail(mimeMessage);

What is MimeMessage?

MimeMessage uses the InternetHeaders class to parse and store the top level RFC 822 headers of a message. The mail. mime. address. strict session property controls the parsing of address headers.

How do I send an email using JavaMail?

Steps to send email using JavaMail APIGet the session object that stores all the information of host like host name, username, password etc. compose the message. send the message.


1 Answers

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

like image 185
abeger Avatar answered Sep 20 '22 21:09

abeger