0

I want to send email from JSF application. I created simple Java SE application and method to send email, it works. So I moved this code to Managed Bean, but in JSF application this doesn't work. I am new to Java, I tried to debug it but though debugging is started and it says that site is on localhost:8080 the site doesn't appear, so I only can Run project and click my button to verify if email is received on inbox.

I am on Ubuntu 12.10, with NetBeans. And since it works perfect in other project (Java Se app) I thought maybe I have to trigger this sending different way in JSP web app? Why it doesn't work from ManagedBean?

Here is my class with copied method (this method works in Java Se app):

import javax.mail.Address;
import javax.mail.*;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.mail.DefaultAuthenticator;


public class myEmailSend {
    public void sendMail(){

        Email email = new SimpleEmail();
        try {
            String authuser = "me@gmail.com";
            String authpwd = "password";
            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 mail3");
            email.addTo("someone@gmail.com", "ToName");
            //email.setStartTLSRequired(false);
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}

I have this bean. I tried both: creating myEmailSend object and using its method, and send directly via sendMail2(): none of these works:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
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
@RequestScoped
public class mySendBean {

    private myEmailSend mE;
    /**
     * Creates a new instance of mySendBean
     */
    public mySendBean() {
        mE=new myEmailSend();
    }

    public void sendMail(){
        mE.sendMail();
    }
    public void sendMail2(){


        Email email = new SimpleEmail();
        try {
            String authuser = "me@gmail.com";
            String authpwd = "password";
            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 mail3");
            email.addTo("someone@gmail.com", "ToName");
            //email.setStartTLSRequired(false);
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}

and try to trigger it from button on site:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

    <body>
        <div>zapytaj <br></br>TODO write content<br></br></div>
        <h:form>
            <h:commandButton id="comand1" value="sendEmail" action="#{mySendBean.sendMail2()}" />
        </h:form>
    </body>
</html>

something is happening cause I see: "waiting for localhost..." in Firefox but still no mail.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • 1
    The code to send the email will work fine. To begin with, remove the email sending code from the managed bean constructor (you don't really want to send an email on every request where the bean is created). Second, make sure your web application server (or your project) has the Java Mail API just in order to send the emails. After all this, the only problem may be that an external agent is blocking the communication (probably your antivirus and a firewall). Try to stop them just to test the functionality. – Luiggi Mendoza Apr 09 '13 at 17:16
  • @LuiggiMendoza thank you very much. I develop and run both applications from same Netbans on same machine. JSF is using Glassfish, Java Se app is using glassfish too? – 4pie0 Apr 09 '13 at 17:33
  • No, GlassFish is using Java SE. – Luiggi Mendoza Apr 09 '13 at 17:34
  • @LuiggiMendoza but this is .xhtml file. doesn't it have to be servlet? – 4pie0 Apr 09 '13 at 18:00
  • Please go through a sane tutorial like the ones proposed in [StackOverflow JSF wiki](http://stackoverflow.com/tags/jsf/info). I heavily recommend you reading BalusC and Mkyong tutorials on this matter. – Luiggi Mendoza Apr 09 '13 at 20:31
  • @LuiggiMendoza thank you, meanwhile I've found something: I can simply put text to Bean and retrieve it on click in page, but if I have this page inside, in center layout then the button is not refreshed on click: can it be? – 4pie0 Apr 09 '13 at 21:31
  • Last is a totally different question than the one stated in your original post (and sorry but I don't understand what you mean with *if I have this page inside, in center layout then the button is not refreshed on click: can it be?* you mean an `iframe`, an include, a page fragment...?). – Luiggi Mendoza Apr 09 '13 at 21:33
  • @LuiggiMendoza yes, now I see this is indeed this issue though still not know how to handle it. When I just Run File page opens, Bean is updated on request, email sent, but when this is included dynamically from menu and appears in center of the main page, then this doesn't work, there is even no box, I don't know why. I will make another question. – 4pie0 Apr 09 '13 at 22:05
  • Sure, I'll wait for it and see if I can provide you an answer :). – Luiggi Mendoza Apr 09 '13 at 22:09

1 Answers1

0

yes, you can : p I just had to use jsf templating

4pie0
  • 29,204
  • 9
  • 82
  • 118
  • What is jsf templating? – Koray Tugay Oct 06 '13 at 09:47
  • http://stackoverflow.com/questions/4792862/how-to-include-another-xhtml-in-xhtml-using-jsf-2-0-facelets and http://stackoverflow.com/questions/15993522/using-templates-in-primefaces-jsf – 4pie0 Oct 06 '13 at 13:22