spring 无法发送邮件- javax.net.ssl,SSLException:无法识别的SSL消息,纯文本连接?

im9ewurl  于 12个月前  发布在  Spring
关注(0)|答案(4)|浏览(152)

我们使用SpringJavaMailSenderImpl发送邮件,配置如下

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${host}"/>
        <property name="port" value="${port}"/>
        <property name="username" value="${mail.username}"/>
        <property name="password" value="${mail.password}"/>
        <property name="javaMailProperties">
            <props>
                <!-- Use SMTP transport protocol -->
                <prop key="mail.transport.protocol" >${mail.transport.protocol}</prop>
                <!-- Use SMTP-AUTH to authenticate to SMTP server -->
                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                <!-- Use TLS to encrypt communication with SMTP server -->
                <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
                <prop key="mail.debug">false</prop>
                <prop key="mail.smtp.ssl.enable">true</prop>
            </props>
        </property>
    </bean>

字符串

属性文件:-

host=XXXX.XXXX.XX
port=25
mail.username=xxxxxxxx
mail.password=xxxxxxx
mail.transport.protocol=smtp
mail.smtp.auth=true
mail.smtp.starttls.enable=true

控制台接口

Exception in thread "taskExecutor-2" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: XXXX.XXXX.XX, port: 25;
      nested exception is:
            javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: XXXX.XXXX.XX, port: 25;
      nested exception is:
            javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?; message exception details (1) are:
    Failed message 1:
    javax.mail.MessagingException: Could not connect to SMTP host: XXXX.XXXX.XX, port: 25;
      nested exception is:
            javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
            at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
            at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
            at javax.mail.Service.connect(Service.java:295)
            at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
            at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
            at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:336)
            at com.XXXX.Mailer$1.run(Mailer.java:52)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at java.lang.Thread.run(Thread.java:744)
    Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
            at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:671)
            at sun.security.ssl.InputRecord.read(InputRecord.java:504)
            at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
            at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
            at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
            at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
            at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:507)
            at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
            at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
            ... 9 more


我们确信这与SSL证书无关,因为在同一台服务器上部署了其他Web应用程序,这些应用程序使用相同的配置完美地发送电子邮件。

i5desfxk

i5desfxk1#

<prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
<prop key="mail.smtp.ssl.enable">true</prop>

字符串
您希望mail.smtp.ssl.enable在TCP连接(端口465)后直接用于隐式SSL,或者mail.smtp.starttls.enable使用STARTTLS命令(端口25)用于显式SSL。但是使用当前属性,您将两者都设置为true。
这意味着它将执行TCP连接到端口25并尝试SSL握手。这将失败,因为服务器正在从SMTP对话框发送纯文本问候,而不是预期的SSL握手。因此,您将获得
无法识别的SSL消息,纯文本连接?
要修复它,请确保使用隐式或显式SSL,但不能同时使用,这取决于端口,即对于端口25,mail.smtp.ssl.enable应该为false。

up9lanfz

up9lanfz2#

问题发生在javax.mail1.4.0中,升级到1.4.7解决了我的问题

4si2a6ki

4si2a6ki3#

截至2023年11月2日,通过Amazon ses发送电子邮件时,以下值有效:

mail.host=email-smtp.us-west-2.amazonaws.com
mail.user=****
mail.user.password=***
[email protected]

mail.debug=false

mail.smtp.timeout=60000
mail.smtp.connectiontimeout=60000
mail.smtp.auth=true
mail.transport.protocol=smtps
mail.smtp.host=email-smtp.us-west-2.amazonaws.com
mail.smtp.port=587
mail.smtp.starttls.enable=true

#JUST REMOVE THE FOLLOWING PROPERTIES 
#mail.smtp.ssl.protocols=TLSv1.2
#mail.smtp.ssl.trust=smtpserver
#mail.transport.protocol.rfc822=smtps
#mail.smtp.user=***
#mail.smtp.password=***
#mail.smtp.socketFactory.port=587
#mail.smtp.socketFactory.class=javax.net.SocketFactory
#mail.smtps.auth=true
#mail.smtp.ssl.enable=true

字符串

vxqlmq5t

vxqlmq5t4#

我尝试了很多方法来解决这个问题,但最终发现自己的工作。
1.在创建代码之前,请确保您能够从终端或任何其他邮件客户端发送电子邮件。一旦您测试过的邮件工作正常,请使用下面的属性和代码。我已经在电子邮件服务器中部署了代码,所以我不需要SSL或TLS。我使用端口25进行中继。
1.所需属性-

mail.smtp.host=<myhostname.com>
mail.smtp.port=25
mail.smtp.auth=true
mail.transport.protocol=smtp
smtpd_recipient_restrictions=permit_sasl_authenticated
user=<sender_username/email>
password=<password>
mail.smtp.username=<sender_username/email>
mail.smtp.password=<password>

字符串
1.所需代码-

public String sendEmail(String email){

         //Compose the message  
          try {  

                FileReader reader=new FileReader("smtp.properties");  
             //Get the session object  
                Properties props = new Properties(); 
                props.load(reader); 

                System.out.println(" Props :: " + props.toString());
                String to = email;
                String host=props.getProperty("mail.smtp.host");
                final String user=props.getProperty("user");
                final String password=props.getProperty("password");

               Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, password);
                    }
                }); 

                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(user));
                message.setRecipients(
                        Message.RecipientType.TO,
                        InternetAddress.parse(to)
                );

                message.setSubject("Testing - SMTP");
                message.setText("Dear User, \n\n Hello !");

                Transport.send(message);

                System.out.println("Done");

               } catch (MessagingException e) {
                    e.printStackTrace();
               } catch (IOException ex) {  
                    ex.printStackTrace();
               }
          return "SUCCESS";
     }


1.所需进口-

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

相关问题