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.