简体   繁体   English

mailto URI在Java.Desktop和Windows / MS Outlook之间被截断

[英]mailto URI truncated between Java.Desktop and Windows/MS outlook

I'm trying to create an automated error reporting tool for our Java desktop app. 我正在尝试为我们的Java桌面应用程序创建一个自动错误报告工具。 the idea is to make it as easy as possible for customers to send us error reports whenever our application crashes. 这样做的目的是让客户在我们的应用程序崩溃时尽可能轻松地向我们发送错误报告。

Using the Desktop.mail API, I am able to craft messages that can be easily edited and sent from our users, but I'm running into system limitations on several platforms (notably Windows 7 and MS Outlook, which most customers are using) 使用Desktop.mail API,我能够编写易于编辑并从用户发送的消息,但是我遇到了多个平台(尤其是大多数客户使用的Windows 7和MS Outlook)上的系统限制

When I run the example code below, you'll notice that the email message that is displayed truncates the included stack trace. 当我运行下面的示例代码时,您会注意到显示的电子邮件会截断所包含的堆栈跟踪。 I believe this has something to do with a maximum length of either command lines or URIs in the underlying systems. 我相信这与底层系统中命令行或URI的最大长度有关。

Is there a better way to craft an email from an error report that is not subject to this limitation? 有没有更好的方法可以从不受此限制的错误报告中制作电子邮件?

import java.awt.Desktop;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.net.URLEncoder;

public class Scratchpad {

    public static void main(String[] args) throws Exception {
        try {
            generateLongStackTrace();
        } catch (Error e) {
            URI uri = createMailURI(e);
            // this will correctly pop up the system email client, but it will truncate the message
            // after about 2K of data (this seems system dependent)
            Desktop.getDesktop().mail(uri);
        }
    }

    // Will eventually generate a really long stack overflow error
    public static void generateLongStackTrace() throws Exception {
        generateLongStackTrace();
    }

    public static URI createMailURI(Error e) throws Exception {
        StringBuilder builder = new StringBuilder();
        builder.append("mailto:foo@example.com?body=");
        // encodes the stack trace in a mailto URI friendly form
        String encodedStackTrace = URLEncoder.encode(dumpToString(e), "utf-8").replace("+", "%20");
        builder.append(encodedStackTrace);
        return new URI(builder.toString());
    }

    // Dumps the offending stack trace into a string object.
    public static String dumpToString(Error e) {
        StringWriter sWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(sWriter);
        e.printStackTrace(writer);
        writer.flush();
        return sWriter.toString();
    }

}

there are length limitations wrt admissible urls in ie and the length of a windows command line (see here , here , here and here ) - i seems you run into one of these (though i admit that i have not rigorously checked). 在ie以及Windows命令行的长度(请参见此处此处此处此处 )的长度中都有可允许的url的长度限制-我似乎遇到了其中一种(尽管我承认我没有严格检查过)。
however i think it's a plausible assumption that even if you could worm your way around the said limits the length of a generic transmission buffer between desktop applications (unless you use a dedicated api for remote controlling the target app) will be restricted somehow without a loophole. 但是,我认为这是一个合理的假设,即使您可以绕过上述限制而在台式机应用程序之间通用传输缓冲区的长度(除非您使用专用的api来远程控制目标应用程序)的长度也会受到某种限制而不会出现漏洞。

therefore i'd suggest one of the following strategies: 因此,我建议采用以下策略之一:

  1. distribution through a web server. 通过Web服务器分发。

    • upload the data to be mailed to a web server instead using the html form file upload technique. 而是使用html表单文件上传技术将要邮寄的数据上传到Web服务器。 basically you have to forge a POST request a payload with content type set to 'multipart/form-data'. 基本上,您必须伪造一个POST请求,将内容类型设置为“ multipart / form-data”的有效负载。 your content will need some wrapper data to conform syntactically with this mime type. 您的内容将需要一些包装器数据,以使该语法在语法上符合该类型。
    • the actual transmission can be instigated by means of the WinHttpRequest COM object under windows or the curl command line program from everywhere else. 实际的传输可以通过Windows下的WinHttpRequest COM对象或其他任何地方的curl命令行程序来实现。
    • server side processing can be delegated to a suitable cgi handler which eg. 服务器端处理可以委托给适当的cgi处理程序,例如。 might produce a (short) link to download the data fom the web server. 可能会产生一个(简短的)链接,以从Web服务器下载数据。
    • this link may be part of the http response to the upload request or you generate it client-side in the proper format for publishing it on the web server unaltered. 此链接可能是对上传请求的http响应的一部分,或者您以正确的格式在客户端生成了该链接,以将其发布到Web服务器上而未更改。
    • pro: 优点:
      this scheme is feasible - i have repeatedly applied it in enterprise projects. 这个方案是可行的-我已经在企业项目中反复应用了它。 data transmission can be secured through https. 可以通过https保护数据传输。
    • con: 缺点:
      requires a web server to implement 需要Web服务器来实施
  2. send a mail using an attachment (for some details see here ): 使用附件发送邮件(有关详细信息, 请参见此处 ):

    • save the body of your message to some file on the desktop. 将邮件正文保存到桌面上的某些文件中。
    • generate a mailto-link that references an attachment (instead of the bulk of your body) 生成引用附件的邮件链接(而不是您的大部分内容)
    • any decent mail client will be able to show the attachment inline if it has some elementary mime type like 'text/plain'. 如果任何体面的邮件客户端具有一些基本的MIME类型(例如“文本/纯文本”),则可以内联显示附件。 on windows platforms you set it by choosing the proper file extension ('.txt') 在Windows平台上,您可以通过选择适当的文件扩展名('.txt')进行设置
    • pro: 优点:
      simple 简单
    • con: 缺点:
      file system access on the client platform; 客户端平台上的文件系统访问; untested (at least by me) 未经测试(至少由我测试)

good luck ! 祝好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM