电子邮件-具有非ASCII字符的多部分邮件(阿拉伯语) [英] Email - multipart message with non-ASCII characters (Arabic)

查看:171
本文介绍了电子邮件-具有非ASCII字符的多部分邮件(阿拉伯语)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子邮件应用程序,应该通过 JavaMail API [ ^ ]发送到SMTP服务器.

现在,我需要发送带有阿拉伯文本和附件的电子邮件.阿拉伯文字显然是由非ASCII字符构建的.
我无法正确传输阿拉伯文字.我很确定我不是第一个这样做的人,所以让我们看看是否有人可以在这里帮助我.

效果很好:发送纯文本阿拉伯电子邮件(不带附件).
(我发布此信息是因为我想向您展示我在这里做什么)

I have an email application that is supposed to send emails via JavaMail API[^] to a SMTP Server.

Now I need to send emails with Arabic text and attachment. Arabic text is obviously build from non-ASCII characters.
I can''t get the Arabic text to be transmitted correctly. I''m pretty sure I''m not the first to do this so let''s see if someone can help me here.

what works fine: sending plain text Arabic Email without Attachment.
(I post this because I want to show you what I''m doing here)

MimeMessage oMimeMessage = new MimeMessage(Session.getDefaultInstance(
  getMimeMessageProperties(), // connection details
  getMailAuthenticator() // username & password
));
oMimeMessage.setText(strBodyText, "UTF-8");
// ... from and to and other things added.


不带附件的消息已正确发送,所有人都在接收它,并且阿拉伯文本显示正确.

什么不起作用:发送带有附件的阿拉伯电子邮件


the message without Attachment is send correctly, everybody is receiving it and the Arabic text is displayed correct.

what does not work: sending Arabic email with attachment

MimeMessage oMimeMessage = new MimeMessage(Session.getDefaultInstance(
  getMimeMessageProperties(), // connection details
  getMailAuthenticator() // username & password
));
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(getMessageBodyPart(oMail.getBody()));



邮件的正文由以下内容组成:



Here the body of the message is made up:

private BodyPart getMessageBodyPart(final String strBody) throws MessagingException  {
  BodyPart oMessageBodyPart = new MimeBodyPart();
  try {
    oMessageBodyPart.setHeader("Content-Transfer-Encoding", "8bit"); // does not work
    String strEncoded = MimeUtility.encodeText(strBody,"utf8",null); // UTF-8 Encoding to get the content across
    oMessageBodyPart.setDataHandler(new DataHandler(new   MessageBodyDataSource(strEncoded))); // text added as DataSource to provide charset UTF-8
    return oMessageBodyPart;
  } catch (Exception oException) {
    Logger.getLogger(SMTPParser.class).error("Can't process body text - will add plain text", oException);
    oMessageBodyPart.setContent(strBody, "text/plain");
  }
  return oMessageBodyPart;
}



以我的观点,我需要将Header Content-Transfer-Encoding: 8bit传递给接收者.
但是电子邮件拒绝并以7bit的价格接收-这毁了我的一天.



To my point of view I need to get the Header Content-Transfer-Encoding: 8bit across to the recipient.
But the email refuses and is received as 7bit - which ruins my day.

How do I get the Arabic body text transmitted correctly`(when attachments are involved)?

推荐答案

解决方案:

-将标题移动到添加数据源的下方
-将Content-Transfer-Encoding切换为"base64"
-编码 AND解码非ASCII字符串

我不知道为什么,但是最后一点是必需的.也许然后将String用某种unicode编码或以某种方式进行了编码,到目前为止,对此还没有进行太多研究.

Solution:

- move headers underneath adding of DataSource
- switch Content-Transfer-Encoding to "base64"
- encode AND decode the non-ASCII String

I don''t know why, but the last point turned out to be needed. Maybe the String is then encoded in some unicode or someway different, didn''t research to much on that so far.

private BodyPart getMessageBodyPart(final String strBody) throws MessagingException  {
	BodyPart oMessageBodyPart = new MimeBodyPart();
	try {
		String strEncoded = MimeUtility.encodeText(strBody,"UTF-8",null);
		String strDecoded = MimeUtility.decodeText(strEncoded);
		MessageBodyDataSource oDataSource = new MessageBodyDataSource(strDecoded);
		oMessageBodyPart.setDataHandler(new DataHandler(oDataSource));
		oMessageBodyPart.setHeader( "Content-Type", oDataSource.getContentType() );
		oMessageBodyPart.setHeader( "Content-Transfer-Encoding", "base64");
		return oMessageBodyPart;
	} catch (Exception oException) {
		Logger.getLogger(SMTPParser.class).error("Can't process body text - will add plain text", oException);
		oMessageBodyPart.setContent(strBody, "text/plain");
	}
	return oMessageBodyPart;
}


这篇关于电子邮件-具有非ASCII字符的多部分邮件(阿拉伯语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆