我在java中编写了一个代码,其中有表单(输入字段),最后是底部的发送邮件按钮。当用户点击按钮时,输入字段中的数据应被提取,然后用作电子邮件中正文的数据。
这是我的代码:
if (role.getValue().equals("1")) {
Desktop desktop = Desktop.getDesktop();
String message = "mailto:username@domain.com?subject=Profildaten&body=" +
"Externe%20Referenz:%20" +
person.getExternalReference() + "%20" + "-%20" +
person.getExternalReferenceType() + "%0A" +
person.getTitle() + "%20" +
person.getContactLandline() + "%0A" +
"Mobil:%20" + person.getContactMobile() + "%0A" +
"Addresse:" + person.getContactStreet();
URI uri = URI.create(message);
try {
desktop.mail(uri);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这一切都很完美,但唯一的问题是地址:person.getContactStreet()实际输入字段要求用户输入街道名称,通常它是两个单词,例如克伦威尔路 - 在克伦威尔和公路之间有一个空间。现在电子邮件的正文不允许有空格和其他无效字符,因此它会弹出一条错误消息,说明输入了无效字符。如何让它接受这个或自动将无效字符转换为url编码?