有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我正在尝试使用javamail api发送邮件,但收到450 4.7.1客户端主机拒绝错误

DEBUG: setDebug: JavaMail version 1.4.4
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.server.com", port 25, isSSL false
220 smtp.server.com ESMTP Postfix (Ubuntu)
DEBUG SMTP: connected to host "smtp.server.com", port: 25

EHLO MYPC
250-smtp.server.com
250-PIPELINING
250-SIZE 20480000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "20480000"
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<DoNotReply@example.com>
250 2.1.0 Ok
RCPT TO:<someone@domain.com>
450 4.7.1 Client host rejected: cannot find your hostname, [172.17.9.70]
DEBUG SMTP: Valid Unsent Addresses
DEBUG SMTP:   someone@domain.com
DEBUG SMTP: Sending failed because of invalid destination addresses
RSET
250 2.0.0 Ok
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
        com.sun.mail.smtp.SMTPAddressFailedException: 450 4.7.1 Client host rejected: cannot find your hostname, [172.17.9.70]

        at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1835)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1098)
        at javax.mail.Transport.send0(Transport.java:195)
        at javax.mail.Transport.send(Transport.java:124)
        at defaultsrc.SendMail.main(SendMail.java:71)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 450 4.7.1 Client host rejected: cannot find your hostname, [172.17.9.70]

        at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1700)
        ... 9 more
QUIT
221 2.0.0 Bye

the above ip 172.17.9.70 is my computer lan IP and MYPC is my host name. It seem than mailserver is successfully connected. But don't know why it says "client host rejected". The mail server does not require any authentication and use port 25. Can anybody help me??


共 (2) 个答案

  1. # 1 楼答案

    我将附和其他评论,并建议这可能是更好地询问服务器故障。。。但我猜这是一个DNS问题。MX正在从您的IP地址查找您的主机名,但没有找到任何内容(“找不到您的主机名”)。见https://en.wikipedia.org/wiki/Forward-confirmed_reverse_DNS

  2. # 2 楼答案

    我遇到了同样的错误,来这里寻找解决方案。 经过一番挖掘,我想出了以下代码,在我的案例中是有效的:

    String recipient = "recipient@example.com";
    String sender = "sender@example.com";
    Session session = Session.getDefaultInstance(new Properties());
    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(sender));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("TEST SUBJECT");
        message.setText("TEST BODY");
        Transport transport = session.getTransport("smtp");
        transport.connect("<smtp.host>", "<smtp.user>", "<smtp.password>");
        message.saveChanges();
        transport.sendMessage(message, new Address[]{new InternetAddress(recipient)});
        Transport.send(message);
    } catch (MessagingException mex) {
        mex.printStackTrace();
    }
    

    也许有人会发现它很有用:)