JavaMail问题:无法向SMTP主机发送命令

时间:2011-07-18 23:58:31

标签: java javamail

我试图做java邮件而且我收到错误"无法向SMTP主机发送命令"。任何帮助,将不胜感激。以及任何未来的问题'解决方案如果可能 确切的例外是

javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is:
    java.net.SocketException: Connection closed by remote host
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2106)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2093)
    at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1184)
    at javax.mail.Transport.send0(Transport.java:197)
    at javax.mail.Transport.send(Transport.java:124)
    at TestEmail.main(TestEmail.java:45)
    at __SHELL17.run(__SHELL17.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:774)
Caused by: java.net.SocketException: Connection closed by remote host
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1186)
    at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:43)
    at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:114)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2104)
    ... 11 more

我的代码如下

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

// Send a simple, single part, text/plain e-mail
public class TestEmail {

    public static void main(String[] args) {

        // SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
        String to = "my gmail account whose name i removed for publicity";
        String from = "my hotmail account whose name i removed for publicity";
        // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
        String host = "smtp.live.com";

        // Create properties, get Session
        Properties props = new Properties();

        // If using static Transport.send(),
        // need to specify which host to send it to
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.starttls.enable", "true");
        // To see what is going on behind the scene
        props.put("mail.debug", "true");
        Session session = Session.getInstance(props);

        try {
            // Instantiatee a message
            Message msg = new MimeMessage(session);

            //Set message attributes
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("Test E-Mail through Java");
            msg.setSentDate(new Date());

            // Set message content
            msg.setText("This is a test of sending a " +
                        "plain text e-mail through Java.\n" +
                        "Here is line 2.");

            //Send the message
            Transport.send(msg);
        }
        catch (MessagingException mex) {
            // Prints all nested (chained) exceptions as well
            mex.printStackTrace();
        }
    }
}

6 个答案:

答案 0 :(得分:11)

今天我经历了同样的问题。但对我来说问题是,在smtp服务器TLS没有启用。所以我改变了这样的邮件属性。
mail.smtp.starttls.enable=false

现在每件事对我都有好处。

答案 1 :(得分:2)

就我而言,在启用邮件程序调试后,我能够找到根问题。

启用邮件程序调试的不同方法:

java -Dmail.debug=true ...

props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");

Jenkins config(/ etc / default / jenkins):

JAVA_ARGS="-Dmail.smtp.starttls.enable=true -Dmail.debug=true"

更多信息:http://www.oracle.com/technetwork/java/faq-135477.html

我的特别错误是,在创建电子邮件时,我在“from:”行中的地址不正确。 Google“G Suite”(适用于商家的Google应用)要求发件人地址与帐户所有者位于同一域中。例如。 mycompanyname.com

邮件调试显示:

MAIL FROM:<jenkins-pipeline@bogusdomain.com> 550-5.7.1 Invalid credentials for relay [192.168.42.42]. The IP address you've 550-5.7.1 registered in your G Suite SMTP Relay service doesn't match domain of 550-5.7.1 the account this email is being sent from. If you are trying to relay 550-5.7.1 mail from a domain that isn't registered under your G Suite account

答案 2 :(得分:1)

服务器需要STARTTLS。如果我使用telnet进行手动SMTP会话,这就是我得到的结果:

220 BLU0-SMTP122.phx.gbl Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at  Mon, 18 Jul 2011 17:08:14 -0700
HELO jhmg.net
250 BLU0-SMTP122.phx.gbl Hello [70.123.155.64]
MAIL FROM:<zzz@zzz.com>
530 5.7.0 Must issue a STARTTLS command first

此服务器不接受未加密的连接

答案 3 :(得分:0)

尝试从地址

添加

mail.setFromAddress(&#34; Name&#34;);

答案 4 :(得分:0)

您需要将ssl信任设置为smtp.gmail.com

properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");

和身份验证器作为会话参数


        Session session = Session.getDefaultInstance(properties,new javax.mail.Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication(
                        sender, senderPassword);// Specify the Username and the PassWord
            }
        });

答案 5 :(得分:-1)

为 O365 添加了这一行。现在工作正常。

sslMailProps.put("mail.smtp.ssl.enable", "true");