0

Hi its my first time sending emails using a web application so I am a bit stuck. I have configured my javamail session on glassfish 4.0. I will post the configurations below. I am trying to send an email by passing parameters to my managed bean. The sendemail() method executes but no email is actually being sent. No error is printed either. I am using netbeans 8.0.

Here is my managed bean.

@Named("emailNotificationBean")
@SessionScoped
public class emailNotificationBean implements Serializable{

@Resource(name="mail/teamjla2")
private javax.mail.Session mailSession;

private String to;
private String subjectA;
private String texts;


public emailNotificationBean(){

}






public void sendEmail(){


Message msg = new MimeMessage(mailSession);
try {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subjectA);

msg.setContent(texts, "text/html");
Transport.send(msg);
} catch (MessagingException me) {


}


System.out.println("Sent message successfully....");
addSuccessMessage("Your information has been sent");

}

private static void addSuccessMessage(String msg) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
}   


public Session getMailSession() {
return mailSession;
}

public void setMailSession(Session mailSession) {
this.mailSession = mailSession;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public String getSubjectA() {
return subjectA;
}

public void setSubjectA(String subjectA) {
this.subjectA = subjectA;
}

public String getTexts() {
return texts;
}

public void setTexts(String texts) {
this.texts = texts;
}

Here is my xhtml page:

    <h:outputText value="to:"></h:outputText>
    <h:inputText id="to" value="#{emailNotificationBean.to}"/>
    <h:outputText value="subjectA"/>
    <h:inputText  id="subjectA" value="#{emailNotificationBean.subjectA}"/>            
    <h:outputText value="texts"></h:outputText>
    <h:inputText  id="texts" value="#{emailNotificationBean.texts}" ></h:inputText>




    <h:commandButton value="send" action="#{emailNotificationBean.sendEmail}"/>

Here are my glassfish javamail session configuraions:

mail host:smtp.gmail.com

default user: username

sender address: username@email.com

mail.smtp.auth:true

mail.smtp.password:passwordd

mail.smtp.port:465

mail.smtp.socketFactory.port:465

mail.smtp.socketFactory.fallback:false

mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory

Any help will be appreciated cause I cant seem to find what could be possibly wrong. Thanks in advance.

ok I added a printStackTrace(); in my catch clause and i got the following output.

Severe:   javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1963)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:367)
    at javax.mail.Service.connect(Service.java:226)
    at javax.mail.Service.connect(Service.java:175)
    at javax.mail.Transport.send0(Transport.java:253)
    at javax.mail.Transport.send(Transport.java:124)

Got it working just changed the glassfish settings using this.

props.put("mail.smtp.host", "smtp.gmail.com");
 props.put("mail.smtp.socketFactory.port", "587");
 props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
 props.put("mail.smtp.auth", "true");
 props.put("mail.smtp.port", "587");
 props.put("mail.smtp.ssl.enable", "false");
 props.put("mail.smtp.starttls.enable", "true");
 props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

0 Answers0