views:

1346

answers:

2

I have similar problem to the one described in this question: I am using the "mailto" protocol to open the default mail client from Java (I am tied to Java 5 for now, so sadly I can't use the Desktop API).

Some of the emails contain Japanese text. The strings are already being UTF-8 encoded as follows:

private void email(String to, String subject, String body)
{
    String encodedSubject = URLEncoder.encode(subject, "UTF-8");
    String encodedBody = URLEncoder.encode(body, "UTF-8");

    String mailto = "mailto:" + to + "?subject=" + encodedSubject + 
        "&body=" + encodedBody;
    String cmd = "cmd.exe /c start \"\" \"" + mailto + "\"";
    Runtime.getRuntime().exec(cmd);
}

The Japanese characters are correctly encoded to their URL-equivalents, so "平" becomes "%E5%B9%B3", for example; however, when Outlook opens the new mail window, the three-byte character is interpreted as three distinct characters - so "%E5%B9%B3" is interpreted as "å¹³".

I am fairly convinced the problem lies with outlook, since the following HTML snippet produces the same effect (SO doesn't seem to allow mailto inside tags, so I can't provide the link directly, sorry):

<html>
    <body>
    <a href="mailto:[email protected]?subject=%E5%b9%B3">click me to test!</a>
    </body>
</html>

In short, how can I persuade Outlook to interpret multi-byte characters correctly when they're coming from a mailto link?

EDIT: To answer Johannes' question: we have a Java app which sends email when certain actions are performed. The standard text for each email is pulled from resource bundles, and in most cases we use the JavaMail API without any problems; but in this one case, there is a requirement for the user to be able to tailor the email before sending.

If anyone can suggest a non-cmd.exe way of producing the same effect (new mail window with subject and body prefilled) - and bearing in mind that we are tied to Java 5, so the Desktop API is sadly not an option - I would be very happy!

A: 

You are not specifying any encoding, so Outlook (or whoever gives the address to Outlook) can only guess. In case of your HTMl snippet, try whether specifying the encoding (UTF-8) explicitly in the <head> changes that behavior.

As far as cmd is concerned, it can't really cope with UTF-8. It handles Unicode as UTF-16, though, albeit with problems.

When I try this here (Windows Live Mail, instead of Outlook, though), everything in the subject gets converted to the legacy codepage so having CJK there should be a problem.

However, I wonder why you are trying to mis-use cmd to let the user write a mail anyway. There are surely better alternatives out there (even though I currently don't know one because I never needed it).

Joey
A: 

I guess your Java mailer works OK but to be sure, try switching to JavaMail.

Have a look at this post. Outlook 2003 by default does not correctly recognize the character encoding of HTML mails (in many case) even though they contain the appropriate tag:

 <meta http-equiv="content-type" content="text/html; charset=UTF-8">

You can switch to Outlook 2007 and/or open the mail in editor and adjust the message properties:

  • open the message
  • Edit -> Edit message
  • Format -> Encoding -> select new encoding
  • File -> Save

Edit: Corrections after the question changed.

Edit2: Sorry hadn't read the full question.

I see you have the Java 5 constraint but if the implementation is also tied to Windows, consider using a JNI/JNA based solution (unfortunately I cannot give you links on that). I would also have a look at the OpenJDK's Desktop API implementation and extract the way from it.

Another option would be to somehow pass in the message without URLEncoding it?

Also if there is a requirement for the user to customize the message before sent, you could provide a simple editor for that within your application and still send the message via JavaMail.

You could also offer the cooperative way of preparing the message for the user, putting it onto the clipboard and open the e-mail client for the user. Then the user only needs to issue a CTRL+V to paste in the prepared text.

kd304