It's all about how to capture JSF output. My approach is to simply invoke the JSF page via JAX-RS, draw output and put it in email. Be careful to make the page as simple as possible (JSF likes to add a lot of JS code) and set absolute URLs for resources. Here is my code (simplified):
@Resource(name = "mail/mySession") Session mailSession; //make request, capture output Client client = ClientBuilder.newClient(); String htmlBody = client.target("http://localhost:8080/AppName/jsfpage.xhtml") .queryParam("id", 10) //we can add some params .request() .get(String.class); //send email Message message = new MimeMessage(mailSession); message.setFrom(); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(" asd@asd.pl ")); message.setHeader("X-Mailer", "JavaMail"); message.setSubject("Subject here"); message.setContent(htmlBody, "text/html; charset=utf-8"); //set captured html as content message.setSentDate(new Date()); Transport.send(message);
source share