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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With