JavaMail问题:无法向SMTP主机发送命令 [英] JavaMail Issue : Can't send command to SMTP host

查看:1296
本文介绍了JavaMail问题:无法向SMTP主机发送命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做java邮件并且我收到错误无法向SMTP主机发送命令。任何帮助,将不胜感激。如有可能,任何未来的问题解决方案。
确切的异常是

I'm trying to do java mail and im getting an error "Cant send command to SMTP host". Any help would be appreciated. And any future problems' solutions if possible. The exact Exception is

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会话,这就是我得到的:

The server requires STARTTLS. Here's what I get if I do a manual SMTP session with telnet:

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

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

This server does not accept unencrypted connections

这篇关于JavaMail问题:无法向SMTP主机发送命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆