简单的Java邮件客户端与在SSL上运行的电子邮件服务器正常工作。但是从Spring JavaMailSenderImpl类使用相同的服务器时不起作用。此外,thunderbird工作正常的邮件服务器。但是,春天有问题。Spring JavaMailSenderImpl不能与SSL一起工作
这是原始的Java代码,工作正常。
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);
}
这里是包含电子邮件配置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="[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>
什么是配置Spring JavaMailSenderImpl的正确方法?
任何帮助将深表谢意。
只是作为如何配置JavaMailSenderImpl以使用smtps和STARTTLS的示例,这实际上非常有用。它弥补了官方文件中的缺陷。 – Jimadilo