首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaMail问题:无法向SMTP主机发送命令

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

Stack Overflow用户
提问于 2011-07-19 07:58:31
回答 6查看 69.5K关注 0票数 8

我正在尝试执行java邮件,但收到错误消息“无法将命令发送到SMTP主机”。任何帮助都将不胜感激。如果可能的话,还有任何未来问题的解决方案。确切的例外是

代码语言:javascript
运行
复制
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

我的代码如下

代码语言:javascript
运行
复制
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();
        }
    }
}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-07-19 08:09:57

服务器需要STARTTLS。下面是我使用telnet进行手动SMTP会话时得到的结果:

代码语言:javascript
运行
复制
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

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

票数 1
EN

Stack Overflow用户

发布于 2013-01-07 18:18:37

今天我也遇到了同样的问题。但对我来说,问题是,在smtp服务器上没有启用TLS。因此,我像这样更改了邮件属性。

mail.smtp.starttls.enable=false

现在,每一件事都对我有利。

票数 12
EN

Stack Overflow用户

发布于 2017-02-18 08:42:23

在我的例子中,在启用邮件程序调试后,我能够找到问题的根源。

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

java -Dmail.debug=true ...

代码语言:javascript
运行
复制
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");

Jenkins配置(/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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6740838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档