Search code examples
javadependency-injectionsmtpjakarta-mailwildfly

Wildfly javamail and mailsession injection Failure sending HELO command to SMTP server


So i need some clarification. I am trying to send email using google SMTP server, using my google apps acount, (the free thing, not the relay). I am using a wildfly 10 container (and JSF), so I do not have a javamail jar in my project(is that correct?). So in my stateless EJB, i do a resource injection:

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

and in my standalone.xml:

<subsystem xmlns="urn:jboss:domain:mail:2.0">
        <mail-session name="mail" jndi-name="java:/mail"/>
</subsystem>

So i am assuming I don't have to define the port,username, and password in the standalone, if i just add the propeties like this:

Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", "XXXX@gmail.com");
    props.put("mail.smtp.password", "XXXX");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.auth", "true");

    mailSession = Session.getDefaultInstance(props, 
            new javax.mail.Authenticator(){
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                        "XXXX@gmail.com", "XXXX");// Specify the Username and the PassWord
                }
        });  

Is this correct, or does it need to be also defined in the standalone.xml?

So i get the error: javax.mail.MessagingException: Failure sending HELO command to SMTP server

Any help on how to get this working is appreciated!

Here is the full code:

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.faces.component.UIInput;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

//import view.Info;
@Stateless
public class EmailEJB implements EmailBeanInterface {


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

@PersistenceContext(unitName = "EmailEJB")
EntityManager em;

String amountToDisplay = null;
List<Info> emailList;



public void setAmountToDisplay(String amountToDisplay) {
    this.amountToDisplay = amountToDisplay;
}

@PostConstruct
public void defaultList() {
    String sqlPrepareStmt = "SELECT e FROM Info e WHERE id <=" + amountToDisplay;
    emailList = (List<Info>) em.createQuery(sqlPrepareStmt, Info.class).getResultList();
}

public List<Info> getEmailList() {
    return emailList;
}

public void send(ArrayList<String> managedList) {

    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "false");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", "XXXX@gmail.com");
    props.put("mail.smtp.password", "XXXX");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.auth", "true");
    //Session mailSession = Session.getDefaultInstance(props);
   // mailSession= Session.getDefaultInstance(props);
    mailSession = Session.getDefaultInstance(props, 
            new javax.mail.Authenticator(){
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                        "XXXX", "XXXX");// Specify the Username and the PassWord
                }
        });
    try {
        MimeMessage m = new MimeMessage(mailSession);
        Address from = new InternetAddress("XXXX@gmail.com");
        Address[] to;
        to = new Address[managedList.size()];
        for (int i = 0; i < managedList.size(); i++) {
            String emailString =  managedList.get(i);
            if(emailString.isEmpty() == true){
                System.out.println("emailString is empty");
            }
            else{
            to[i] = new InternetAddress(java.net.IDN.toASCII(emailString));
            }
        }

        m.setRecipients(Message.RecipientType.TO, to);
        m.setSubject("Wildfly 10 - Checkbox test");
        m.setSentDate(new java.util.Date());
        BodyPart messageBodyPart = new MimeBodyPart();

        // Fill the message
        messageBodyPart.setText("testing application managed javamail props");

        // Create a multipart message
        Multipart multipart = new MimeMultipart();

        // Set text message part
        multipart.addBodyPart(messageBodyPart);
        m.setContent(multipart);
        Transport transport = mailSession.getTransport("smtp");
        transport.connect(host, "XXXX@gmail.com", "XXXX");
        transport.send(m);
        transport.close();
        System.out.println("Mail sent!");


    } catch (javax.mail.MessagingException e) {
            e.printStackTrace();
    }
}

}

This is also part of the stack trace, could it be a problem with geronimo? Can i use sun instead, if so how do i do that?

23:23:23,215 ERROR [stderr] (default task-5) at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.sendHelo(SMTPTransport.java:1914)

23:23:23,215 ERROR [stderr] (default task-5) at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.sendHandshake(SMTPTransport.java:1829)

23:23:23,216 ERROR [stderr] (default task-5) at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:393)


Solution

  • Well I think is was missing an SSL Socket Factory in my props, but i switched to using TLS now and it works! Here is a link: http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ Really I was just over complicating it.