简体   繁体   English

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

[英]Unable to Send Mail - javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

We are sending Mail using Spring JavaMailSenderImpl .我们使用 Spring JavaMailSenderImpl发送邮件。 Following is the configuration以下是配置

 <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>

Properties File :-属性文件:-

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

Console Logs控制台日志

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

We are convinced that this is not related to the SSL certificate as there are other web applications deployed in the same server which sends email perfectly with the same configuration.我们确信这与 SSL 证书无关,因为在同一服务器中部署了其他 Web 应用程序,它们使用相同的配置完美发送电子邮件。 What could be the issue here ?这可能是什么问题?

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

You want either mail.smtp.ssl.enable for implicit SSL directly after TCP connect (port 465) or mail.smtp.starttls.enable for explicit SSL using the STARTTLS command (port 25).您希望在 TCP 连接(端口 465)后直接使用mail.smtp.ssl.enable进行隐式 SSL,或者使用 STARTTLS 命令(端口 25)使用显式 SSL 使用mail.smtp.starttls.enable But with your current properties you set both to true.但是使用您当前的属性,您将两者都设置为 true。

This means it will do a TCP connect to port 25 and try a SSL handshake there.这意味着它将 TCP 连接到端口 25 并在那里尝试 SSL 握手。 This will fail because the server is sending a plain text greeting from the SMTP dialog and not the expected SSL handshake.这将失败,因为服务器正在从 SMTP 对话框发送纯文本问候语,而不是预期的 SSL 握手。 Thus you get因此你得到

Unrecognized SSL message, plaintext connection?无法识别的 SSL 消息,纯文本连接?

To fix it make sure that you either use implicit or explicit SSL but not both depending on the port, ie for port 25 mail.smtp.ssl.enable should be false.要修复它,请确保您使用隐式或显式 SSL,但不能同时使用取决于端口,即端口 25 mail.smtp.ssl.enable应为 false。

I tried to resolve this by many methods but finally found this working by myself.我尝试通过多种方法解决此问题,但最终发现这是我自己的工作。

  1. Make sure you are able to send emails from Terminal or by any other mail client before creating code.在创建代码之前,请确保您能够从终端或任何其他邮件客户端发送电子邮件。 Once your have tested mail is working fine then use below properties and code.一旦您测试过邮件工作正常,然后使用以下属性和代码。 I have deployed the Code in the Email server so I don't require SSL or TLS.我已经在电子邮件服务器中部署了代码,所以我不需要 SSL 或 TLS。 I am using port 25 for Relay.我正在使用端口 25 进行中继。

  2. Required Properties -所需属性 -

     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>
  3. Required Code -所需代码 -

     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"; }
  4. Required Imports -所需的进口 -

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

问题出现在 javax.mail 1.4.0 中,升级到 1.4.7 解决了我的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jmeter 侦听器显示“javax.net.ssl.SSLException:无法识别的 SSL 消息,纯文本连接?” - Jmeter listener showing “ javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?” 随机出现的javax.net.ssl.SSLException:无法识别的SSL消息,明文连接? - Random occurence of javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? 获取源网页的源代码,javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? - get source web page's source code, javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? spotify/dockerfile-maven 插件:无法构建映像:javax.net.ssl.SSLException:无法识别的 SSL 消息,纯文本连接 - spotify/dockerfile-maven plugin: Could not build image: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection 连接到IMAP javax.net.ssl.SSLException:无法识别的SSL消息 - Connecting to IMAP javax.net.ssl.SSLException: Unrecognized SSL message javax.net.ssl.SSLException:不支持或无法识别的 SSL 消息 - javax.net.ssl.SSLException: Unsupported or unrecognized SSL message javax.net.ssl.SSLException:连接重置 - javax.net.ssl.SSLException: Connection reset 获取不受支持或无法识别的 SSL 消息; 调用外部 API 时,嵌套异常是 javax.net.ssl.SSLException - Getting Unsupported or unrecognized SSL message; nested exception is javax.net.ssl.SSLException while calling external API 客户端无法与 WireMock 通信 HTTPS:javax.net.ssl.SSLException:不支持或无法识别的 SSL 消息 - client can't talk HTTPS to WireMock: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message 使用 JavaMail 发送邮件时出现 javax.net.ssl.SSLException - javax.net.ssl.SSLException when sending mail using JavaMail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM