Search code examples
utf-8character-encodingjakarta-mail

dumbster unit testing mail server does not return expected mail body?


i am writing the junit for email application that sends mail to any external domains like gmail etc. I am using dumbster(Fake mail server) apis for that which includes class like SmtpMessage,SmtpServer etc. i am writing the junit where i am sending the unicode body

   String unicodeBody = "TestBody\u0393"

But when i receive it from Fake mail server i get it like this "TestBody=CE=93". Code for smtp server that receives the mail is

  SmtpMessage email=smtpServer.getReceivedEmail().next();

i am not sure what i am missing here?

just for information i send the mail as MimeMessage (whose content type is multipart/alternative) which internally contains two mimeBodyParts i.e one for text body and another for html body.

EDIT:- Complete email body is:-

MIME-Version: 1.0
Message-ID: <18805827.1.1355311147338.JavaMail.SMiles@INN-L-0019>
Subject: subject
Date: Wed, 12 Dec 2012 16:49:07 +0530 (IST)
To: [email protected]
Content-Type: multipart/alternative;
Reply-To: [email protected]
From: [email protected]

------=_Part_0_13986615.1355311147225Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: quoted-printable
body=CE=93------=_Part_0_13986615.1355311147225Content-Type: text/html; charset=UTF-8Content-Transfer-Encoding: quoted-printable
body=CE=93------=_Part_0_13986615.1355311147225--

------=_Part_0_13986615.1355311147225Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: quoted-printable
body=CE=93------=_Part_0_13986615.1355311147225Content-Type: text/html; charset=UTF-8Content-Transfer-Encoding: quoted-printable
body=CE=93------=_Part_0_13986615.1355311147225--
{MIME-Version=[1.0], Message-ID=[<18805827.1.1355311147338.JavaMail.Smiles@INN-L-0019>], Subject=[subject], Date=[Wed, 12 Dec 2012 16:49:07 +0530 (IST)], To=[[email protected]], Content-Type=[multipart/alternative;], Reply-To=[[email protected]], From=[[email protected]]}

Solution

  • That is called Quoted Printable encoding, and you can decode it with Apache Commons Codec for instance:

    public static void main(String[] args) {
        QuotedPrintableCodec a = new QuotedPrintableCodec("UTF-8");
        try {
            System.out.println(a.decode("TestBody=CE=93"));
            // prints "TestBodyΓ"
        } catch (DecoderException e) {
            e.printStackTrace();
        }
    }
    

    It will first turn =CE=93 into the actual byte values 0xCE 0x93, and then decode them as UTF-8 (Which was passed in the constructor)