我正在玩JSF,我想知道这一点,我想向用户发送一个邀请,让我们说,我的网站。邀请函将向我想邀请的用户发送一封电子邮件,说Foo发送了这封电子邮件,并且该电子邮件还包含一个供用户打开的链接,一个字段已经填满了Foo的名字/电子邮件。问题是我不知道如何从URL中获取该参数并将其放在任何字段中,如outputText或label。
类User包含基本内容。
public class User {
private String name;
private String email;
//Getters and Setters here.
Mail类有一个方法,用于保存发送电子邮件的属性(如正文,主题和命运)
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);
}
}
类Builder使用UriBuilder java类,因此可以构建电子邮件正文并在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);
}
我的程序会向用户发送一封包含其姓名的电子邮件,其链接如下:
本地主机:8080 / BuilderTest /面/的index.xhtml;富
然而,我想获得链接的最后一部分" Foo"即发送邀请以在屏幕上打印的用户
<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>