Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting only email address to display when using message.getFrom() in JavaMail

Currently when using JavaMail if I use getFrom() to decompose a message into its separate parts the getFrom() will also display the name of the sender. This may be a simple question but how do you make it so only the email address is returned. Sorry if this is a simple question but I cannot seem to find an answer.

like image 355
Rhys12341111 Avatar asked Mar 06 '11 23:03

Rhys12341111


People also ask

Which protocol is used to receive the messages in JavaMail?

IMAP: Acronym for Internet Message Access Protocol. It is an advanced protocol for receiving messages. It provides support for multiple mailbox for each user, in addition to, mailbox can be shared by multiple users. It is defined in RFC 2060.


1 Answers

As it turns out, the address has already been parsed for you. Because of JavaMail's silly extra layer of abstraction, it's returning InternetAddress objects as their Address superclass. Address objects are pretty much useless. You need to cast them back down to InternetAddress and then just get the email part:

Address[] froms = message.getFrom();
String email = froms == null ? null : ((InternetAddress) froms[0]).getAddress();
like image 100
dkarp Avatar answered Sep 20 '22 12:09

dkarp