12

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

这是运行良好的原始 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 的正确方法是什么?

任何帮助将不胜感激。

4

1 回答 1

5

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

于 2013-05-22T04:49:40.223 回答