My index.xhtml page contain this and more other little thing like footer, header,menu etc... :
index.html contain :
<h:head/>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit position="center" >
<h:panelGroup id="content" layout="block">
<h:form id="contentform">
<!-- Cas page contact == page affichée lors du clic sur contact -->
<h:form id="contact">
<h:panelGroup rendered="#{navBean.page == 'contact'}">
<ui:include src="contact.xhtml" />
</h:panelGroup>
</h:form>
</h:form>
<!--Fin de gestion des cas possible -->
</h:panelGroup>
</p:layoutUnit>
<!-- Fin layout central -->
</p:layout>
</h:body>
I'm using JSF 2.1 and PrimeFaces. I have the following command button on contact.xhtml
:
<h:head/>
<h:body>
<p:commandButton value="Envoie la sauce" action="#{mail.sendEmail()}" >
<f:ajax execute="@form" render="@form" />
</p:commandButton>
</h:body>
It refers to the following bean action:
@ManagedBean(name="mail")
@SessionScoped
public class MailBean {
public void sendEmail() throws IOException {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("toto@yopmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("toto@gmail.com"));
message.setSubject("Demande de Contact");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
//to see if message was send
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
This is supposed to send a mail via JavaMail, however the button doesn't do anything except refreshing the page.
I have tested the SendHTMLEmail class as a stand alone Java Application, it works perfectly.
What i'm trying to do is a simple contact form made in xhtml using JSF2.1.
UPDATE-
Ok some news today !
If i use this thing : <p:commandButton action="#{mail.send()}"/>
on the page index.xhtml something happen i have to determine why the class can't be find... :
<?xml version='1.0' encoding='UTF-8'?>
<partial-response><error><error-name>class javax.faces.el.EvaluationException</error-name><error-message><![CDATA[/index.xhtml @35,47 action="#{mail.send()}": Target Unreachable, identifier 'mail' resolved to null]]></error-message></error></partial-response>
BUT on the contact.xhtml i have only this with the same button :
<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="log:messages"><![CDATA[<div id="log:messages" class="ui-messages ui-widget" aria-live="polite"></div>]]></update><update id="javax.faces.ViewState"><![CDATA[-2283025416922274948:-229809047557820449]]></update></changes></partial-response>
So all my have an unique id ! Someone have an idea to what happen ? edit1 : full code added edit2 : added the index.html with all balises edit3 : Discovered something about the action button into my xhtml page