Spring JavaMailSenderImpl不支持SSL

时间:2013-05-22 04:43:07

标签: spring ssl javamail

简单的Java Mail客户端与在SSL上运行的电子邮件服务器一起正常工作。但是从Spring JavaMailSenderImpl类使用时,同一服务器无法正常工作。 thunderbird也可以正常使用邮件服务器。但是,春天有问题。

这是原始的Java代码,可以正常工作。

    final String username = "id@server.com";
    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("id@server.com"));

        message.setRecipients(  Message.RecipientType.TO, 
                                InternetAddress.parse("id@gmail.com"));

        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);
    }

这是包含电子邮件配置的applicationContext.xml,

    <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="id@server.com" />
        <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>

配置Spring JavaMailSenderImpl的正确方法是什么?

任何帮助都将深表感谢。

1 个答案:

答案 0 :(得分:4)

配置正确。它工作正常。你能检查一下用户名/密码吗?