开发者

Desktop.Action.MAIL encoding subject and body strings correctly for mailto: in URI

开发者 https://www.devze.com 2023-03-21 22:10 出处:网络
I have a question specific to the Java Desktop API in Java 6, more specifically desktop.mail(URI uri)..

I have a question specific to the Java Desktop API in Java 6, more specifically desktop.mail(URI uri)..

I was wondering if there is a function one could use to ensure that the Subject and Body in f.ex:

mailToURI = new URI("mailto", getToEmails() + "?SUBJECT=" + getEmailSubject() + "&BODY=" + getEmailBody(), null);

desktop.mail(mailToURI);

will be kept in accordance with rfc2368 and still be displayed correctly in the email application?

Right now examples of problematic texts are the scandinavian letters: æøå / ÆØÅ and adding complex URLS in the Body containing ampersands (&) and such f.ex: http://www.whatever.com?a=b&c=d etc..

Is there a function in Java that ensures the aboved seeked integrity is preserved when using the mailto: URI scheme with Java Desktops mail(URI) function?

Would it be possible to make one?

At this point I have tried everything I can think of including:

  • MimeUtility.encodeText()
  • URLEncode.encode(..
  • A custom function encodeUnusualCharacters()

private static final Pattern SIMPLE_CHARS = Pattern.compile("[a-zA-Z0-9]");

private String encodeUnusualChars(String aText) {
    StringBuilder result = new StringBuilder();
    CharacterIterator iter = new StringCharacterIterator(aText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        char[] chars = {c};
        String character = new String(chars);
        if (isSimpleCharacter(character)) {
            result.append(c);
        } else {
            //hexEncode(character, "UTF-8", result);
        }
    }
    return result.toString();
}

private boolean isSimpleCharacter(String aCharacter) {
    Matcher matcher = SIMPLE_CHARS.matcher(aCharacter);
    return matcher.matches();
}

/**
For the given character and encoding, appends one or more hex-encoded characters.
For double-byte characters, two hex-encoded items will be appended.
 */
private static void hexEncode(String aCharacter, String aEncoding, StringBuilder aOut) {
    try {
        String HEX_DIGITS = "0123456789ABCDEF";
        byte[] bytes = aCharacter.getBytes(aEncoding);
        for (int idx = 0; idx < bytes.length; idx++) {
            aOut.append('%');
            aOut.append(HEX_DIGITS.charAt((bytes[idx] & 0xf0) >> 4));
            aOut.append(HEX_DIGITS.charAt(bytes[idx] & 0xf));
        }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(LocalMail.class.getName()).log(Level.SEVERE, null, ex);
    }
}
  • And many more...

At the best I end up with the encoded text in the email that is opened up.

Not providing any special encoding will cause æøå or similar to stop further processing of the content.

I feel I am missing something crucial. Could anyone please enlighten me with a solution to this?

For line breaks I use String NL = System.getProperty("line.separator");

Perhaps there is some System specific stuff that needs to be called to make this work??

By the way I am currently on Mac OS X 10.6.8 with Mail 4.5

marius$ java -version java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03-384-10M3425) Java HotSpot(TM) Client VM (build 20.1-b02-384, mixed mode)

I really feel there must be a way - otherwise the subject and message part of the desktop.mail(URI) function is completely unreliable to the point of being useless.

Any help to point me in the right direction is grea开发者_JAVA百科tly appreciated!!


Thanks Marius, it's a very useful line of code.

I modified it a bit for performances...

It's better to use "replace" instead of "replaceAll", when you are not using RegExp.

This:

.replace("+", "%20")

is faster than:

.replaceAll("\\+", "%20")

Both replace ALL occurrences, but the first one does not have to do any regexp parsing. http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29

Also, if the original string already has \r\n for new lines, the second replace will double the \r. It's not a big issue, but I prefer to remove that one and provide a proper input string:

String result = java.net.URLEncoder.encode(src, "utf-8").replace("+", "%20")


Try this, hope it will work for you.

String result = java.net.URLEncoder.encode(src, "utf-8").replaceAll("\\+", "%20").replaceAll("\\%0A", "%0D%0A");
0

精彩评论

暂无评论...
验证码 换一张
取 消