0

这就是我目前正在做的事情:

private void openEmailButtonMouseClicked(MouseEvent e) {

    String mailto = getClientConfigValue("email", "mailto");
    String ccto = getClientConfigValue("email", "ccto");
    String subject = getClientConfigValue("email", "subject");
    String body = getClientConfigValue("email", "body");
    String currClient = getCurrClient();
    String bodyPath = getSpecificClientConfigPath(currClient) + body;
    String text = "";

    try {
        BufferedReader br = new BufferedReader(new FileReader(bodyPath));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            text = sb.toString();
            while (text.contains(" ")) {
                text = text.replace(" ", "%20");
            }
            text = text.replaceAll("(\r\n|\r|\n|\n\r)", "%0A");
        }
        finally {
            br.close();
        }
    }
    catch (Exception e1) {
        e1.printStackTrace();
    }

    try {
        Desktop.getDesktop().mail(new URI("mailto:" + mailto + "?subject=" + subject + "&cc=" + ccto + "&body=" + text));

    }
    catch (Exception e1) {
        e1.printStackTrace();
    }
}

它适用于 .txt 和 .doc(没有文本格式)。但它仍然不是很令人满意,因为我无法将格式化的文本发送到标准桌面电子邮件客户端,因为我被迫生成一个 url。有人知道通过 mailto 或其他任何方式将格式化文本解析到电子邮件客户端的方法吗?

4

1 回答 1

0

不,这不是 java 的限制,而是 mailto url 格式的限制(检查RFC2368)。例如,在 Web 浏览器中也是如此。

请参阅带有 HTML 正文的 MailTo

于 2014-01-17T15:56:03.753 回答