1

I am developing a small mail client in the Java Play Framework and I'm using SendGrid for the e-mails. When an e-mail is received, it gets posted to a url and I then parse the posted form using JsonNode. Now the problem is the "to", "from", "subject" fields of that form are automatically converted by SendGrid to UTF-8. Now comes the problem: apparently, the email message body is encoded in "ISO-8859-1". And I need to convert that String to "UTF-8". I already tried several ways of doing so, but most probably I'm doing something very wrong, since I always get strange characters for French or German words containing accents/umlauts (Example "Zürich" comes out as "Z?rich". The code I'm using for the conversion is the following:

byte[] msg = message.getBytes("ISO-8859-1");
byte[] msg_utf8 = new String(msg, "ISO-8859-1").getBytes("UTF-8");
message = new String(msg_utf8, "UTF-8");

Could you, please, suggest a solution? Thank you very much in advance!

franciscb
  • 115
  • 1
  • 9
  • What is "message"? Does http://stackoverflow.com/questions/652161/how-do-i-convert-between-iso-8859-1-and-utf-8-in-java/1559761#1559761 work for you? – Schleichardt Oct 18 '13 at 18:38
  • I'm getting the String message from the posted form (by SendGrid). No, I've already tried that. So I'm directly given this message String, which is in ISO-8859-1, according to the e-mail header. And I have to convert it to UTF-8, so I can store it in a database. – franciscb Oct 18 '13 at 19:12
  • Can you give the email header as byte array? To help you, your problem should be easy to reproduce. – Schleichardt Oct 18 '13 at 19:43
  • The message String I get like this: String message = form.get("text"); where final DynamicForm form = form().bindFromRequest(); So I don't really know what you mean? – franciscb Oct 18 '13 at 19:58
  • Isn't there anyone who has successfully received emails through SendGrid in Java? – franciscb Oct 20 '13 at 10:05

1 Answers1

0

Ok so I managed to get the raw byte request from SendGrid using the annotation and created the java String with the correct encodings:

@BodyParser.Of(BodyParser.Raw.class)
public static Result getmail() {
 ...
}

Now the problem is that for retrieving the file attachments from the request I would need the request to be parsed as MultipartFormData. With the annotation above set, I get a NullPointerException when calling, which was predictable:

request().body().asMultipartFormData().getFiles()

Does any of you have any idea on how I could get the same request again, but parsed with the @BodyParser.Of(Bodyparser.MultipartFormData.class) ? So I kind of need to combine the two annotations or find a way to convert the byte[] I get from the Raw parser to a MultiFormData. Thanks!

franciscb
  • 115
  • 1
  • 9