I'm trying to send a simple mail using spring.
Here is my mail sender bean definition in java configuration.
@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setSession(getMailSession());
return mailSender;
}
public Session getMailSession() {
JndiTemplate template = new JndiTemplate();
Session session = null;
try {
session = (Session) template.lookup("java:jboss/mail/Default");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return session;
}
I'm running is jboss wildfly and the beans are created without any issue.
Here is my code to send the email.
@Autowired
private JavaMailSender mailSender;
@Override
public void sendMail(String mailTo, String subject, String content) throws MessagingException{
MimeMessage message = mailSender.createMimeMessage();
message.setSubject(subject);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo,false));
mailSender.send(message);
}
the JavaMailSender is injected correctly. when I debug the execution happens till mailsender.send()
method. and it starts to hang.
It seems all the configurations in the jboss is correct. I also tried specifying the mail server parameters in the bean it self. but still it's not working.
What am I doing wrong here?