我正在尝试从我的收件箱中读取邮件,但我一直收到异常“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();
}
}
例外:
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已启用但仍无效
你还有其他想法可能是什么原因吗?
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
答案 0 :(得分:2)
根据this link from Gmail,服务器应为pop.gmail.com
(而不是smtp.gmail.com
),您必须允许POP访问您的帐户才能实现此目的。
答案 1 :(得分:0)
大多数人都无法在SMTP中配置Gmail ID。
首先,你必须在gmail帐户上进行一些设置:
1)在该帐户上设置不太安全的应用
2)从帐户设置选项
启用弹出窗口3)如果它是Windows服务器登录gmail帐户一次在同一台服务器上。
它将100%工作。
由于 Sajju