Java MailConnectException:无法连接到主机,端口:smtp.gmail.com,110;超时 -1;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25168481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 110; timeout -1;
提问by Xerath
I'm trying to read messages from my inbox, but I'm keep getting exception "MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 110; timeout -1;"
我正在尝试从我的收件箱中读取邮件,但我不断收到异常“MailConnectException:无法连接到主机,端口:smtp.gmail.com,110;超时 -1;”
I disabled my AntiVir and Firewall, but doesn't help.
我禁用了我的 AntiVir 和防火墙,但没有帮助。
Below is code and console report:
下面是代码和控制台报告:
public class JavaMailPOP3eMail {
private String server = null;
private String user = null;
private String pass = null;
public void sendMail() {
server = "smtp.gmail.com";
user = "[email protected]";
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)
EDIT:
编辑:
POP is enabled but still doesn't work
POP 已启用但仍不起作用
Do you have any other idea what the reason could be ?
你有什么其他想法可能是什么原因?
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
采纳答案by Mariano D'Ascanio
According to this link from Gmail, the server should be pop.gmail.com
(instead of smtp.gmail.com
) and you have to allow POP access to your account for this to work.
根据Gmail 的这个链接,服务器应该是pop.gmail.com
(而不是smtp.gmail.com
),并且您必须允许 POP 访问您的帐户才能使其工作。
回答by Sujauddin Saiyed
Most people get failed to configured Gmail ID in SMTP .
大多数人无法在 SMTP 中配置 Gmail ID。
First you have to do some setting on gmail account :
首先,您必须对 gmail 帐户进行一些设置:
1) Set less secure app on that account
1)在该帐户上设置安全性较低的应用程序
2) Enable pop from account setting option
2)启用从帐户设置选项弹出
3) if it is windows server login gmail account once on the same server.
3)如果是windows server,在同一台服务器上登录gmail账号一次。
It will work 100 percent.
它将 100% 工作。
Thanks Sajju
谢谢Sajju