I want to send email with JSF. I created page that sends email successfully, but when I call this page dynamically from main menu it appears in center of my page, but some boxes do not appear and worst thing: Bean doesn't work at all. So my question is how should I include this page correctly, in order to still use all functionality of Beans, or alternatively how to call bean from this page. note: it works, just stops when is inside main page.
because I have also problem to call method sendMail2() of my Bean directly, at the moment I call setter of property (because it works) and inside this setter method I call sendMail2().
page:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h3>JSF 2.0 + Ajax Hello World Example</h3>
<h:form>
<h:inputText id="namez" value="#{mySendBean.name}"></h:inputText>
<h:commandButton value="Welcome Me">
<f:ajax execute="namez" render="outputz" />
</h:commandButton>
<h2><h:outputText id="outputz" value="#{helloBean2.sayWelcome}" /></h2>
</h:form>
</h:body>
</html>
Bean:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.mail.*;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
/**
*
* @author root
*/
@ManagedBean
@SessionScoped
public class mySendBean {
private myEmailSend mE;
/**
* Creates a new instance of mySendBean
*/
public mySendBean() {
mE=new myEmailSend();
}
private static int j;
/**
* Get the value of j
*
* @return the value of j
*/
public int getJ() {
return j;
}
/**
* Set the value of j
*
* @param j new value of j
*/
public void setJ(int j) {
this.j = j;
}
private String name="iop";
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
sendMail2();
}
public String getSayWelcome(){
//check if null?
if("".equals(name) || name ==null){
return "";
}else{
return "Ajax message : Welcome " + name;
}
}
public void setSendMail(){
sendMail2();
}
public void sendMail2(){
Email email = new SimpleEmail();
try {
String authuser = "me@gmail.com";
String authpwd = "pass";
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true);
email.setHostName("smtp.gmail.com");
email.getMailSession().getProperties().put("mail.smtps.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtps.port", "587");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("me@gmail.com", "Agencja Ubezpieczeniowa");
email.setSubject("TestMail");
email.setMsg("This is a test mail 4");
email.addTo("someone@gmail.com", "ToName");
//email.setStartTLSRequired(false);
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
}
}
and here how I render menu:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<f:facet name="first">
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
</f:facet>
</h:head>
<h:body>
<style type="text/css">
@import url("myCss.css");
</style>
<p:layout fullPage="true">
<p:layoutUnit styleClass="myLayoutStyleClass ui-layout-unit-content" position="north" size="200" resizable="true" closable="true" collapsible="true" border="0">
<h:form style="margin: 0; border:0" ><h1 style="color: white"><br>ubezpieczenia</br></h1></h:form>
</p:layoutUnit>
<p:layoutUnit position="south" size="100" closable="true" collapsible="true">
Zapraszamy do odwiedzania naszego biura!
</p:layoutUnit>
<p:layoutUnit style="background-color: white;color: white" position="west" size="175" header="Menu" collapsible="true">
<h:form style="background-color: white">
<p:menu style="background-color: white;border-width: 0" >
<f:ajax render=":content">
<p:menuitem value="O naszej agencji" action="#{helloBean.setName('/main_pages/onas.xhtml')}" update=":content" />
<p:menuitem value="Ubezpieczenia pojazdów" action="#{helloBean.setName('/main_pages/ubpoj.xhtml')}" update=":content" />
<p:menuitem value="Ubezpieczenia majątkowe" action="#{helloBean.setName('/main_pages/ubmaj.xhtml')}" update=":content" />
<p:menuitem value="Ubezpieczenia na życie" action="#{helloBean.setName('/main_pages/ubnaz.xhtml')}" update=":content" />
<p:menuitem value="Zapytaj" action="#{helloBean.setName('/main_pages/zapytaj.xhtml')}" update=":content" />
<p:menuitem value="Kontakt" action="#{helloBean.setName('/main_pages/kontakt.xhtml')}" update=":content" />
</f:ajax>
</p:menu>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="center">
<br></br><br></br>
<p:panel id="content">
<ui:include src="#{helloBean.name}" />
</p:panel>
</p:layoutUnit>
</p:layout>
</h:body>
</html>