Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
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)
Java
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
Java
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:
Java
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;
}


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)?
Posted

1 solution

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.

Java
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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900