Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JavaMailSenderImpl is not working with SSL [closed]

Simple Java Mail client is working fine with an email server running on SSL. But the same server is not working when used from Spring JavaMailSenderImpl class. Also thunderbird works fine the mail server. But, spring has issues.

Here is the original Java Code that works fine.

    final String username = "[email protected]";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtps.ssl.checkserveridentity", "true");
    props.put("mail.smtps.ssl.trust", "*");
    props.put("mail.smtp.host", "server.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress("[email protected]"));

        message.setRecipients(  Message.RecipientType.TO, 
                                InternetAddress.parse("[email protected]"));

        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler, \n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

here is the applicationContext.xml that contains the email configuration,

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="javaMailProperties">
            <props>
                    <prop key="mail.smtp.starttls.enable">true</prop>
                    <prop key="mail.smtp.auth">true</prop>
                    <prop key="mail.smtps.ssl.checkserveridentity">true</prop>
                    <prop key="mail.smtps.ssl.trust">*</prop>
            </props>
        </property>
        <property name="username"  value="[email protected]" />
        <property name="password"  value="password" />
        <property name="port"><value>587</value></property>
        <property name="protocol"><value>smtp</value></property>
        <property name="host" value="server.com"/>          
</bean>

What is the right way to configure Spring JavaMailSenderImpl ?

Any help would be deeply appreciated.

like image 933
Nambi Avatar asked May 22 '13 04:05

Nambi


1 Answers

The configuration is correct. It works fine. Can you check the userid / password ?

like image 196
Nambi Sankaran Avatar answered Oct 18 '22 01:10

Nambi Sankaran