gpt4 book ai didi

JavaMail 问题 : Can't send command to SMTP host

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:07 31 4
gpt4 key购买 nike

我正在尝试发送 java 邮件,但收到错误消息“无法向 SMTP 主机发送命令”。任何帮助,将不胜感激。如果可能的话,还有任何 future 问题的解决方案。确切的异常(exception)是

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();
}
}
}

最佳答案

今天我遇到了同样的问题。但对我来说,问题是在 smtp 服务器中未启用 TLS。所以我已经像这样更改了邮件属性。
mail.smtp.starttls.enable=false

现在一切都对我有用。

关于JavaMail 问题 : Can't send command to SMTP host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6740838/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com