简体   繁体   English

Play框架-通过SendGrid接收电子邮件-电子邮件正文的字符编码

[英]Play Framework - receiving email through SendGrid - character encoding of email body

I am developing a small mail client in the Java Play Framework and I'm using SendGrid for the e-mails. 我正在Java Play Framework中开发一个小型邮件客户端,并且正在使用SendGrid来发送电子邮件。 When an e-mail is received, it gets posted to a url and I then parse the posted form using JsonNode. 收到电子邮件后,它将被发布到url,然后我使用JsonNode解析发布的表单。 Now the problem is the "to", "from", "subject" fields of that form are automatically converted by SendGrid to UTF-8. 现在的问题是该表单的“ to”,“ from”,“ subject”字段由SendGrid自动转换为UTF-8。 Now comes the problem: apparently, the email message body is encoded in "ISO-8859-1". 现在出现了问题:显然,电子邮件正文是用“ ISO-8859-1”编码的。 And I need to convert that String to "UTF-8". 而且我需要将该字符串转换为“ 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: 我已经尝试了几种方法,但是很可能是我做错了,因为我总是对包含重音/变音符的法语或德语单词感到陌生的字符(示例“Zürich”以“ Z?rich”出现)。我用于转换的代码如下:

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! 提前非常感谢您!

Ok so I managed to get the raw byte request from SendGrid using the annotation and created the java String with the correct encodings: 好的,所以我设法使用注释从SendGrid获取了原始字节请求,并使用正确的编码创建了Java String:

@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. 现在的问题是,为了从请求中检索文件附件,我需要将请求解析为MultipartFormData。 With the annotation above set, I get a NullPointerException when calling, which was predictable: 设置完上面的注释后,调用时会收到NullPointerException,这是可以预测的:

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) ? 你们是否对我如何再次获得相同的请求有任何想法,但是可以使用@ 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. 因此,我有点需要结合这两个注释或找到一种方法,将我从Raw解析器获得的byte []转换为MultiFormData。 Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM