My application sends an HTML email with a hyperlink. The user should be able to click on the link in a mail client and the browser opens the page. Easy... i thought.
But now the link contains special characters in a query param. E.g. the value for the second query param is id=1234/pid=1000
and the resulting java.net.URLEncoder.encode()
d URL becomes
http://example.com/path/?key1=value1&key2=id%3D1234%2Fpid%3D1000
And this is what gets embedded into an HTML mail:
<html ...
<head>
<meta http-equiv=Content-Type content="text/html; charset=unicode">
...
<body lang=EN-US ...
<a href="http://example.com/path/?key1=value1&key2=id%3D1234%2Fpid%3D1000">link text</a>
...
(at least this can be seen in the raw mail content by the receiving client).
Problem is that when clicking on the link, the browser opens up but the query params are double encoded:
http://example.com/path/?key1=value1&key2=id%253D1234%252Fpid%253D1000
and the link does not work anymore. Funny thing, if the link is copy/pasted into the browser, the additional encoding is omitted (as expected).
When hovering over the link with the mouse, the URL is displayed decoded:
http://example.com/path/?key1=value1&key2=id=1234/pid=1000
(tested with Outlook and Thundbird, both have same behaviour).
So my question is, what is wrong here?
- Is it my first encoding and should I skip it?
- Is it my first encoding and am I doing it wrong?
- Are the mail clients to blame?
I can find tons of information about how URL encoding works and that it should be done but not which encoding in which use cases. Thanks.