0

I have tried to troubleshoot this issue a lot and now as a last resort posting it here. Please help me!

Issue: I am able to send mail by calling the class from a main method. When I try to call the same class from a Struts2 Action class it does not send an email, rather gives the below error. I am using Jboss EAP 7.0.0.Alpha1. I think the issue might be lying in the JBoss configuration. I have also changed the Standalone-full.xml file.(To note I am using standalone-full.xml and my other web components are working fine). I did run it in debug mode and saw all values are getting populated. When we run it from the struts2 action class it does not send the message.

Changes made to Standalone-full.xml:

<mail-session name="java:jboss/mail/Default" 
        from="somedummyuser@gmail.com" jndi-       
        name="java:jboss/mail/Default">

           <smtp-server password="******" username="somedummyuser" 
                 ssl="true" outbound-socket-binding-ref="mail-smtp"/>
        </mail-session>
    </subsystem>

     <outbound-socket-binding name="mail-smtp">
        <remote-destination host="smtp.gmail.com" port="465"/>
     </outbound-socket-binding>

Java Code:

  public class SendEmail {  

       private MailSender mailSender;

       public void setMailSender(MailSender mailSender) {  
       this.mailSender = mailSender;  
      }  

       public void sendMail(String from, String to, String subject, String   
       msg) 
       throws Exception,NamingException{  
      //creating message  
      SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); 
      simpleMailMessage.setFrom(from);  
      simpleMailMessage.setTo(to);  
      simpleMailMessage.setSubject(subject);  
      simpleMailMessage.setText(msg);  
      //sending message  
      System.out.println("SimpleMailMessage object:"+simpleMailMessage);
      mailSender.send(simpleMailMessage);     
    }  

 }

Class with MainMethod Which is executing the above code findE and sending the email successfully:

public class MainMethod_SendEmail {

 public static void main(String[] args) throws Exception{  

    Resource r=new ClassPathResource("webApplicationContext.xml");  
    BeanFactory b=new XmlBeanFactory(r);  
    SendEmail m=(SendEmail)b.getBean("sendEmail");  

    String sender = AccessPropertiesUtil.getInstance().getProperty("from");
    String receiver = AccessPropertiesUtil.getInstance().getProperty("to");
    String subject =    
     AccessPropertiesUtil.getInstance().getProperty("subject");
    String message = 
    AccessPropertiesUtil.getInstance().getProperty("message");

    m.sendMail(sender, receiver, subject, message);

    System.out.println("success");  
    }  

}

But the same mail sending code is not getting executed from a Struts2 Action class: Struts2Action Class:

     public String execute()
    {
     emailSending();
    }

     private void emailSending() throws Exception
    {
        System.out.println("Sending Email");
        String sender = 
         AccessPropertiesUtil.getInstance().getProperty("from");
        String receiver = 
         AccessPropertiesUtil.getInstance().getProperty("to");
        String subject = 
         AccessPropertiesUtil.getInstance().getProperty("subject");
        String message = 
         AccessPropertiesUtil.getInstance().getProperty("message");

        sendEmail.sendMail(sender, receiver, subject, message);

        }




Error Trace:
     org.springframework.mail.MailSendException: Mail server connection 
    failed;     
    nested exception is javax.mail.MessagingException: Could not  
    connect to SMTP host: smtp.gmail.com, port: 465, response: -1. Failed 
    messages:   javax.mail.MessagingException: Could not connect to SMTP 
    host: smtp.gmail.com, port: 465, response: -1; message exception details 
    (1) are:
    2016-07-13 02:45:33 ERROR stderr:71 - Failed message 1:
    2016-07-13 02:45:33 ERROR stderr:71 - javax.mail.MessagingException: 
    Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
    2016-07-13 02:45:33 ERROR stderr:71 -   at 
    com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2042)
    2016-07-13 02:45:33 ERROR stderr:71 -   at   
    com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
    2016-07-13 02:45:33 ERROR stderr:71 -   at  
    javax.mail.Service.connect(Service.java:364)
  • The JavaMail FAQ has [tips for debugging connection problems](http://www.oracle.com/technetwork/java/javamail/faq/index.html#condebug). Is there a firewall on your machine or network that's preventing the connection? Are you running your "main" program on the same machine that you're running Jboss on? – Bill Shannon Jul 13 '16 at 18:56
  • You should consider downloading the GA version instead of Alpha1. You can download EAP 7 GA from [here](http://www.jboss.org/products/eap/download/) for development use. – CoolBeans Jul 13 '16 at 23:00
  • @BillShannon -- Thanks for replying. Yes I am running the main program from the same machine, infact from the same package. My Sendmail class is same , when I try to call the class from a Struts2 action class it does not send the email, whereas when I call it from a main class it sends. – Shubham Roy Choudhury Jul 14 '16 at 00:23
  • Can someone help me to open a port in Jboss EAP 7.0.0.Alpha1 for any outgoing connections. I think this will solve my problem. As the code works fine on Tomcat but on JBoss. I would like to open port 465 on my JBoss server so that it can make connection to the Gmail's SMTP server. – Shubham Roy Choudhury Jul 14 '16 at 01:34
  • Is Jboss running with a [Java security manager](http://www.oracle.com/technetwork/java/javamail/faq/index.html#securityManager) enabled? – Bill Shannon Jul 14 '16 at 06:24

0 Answers0