I am playing with JSF a bit and I wondered this, I wanted to send to an user an invite to, let's say, my site. The invite is going to send an email to the user I wanted to invite, saying that Foo sent this email and the email also contains a link for the user to open and a field would come already filled with the Foo's name/email. The problem is that I have no idea how to take that parameter from the URL and put it in any field like an outputText or label.
The class User contains the basic stuff.
public class User {
private String name;
private String email;
//Getters and Setters here.
The class Mail has a method that holds the properties to make an email(like body, subject and destiny)
public class Mail {
public void sendMailPwd(String emailDestiny, String emailSubject, String emailBody){
final Properties props = new Properties();
props.put("mail.smtp.host", "smtp.outlook.com"); // SMTP Host
props.put("mail.smtp.port", "587"); // TLS Port
props.put("mail.smtp.auth", "true"); // enable authentication
props.put("mail.smtp.starttls.enable", "true"); // enable STARTTLS
final Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("foo@outlook.com", "barpass");
}
});
session.setDebug(true);
try {
final Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("foo@outlook.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailDestiny));
message.setSubject(emailSubject);
message.setText(emailBody);
Transport.send(message);
} catch (final MessagingException e) {
throw new RuntimeException(e);
}
}
And the class Builder, makes use of the UriBuilder java class so it can build the body of the email and create the link with the user's name in the URL
public class Builder {
Mail mail = new Mail();
public void sendInvite(String emailDestiny, User user) {
UriBuilder builder = UriBuilder
.fromPath("localhost:8080/")
.path("BuilderTest/faces/index.xhtml")
.replaceMatrix(user.getName());
URI uri = builder.build();
String emailSubject = "INVITE!";
String emailBody =
"Congrats! You got invited by: " + user.getName()
+ "\n Click on the link to get access to our services!"
+ "\n " + uri ;
mail.sendMailPwd(emailDestiny, emailSubject, emailBody);
}
My program is send users an email with their name, and the link is coming out like this:
localhost:8080/BuilderTest/faces/index.xhtml;Foo
However, I wanted to get that last part of the link "Foo" that is the user that sent the invite to get printed on the screen
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta charset="utf-8" />
</h:head>
<h:body>
<p:outputLabel value="Hola Mundo!"/>
<!-- Some output tag that will display foo's name -->
</h:body>
</ui:composition>