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)