Pita
Pita

Reputation: 508

URL Encoding in Java Mail

I have written a code in java, where there are forms (input fields) and finally a send email button at the bottom. when the user clicks on the button, the data from the input fields should get extracted then be used as data for the body in the email.

this is the code i have:

if (role.getValue().equals("1")) {          
    Desktop desktop = Desktop.getDesktop();
    String message = "mailto:[email protected]?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();
    }   
}

It all works perfectly, but the only issue is for Address: person.getContactStreet() the actual input field requires the user to enter a street name, and usually it would be two words, e.g. Cromwell Road - with a space inbetween cromwell and road. now the body of the email doesnt allow to have spaces and other invalid characters, hence why it is popping an error message saying invalid characters have been entered. how can i make it accept this or convert the invalid characters automatically to url encodings?

Upvotes: 0

Views: 1808

Answers (1)

user1210061
user1210061

Reputation:

Have a look at this site, it tells you how to encode the URLS in the body automatically.

Best way to encode URL in Java

Upvotes: 5

Related Questions