我正在嘗試爲我們的Java桌面應用程序創建一個自動錯誤報告工具。我們的想法是儘可能讓客戶在我們的應用程序崩潰時向我們發送錯誤報告。mailto URI在Java.Desktop和Windows/MS Outlook之間截斷
使用Desktop.mail API,我可以製作消息,可以很容易地編輯並從我們的用戶發送,但我遇到了幾個平臺上的系統限制(特別是Windows 7和MS Outlook,大多數客戶正在使用)
當我運行下面的示例代碼時,您會注意到顯示的電子郵件截斷了包含的堆棧跟蹤。我相信這與底層系統中命令行或URI的最大長度有關。
有沒有更好的方法來製作電子郵件從錯誤報告,不受此限制?
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:[email protected]?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();
}
}
感謝您的想法。過去幾天我一直在調查選項2。看來附件選項不是mailto URI規範的正式部分,雖然一些較老的電子郵件客戶端(outlook 98/2003)確實支持這個元素,但大多數現代郵件客戶端(Outlook 2010)都不支持,所以它並不是真的可行選項了。 – JohnnyO 2012-03-01 21:35:42
有趣。第二個想到的限制可能是明智的,自動附加文件使得它太容易打破安全...感謝信息。 – collapsar 2012-03-01 22:16:22