Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InternetAddress only the email address without name?

I'm working on a IMAP application with Java, and I want to retrieve only the email address without the name. Here is the relevant code I'm using.

Message[] msg = folder.getMessages();
for (int i = 0; i < msg.length; i++)
{
  if (!msg[i].isSet(Flag.SEEN))
  {
    EmailSenderInfo emailSenderInfo = new EmailSenderInfo();                            
    String from = InternetAddress.toString(msg[i].getFrom());
  }
}

When I print the variable "from", it prints something like below

name <[email protected]>

How can I only get the email address without the name?

like image 296
Arya Avatar asked Sep 15 '25 11:09

Arya


1 Answers

The getFrom method returns an array of Address objects, which will actually be InternetAddress objects. Normally there's only one From address so you can just use the first element in the array. Then use the InternetAddress.getAddress method:

 InternetAddress ia = (InternetAddress)msg[i].getFrom()[0];
 String from = ia.getAddress();
like image 135
Bill Shannon Avatar answered Sep 17 '25 01:09

Bill Shannon