1

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?

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
Chathruaka Waas
  • 341
  • 1
  • 5
  • 23
  • Is your mail session correctly configured in JBoss? Is the SMTP server up and running, can you actually connect to the server from JBoss. Looks like a mail session configuration problem to me. – M. Deinum Sep 26 '14 at 08:48
  • i'm using gmail smtp. and i can telnet and connect to it. this is what i used : telent smtp.gmail.com 465 – Chathruaka Waas Sep 26 '14 at 08:50
  • this is my jboss configuration : – Chathruaka Waas Sep 26 '14 at 08:52
  • after 10 mins of hanging it throws the exception : Caused by: 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 – Chathruaka Waas Sep 26 '14 at 08:56
  • the expection clearly tells that the smtp is refusing the connection at the port. I hope you should test it at port 587 – Ekansh Rastogi Sep 26 '14 at 09:03
  • A similar question has been answered at http://stackoverflow.com/questions/15597616/sending-email-via-gmail-smtp-server-in-java – Ekansh Rastogi Sep 26 '14 at 09:07
  • Hi thanks for the info guys. i tried going through them but still couldnt get my issue sorted. as one has suggested in a post i added the session.debut(true). and i got this error. javax.mail.MessagingException: No MimeMessage content. i tried using the SimpleMailMessage and sending the email but the same result. – Chathruaka Waas Sep 26 '14 at 10:45
  • Guys got it sorted my silly mistake i have being trying to sent the email with a null content. Thanks for all the help. – Chathruaka Waas Sep 26 '14 at 10:51

2 Answers2

1

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;

  }
sidgate
  • 14,650
  • 11
  • 68
  • 119
0

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>
Alboz
  • 1,833
  • 20
  • 29