【问题标题】:"Exception reading response" error trying to send mail in java尝试在 java 中发送邮件时出现“异常读取响应”错误
【发布时间】:2020-03-18 07:02:05
【问题描述】:

这是一个从 java 发送电子邮件的小代码。

public static void sendMail(){
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", "mail.domain.tld");
    props.put("mail.smtp.debug", "true");
            Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("sender@domain.tdl", "pwd");
        }
      });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("sender@domain.tdl"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("receiver@yahoo.fr"));
        message.setSubject("Testing Subject");
        message.setContent("This is a test <b>HOWTO<b>", "text/html; charset=ISO-8859-1");
        Transport.send(message);
        System.out.println("Done");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

但是运行它我有这个错误

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketException: Software caused connection abort: recv failed
    at Test.main(Test.java:112)
Caused by: javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketException: Software caused connection abort: recv failed
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2445)
    at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:2307)
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2340)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1808)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1285)
    at javax.mail.Transport.send0(Transport.java:255)
    at javax.mail.Transport.send(Transport.java:124)
    at Test.main(Test.java:107)
Caused by: java.net.SocketException: Software caused connection abort: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:126)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:104)
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2425)
    ... 7 more

我不明白错误的原因,因为这段代码过去对我有用。 这是我正在使用的罐子:

  • dsn-1.6.0
  • gimap-1.6.0
  • imap-1.6.0
  • javax.mail-1.6.0
  • javax.mail-api-1.6.0
  • logging-mailhandler-1.6.0
  • mailapi-1.6.0
  • pop3-1.6.0
  • smtp-1.6.0

@Bill 要求修改后,这是我遇到的新错误

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: mail.hereka.org, port: 25;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at Test.main(Test.java:134)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: mail.hereka.org, port: 25;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2196)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:726)
    at javax.mail.Service.connect(Service.java:366)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:267)
    at javax.mail.Transport.send0(Transport.java:252)
    at javax.mail.Transport.send(Transport.java:174)
    at Test.main(Test.java:130)
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:619)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:393)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2160)
    ... 7 more

但是如果我删除 ssl 属性,这是新的错误

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketException: Software caused connection abort: recv failed
    at Test.main(Test.java:134)
Caused by: javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketException: Software caused connection abort: recv failed
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2445)
    at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:2307)
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2340)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1808)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1285)
    at javax.mail.Transport.send0(Transport.java:255)
    at javax.mail.Transport.send(Transport.java:174)
    at Test.main(Test.java:130)
Caused by: java.net.SocketException: Software caused connection abort: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:126)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:104)
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2425)
    ... 7 more

谢谢

【问题讨论】:

    标签: java email jakarta-mail socketexception


    【解决方案1】:

    这与 jar 无关,更多的是 SMTP 服务器错误。 你可以尝试使用

    telnet mail.domain.tld
    

    并尝试连接到主机。我建议你包括一个像

    这样的端口
    props.put("mail.smtp.port", "portnumber");
    

    您可以尝试使用标准端口 25(不安全)或 587(安全)。

    【讨论】:

    • 感谢@Guru 我在端口 25 上尝试了 telnet,即使我在控制台上有消息“我们不授权使用此系统来传输未经请求的和/或批量电子”,连接也已完成邮件”。
    • 在我还添加的代码上,就像你建议的 props.put("mail.smtp.port", "portnumber"); 现在我有错误 Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Invalid Addresses; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 550-Please turn on SMTP Authentication in your mail client. (Name) 550-[41.244.226.223]:63273 is not permitted to relay through this server 550 without authentication.
    • 如果仍然失败,请打开JavaMail debug output并将调试输出添加到上面的原始帖子中。
    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 2014-03-22
    • 2018-06-24
    • 2012-08-23
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多