4

I made an E- Mail Client for my Android phone with the Javamail Api. If I try to get the sender's mail address and the recipients mail address with the following methods:

Address[] froma = m.getFrom();
        String from = InternetAddress.toString(froma);

        Address[] toa = m.getRecipients(Message.RecipientType.TO);
        String to = InternetAddress.toString(toa);

I get a String like this back:

"Georg =?ISO-8859-1?Q?SP=E4the?= and it has to be Georg Späthe or Georg Spaethe.

I think the Problem is that this are German Mails with another encoding. Can anybody help me to solve that problem?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jwandscheer
  • 178
  • 3
  • 17

3 Answers3

9

MIME headers are encoded as per RFC 2047, therefore you need to decode them first.

String decoded = MimeUtility.decodeText("Georg =?ISO-8859-1?Q?SP=E4the?=");

JDK import:

import javax.mail.internet.MimeUtility;

For Android:

import com.android.email.mail.internet;

See also Javadoc of MimeUtility

stacker
  • 68,052
  • 28
  • 140
  • 210
0

Each of the Address objects will be an InternetAddress, cast it to that and use the getAddress or getPersonal method depending on what you want.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
0

For what it's worth: You can cast to InternetAddress and use toUnicodeString that will return the decoded mail in the form of "Georg Späthe" <georg.spaethe@example.com> if your input address was "Georg =?ISO-8859-1?Q?SP=E4the?=" <georg.spaethe@example.com>. ALso, as explained by Bill Shannon, you can call getPersonal and getAddress to get the decoded values, so there is no need to fiddle around with MimeUtility yourself.

Lothar
  • 5,323
  • 1
  • 11
  • 27