简体   繁体   English

Java mailto非法字符冒号?

[英]Java mailto Illegal character colon?

im trying to send an email with an attachment, but it keeps saying: 我试图发送带有附件的电子邮件,但一直在说:

 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 Code: 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);    

Should be the colon right? 应该是冒号吧? But why??? 但为什么???

You're calling URLEncoder.encode , but ignoring the result. 您正在调用URLEncoder.encode ,但是忽略了结果。 I suspect you were trying to achieve something like this: 我怀疑您正在尝试实现以下目标:

String encoded = URLEncoder.encode(message, "UTF-8");
URI uri = URI.create(encoded);

... although at that point you'll have encoded the colon after the mailto part as well. ...尽管此时您还将在mailto部分之后对冒号进行编码。 I suspect you really want something like: 我怀疑您真的想要这样的东西:

String query = "subject=ThePDFFile&attachment=\""+path;
String prefix = "mailto:recipient@mailserver.com?";
URI uri = URI.create(prefix + URLEncoder.encode(query, "UTF-8"));

Or even encoding just the values: 甚至只编码值:

String query = "subject=" + URLEncoder.encode(subject, "UTF-8");
    + "&attachment=" + URLEncoder.encode(path, "UTF-8"));
URI uri = URI.create("mailto:recipient@mailserver.com?" + query);

... or create the URI from the various different parts separately, of course. ...或者当然可以从各个不同的部分分别创建URI

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

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