Search code examples
javaencodingutf-8mimedecoding

MimeUtility.decode() doesn't work for every encoded text


I am working o a mail application and I have some troubles with decoding mime encoded text. I am using MimeUtility.decode() but it doesn't for every encoded text. Some texts are decoded properly but others couldn't.

These encoded text which can't be decoded especially have utf-8 and iso-8859-9 encoding type.

How I can solve this issue??

This is the code I used for decoding

MimeUtility.decodeText(text);

These are example of failing text:

Failing Text 1

Failing Text 2


Solution

  • ****Solution***** (Thanks to @user_xtech007)

    I solve this with problem with decoding encoded parts by splitting multiple encoded parts with regex .

    Here is the codes of method I using

    private final String ENCODED_PART_REGEX_PATTERN="=\\?([^?]+)\\?([^?]+)\\?([^?]+)\\?=";
    
    private String decode(String s)
    {
        Pattern pattern=Pattern.compile(ENCODED_PART_REGEX_PATTERN);
    
        Matcher m=pattern.matcher(s);
    
        ArrayList<String> encodedParts=new ArrayList<String>();
    
        while(m.find())
        {
            encodedParts.add(m.group(0));
    
        }
    
        if(encodedParts.size()>0)
        {
            try
            {
                for(String encoded:encodedParts)
                {
                    s=s.replace(encoded, MimeUtility.decodeText(encoded));
                }
    
                return s;
    
            } catch(Exception ex)
            {
                return s;
            }
        }
        else
            return s;
    
    }