Chathruaka Waas
Chathruaka Waas

Reputation: 341

Email sending hangs in Spring with Java Mail

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?

Upvotes: 1

Views: 5406

Answers (2)

sidgate
sidgate

Reputation: 15234

Though the issue is already resolved, I faced similar issue, and none of the steps mentioned above and on other threads worked. I had to additionally specify the protocol as SMTPS for this to work. So here is my working code snippet.

    @Bean
  JavaMailSender javaMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost(props.getHost());
    mailSender.setPort(props.getPort());
    mailSender.setUsername(props.getUsername());
    mailSender.setPassword(props.getPassword());

    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.auth", props.getSmtp().isAuth());
    mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    mailProperties.put("mail.smtp.starttls.enable", props.getSmtp().isStarttlsEnable());
    mailSender.setJavaMailProperties(mailProperties);
    mailSender.setProtocol("smtps");

    return mailSender;

  }

Upvotes: 1

Alboz
Alboz

Reputation: 1851

Follow these instructions to send an email with Spring and Gmail: Email with Spring and Gmail

You're completely missing the authentication part. And make sure the Gmail smtp server address is correct.

Taken from the above link these must be the configurations;

<property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="username" />
    <property name="password" value="password" />

    <property name="javaMailProperties">
       <props>
              <prop key="mail.smtp.auth">true</prop>
              <prop key="mail.smtp.starttls.enable">true</prop>
           </props>
    </property>

Upvotes: 0

Related Questions