2

I have below java code, which pick an email from inbox and write to activeMQ queue which has Japanese Text as subject.

public class SimpleRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("imaps://outlook.office365.com?username=*****&password=****&unseen=true&connectionTimeout=600000&mapMailMessage=truecopyTo=out")
        .process(new MyProcessor())
        .to("jms:queue:OUTqueue");

    }
}

And in MyProcessor(), I am retrieving

String sub = exchange.getIn().getHeader("Subject").toString();

This subject returns "=?iso-2022-jp?B?UkU6IBskQiFaJDQwTU1qIVsbKEJqYXBhbmVzZQ==?=" and not the actual Japanese Text, there is no issue with body text.

Any help how to get the Japanese text of the Subject?

Gayan Mettananda
  • 1,498
  • 14
  • 21
NeedToLearn
  • 147
  • 1
  • 2
  • 11

2 Answers2

3

That getHeader method returns a raw header value, and it is encoded using RFC-2047 text encoding. Assuming you're using JavaMail, you can decode it using javax.mail.internet.MimeUtility, specifically MimeUtility.decodeText (and for fully correct behavior, unfold, see code below, although not necessary for the value shown).

Alternatively, if this is a JavaMail Message (specifically MimeMessage or subclass), use getSubject() instead of getHeader("Subject"). This will automatically decode the value. See its implementation in JavaMail 1.6.2:

public String getSubject() throws MessagingException {
    String rawvalue = this.getHeader("Subject", (String)null);
    if (rawvalue == null) {
        return null;
    } else {
        try {
            return MimeUtility.decodeText(MimeUtility.unfold(rawvalue));
        } catch (UnsupportedEncodingException var3) {
            return rawvalue;
        }
    }
}

For example, the following code:

String encodedValue = "=?iso-2022-jp?B?UkU6IBskQiFaJDQwTU1qIVsbKEJqYXBhbmVzZQ==?=";
String decodedValue = MimeUtility.decodeText(encodedValue);
System.out.println(decodedValue);

will print out:

RE: 【ご依頼】japanese
Community
  • 1
  • 1
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
0

Change ... message as byte[]to what is applicable in your case.

byte[] text = ... message as byte[];
String textAsUnicode = new String(text,"ISO2022JP");

Oracle's list of supported encodings