Search code examples
unixcharacter-encodingjakarta-mail

Question marks in email for characters like non-breaking space. It only occurs on Unix and not on Windows


I am facing a weird problem related to content type/encoding. Here is my Java code snippet below. This code works perfectly fine on a Windows machine where the application server is running on windows and the SMTP server for sending emails is also Windows localhost. When I deploy the same code on a Unix server, the email sent for the exact same content contains question marks (???) for special characters like non-breaking white space.

I did a lot of googling, but I did not find any solution. How can I fix this problem? The content types I tried were ISO-8859-1, UTF-8 and Windows-1252. Nothing helps.

        MimeMessage message = new MimeMessage(session);
                .............

        Multipart mp = new MimeMultipart();
        MimeBodyPart messageBody = new MimeBodyPart();
        messageBody.setContent(mailMessage, "text/html;charset=Windows-1252");
        messageBody.setHeader("Content-Type", "text/html;charset=Windows-1252");

        // Add body to the multimedia part
        mp.addBodyPart(messageBody);
        message.setContent(mp);

        // Send message
        Transport.send(message);

Solution

  • Ultimately, I had to go with a crude way of doing it. I replaced such characters with space.

    mailMessage.replaceAll("[^\\x20-\\x7e]", " ");
    

    Now, all the special characters like non-breaking space or any other character falling out of normal range, will be replaced with space. The email in this case was anyway meant for normal text.