簡單的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