Primefaces link open an email compose window

11,766

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&amp;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.

Share:
11,766
lofa in
Author by

lofa in

Updated on July 07, 2022

Comments

  • lofa in
    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
    blo0p3r almost 11 years
    By using the JSF method, it escapes my subject and body : Some+subject and Some+body respectively. Anyway to get around this (which would make for a more elegant solution)?
  • LStrike
    LStrike about 10 years
    Same problem here and no solution.
  • BalusC
    BalusC about 10 years
    Just press "Ask Question" button then?