给定:
String email1 = "simple@example.org";
// legal email address according to wikipedia [1]
String email2 = "\"()<>[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a\"@example.org";
什么是创建mailto: URI(以字符串对象形式)的最佳/正确方式?
我尝试过:
String uri = new URI("mailto", the_email_address, null).toString();
这是我找到的最接近的答案,但它没有对电子邮件地址的本地部分中的问号(?)进行编码,但根据RFC 6068,应该进行编码。 它还不能处理RFC中的示例,例如"not@me"@example.org或unlikely?address@example.com。
[1] 有效电子邮件地址示例 附注:mailto:超链接中应该对加号进行编码吗?中有一些有用的信息。
我接受了这个作为解决方案:
import org.apache.http.client.utils.URIBuilder;
// from Apache HttpClient
// maven group: org.apache.httpcomponents artifact: httpclient
String emailURL = new URIBuilder().setScheme("mailto").setPath(the_email_address).toString();