gpt4 book ai didi

java - MailConnectException : Couldn't connect to host, 端口 : smtp. gmail.com,110;超时-1;

转载 作者:行者123 更新时间:2023-11-30 09:04:35 34 4
gpt4 key购买 nike

我正在尝试从我的收件箱中读取邮件,但我一直收到异常“MailConnectException:无法连接到主机,端口:smtp.gmail.com,110;超时-1;”

我禁用了我的 AntiVir 和防火墙,但没有帮助。

下面是代码和控制台报告:

public class JavaMailPOP3eMail {

private String server = null;
private String user = null;
private String pass = null;

public void sendMail() {
server = "smtp.gmail.com";
user = "user123@gmail.com";
pass = "pass123";

Store store = null;
Folder folder = null;
try {
// get default session
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties, null);
session.setDebug(true);
// get a pop3 message store, and connect to it
store = session.getStore("pop3");
store.connect(server, user, pass);
// get the default folder
folder = store.getDefaultFolder();
if (folder == null) {
throw new Exception("No default folder");
}
// get the inbox
folder = folder.getFolder("INBOX");
if (folder == null) {
throw new Exception("No POP3 INBOX");
}
// open the folder read only
folder.open(Folder.READ_ONLY);
// get the messages and print them
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
printMail(messages[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (folder != null) {
folder.close(false);
}
if (store != null) {
store.close();
}
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
}

public void printMail(Message message) {
try {
// get header information
String from = null;
from = ((InternetAddress) message.getFrom()[0]).getPersonal();
// print sender details
System.out.println("From: " + from);
// get and print subject
String subj = message.getSubject();
System.out.println("Subject: " + subj);
// get the message itself
Part messagePart = message;
Object content = messagePart.getContent();
if (content instanceof Multipart) {
messagePart = ((Multipart) content).getBodyPart(0);
System.out.println("[ Multipart Message ]");
}
// get the content type
String contentType = messagePart.getContentType();
// if the content is plain text, print it
System.out.println("Content: " + contentType);
if (contentType.startsWith("text/plain") ||
contentType.startsWith("text/html")) {
InputStream is = messagePart.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is)
);
String line = br.readLine();
while (line != null ) {
System.out.println(line);
line = br.readLine();
}
}
System.out.println("");
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
JavaMailPOP3eMail mail = new JavaMailPOP3eMail();
mail.sendMail();
}

}

异常(exception):

DEBUG: setDebug: JavaMail version 1.5.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]
DEBUG POP3: mail.pop3.rsetbeforequit: false
DEBUG POP3: mail.pop3.disabletop: false
DEBUG POP3: mail.pop3.forgettopheaders: false
DEBUG POP3: mail.pop3.cachewriteto: false
DEBUG POP3: mail.pop3.filecache.enable: false
DEBUG POP3: mail.pop3.keepmessagecontent: false
DEBUG POP3: mail.pop3.starttls.enable: false
DEBUG POP3: mail.pop3.starttls.required: false
DEBUG POP3: mail.pop3.apop.enable: false
DEBUG POP3: mail.pop3.disablecapa: false
DEBUG POP3: connecting to host "smtp.gmail.com", port 110, isSSL false
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 110; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:211)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at example3.JavaMailPOP3eMail.sendMail(JavaMailPOP3eMail.java:43)
at example3.JavaMailPOP3eMail.main(JavaMailPOP3eMail.java:118)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:312)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
at com.sun.mail.pop3.Protocol.<init>(Protocol.java:112)
at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:264)
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:207)

编辑:

POP 已启用,但仍然不起作用

enter image description here

您是否知道原因可能是什么?

DEBUG: setDebug: JavaMail version 1.5.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]
DEBUG POP3: mail.pop3.rsetbeforequit: false
DEBUG POP3: mail.pop3.disabletop: false
DEBUG POP3: mail.pop3.forgettopheaders: false
DEBUG POP3: mail.pop3.cachewriteto: false
DEBUG POP3: mail.pop3.filecache.enable: false
DEBUG POP3: mail.pop3.keepmessagecontent: false
DEBUG POP3: mail.pop3.starttls.enable: false
DEBUG POP3: mail.pop3.starttls.required: false
DEBUG POP3: mail.pop3.apop.enable: false
DEBUG POP3: mail.pop3.disablecapa: false
DEBUG POP3: connecting to host "pop.gmail.com", port 110, isSSL false
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: pop.gmail.com, 110; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:211) at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:211)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at example3.JavaMailPOP3eMail.sendMail(JavaMailPOP3eMail.java:41)
at example3.JavaMailPOP3eMail.main(JavaMailPOP3eMail.java:116)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:312)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
at com.sun.mail.pop3.Protocol.<init>(Protocol.java:112)
at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:264)
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:207)
... 4 more

最佳答案

根据 this link from Gmail ,服务器应该是 pop.gmail.com(而不是 smtp.gmail.com),并且您必须允许 POP 访问您的帐户才能正常工作。

关于java - MailConnectException : Couldn't connect to host, 端口 : smtp. gmail.com,110;超时-1;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25168481/

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