Primefaces link open an email compose window
Solution 1
Use the HTML standard mailto:
link syntax. You just need to make sure that the generated HTML link ends up to look like
<a href="mailto:[email protected]?subject=Some%20subject&body=Some%20body">mail</a>
This can in JSF be achieved by for example
<h:outputLink value="mailto:#{user.email}">
<f:param name="subject" value="Some subject" />
<f:param name="body" value="Some body" />
<h:outputText value="mail" />
</h:outputLink>
Those links will open the client's default mail composition editor. The subject
and body
parameters are optional and allows you to set a default subject and body in the email editor.
Solution 2
The bad news is that the JSF tag h:outputLink
escapes the subject and body as query parameters, replacing spaces with +
instead of %20
.
The good news is that you can achieve the correct result with java.net.URI class:
java.net.URI uri = new java.net.URI("mailto", "[email protected]", "subject=Some subject&body=Some body");
String result = "mailto:?" + uri.getRawFragment();
assert result.equals("mailto:?subject=Some%20subject&body=Some%20body");
Note that the first two arguments in the constructor are irrelevant.
You can use the result
in the href
attribute of your JSF/Primefaces tag, such as h:outputLink
or p:button
which opens the default e-mail client with a pre-defined subject and body and an empty recipient address.
lofa in
Updated on July 07, 2022Comments
-
lofa in almost 2 years
With JSF-Primefaces how can I make a link on a dataTable column for emailId, such that onclick will open an email compose window? I'm using Primefaces.3.0.M3 with JSF2.
-
blo0p3r almost 11 yearsBy using the JSF method, it escapes my subject and body :
Some+subject
andSome+body
respectively. Anyway to get around this (which would make for a more elegant solution)? -
LStrike about 10 yearsSame problem here and no solution.
-
BalusC about 10 yearsJust press "Ask Question" button then?