当前位置: 首页 > 面试题库 >

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

谢典
2023-03-14
问题内容

我正在尝试执行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();
        }
    }
}

问题答案:

服务器需要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

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



 类似资料:
  • 我正在尝试从我的java发送电子邮件,但我无法连接到我的主机。这里是我的代码: 这里是我的代码: 这里又是我的报告错误: 有人能帮帮我吗?:(

  • 我已经将证书导入了cacerts(JAVA\u HOME指向的地方),但仍然出现了这个错误。无法解决问题。客户端的邮件服务器(Microsoft ESMTP服务器)没有防火墙问题。我可以从我的服务器远程登录。已尝试SSLpoke,但超时。试过25号端口的tcpdump,可以看到来回的通信。基本上是试图触发来自邮件服务器的出站电子邮件。 详细的错误日志为: 原因:javax。邮政MessaginEx

  • 我在我的camunda环境中有这个类(我在camunda modeller中使用这个类作为服务任务):

  • 当我从服务器发送电子邮件时,它会给我两个错误- SMTP错误:无法连接到SMTP主机。无法发送邮件。邮件错误:SMTP错误:无法连接到SMTP主机。 我在另一个关于堆栈溢出的答案上找到了很多提示,但都不起作用。我试过465/587/65号端口。如果我设置端口587,下面的代码在本地系统上也能正常工作。但在服务器中,它不起作用。

  • 我正在处理一封使用phpmailer类的Web邮件。问题是我收到错误“SMTP错误:无法连接到SMTP主机。邮件未发送PHP邮件错误:SMTP错误:无法连接到SMTP主机。” 我的代码是: 有人想让我知道我错在哪里,我需要你对这个问题的关注。

  • 我试图使用pyspark将每日批次的数据发送到Kafka主题,但我当前收到以下错误: Traceback(最近的最后一次调用): File", line 5, in File"/usr/local/rms/lib/hdp26_c5000/park2/python/pyspark/sql/readwriter.py", line 548, in保存自己。_jwrite.save()File"/usr