1.发送MIME邮件遇到的错误
JavaMail Exception: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(InputRecord.java:523) at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:355) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:789) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1112) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1139) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1123) at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672) ... 19 more
解决方法:
配置JavaMail的Properties时,不要指定“mail.smtp.socketFactory.class”,因为TLS使用的是普通的Socket。
然后指定属性“mail.smtp.starttls.enable”为“true”。
代码如下:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.qq.com"/>
<property name="port" value="25"/>
<property name="username" value="[email protected]"/>
<property name="password" value="aa1111111"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<!-- <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
-->
</props>
</property>
</bean>
2.发送纯文本邮件