我试图发送带附件的电子邮件,但它一直在说:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Illegal character in opaque part at index 64: mailto:recipient@mailserver.com?subject=ThePDFFile&attachment=C:\Users\Rascal\AppData\Local\Temp\FreelancerList-16-12-2014_09-227568200505392670736.doc
Java代码:
Desktop desktop = Desktop.getDesktop();
String message = "mailto:recipient@mailserver.com?subject=ThePDFFile&attachment=\""+path;
URLEncoder.encode(message, "UTF-8");
URI uri = URI.create(message);
desktop.mail(uri);
结肠应该是吗?但为什么???
答案 0 :(得分:3)
您正在调用URLEncoder.encode
,但忽略了结果。我怀疑你正在努力实现这样的目标:
String encoded = URLEncoder.encode(message, "UTF-8");
URI uri = URI.create(encoded);
...虽然在那时你也会在mailto
部分之后对冒号进行编码。我怀疑你真的想要这样的东西:
String query = "subject=ThePDFFile&attachment=\""+path;
String prefix = "mailto:recipient@mailserver.com?";
URI uri = URI.create(prefix + URLEncoder.encode(query, "UTF-8"));
甚至只编码值:
String query = "subject=" + URLEncoder.encode(subject, "UTF-8");
+ "&attachment=" + URLEncoder.encode(path, "UTF-8"));
URI uri = URI.create("mailto:recipient@mailserver.com?" + query);
...或者当然,分别从各个不同的部分创建URI
。