【发布时间】:2016-04-25 16:17:59
【问题描述】:
我正在开发一个从 gmail 读取电子邮件的程序,该程序有一个代理系统来监控用户活动。我尝试使用所有可能的解决方案,但似乎没有一个有效。任何帮助将不胜感激。在此先感谢您的帮助。
代码:
public MailReader()
{
/* Set the mail properties */
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com","<EMAIL>", "<PASS>");
/* Mention the folder name which you want to read. */
inbox = store.getFolder("Inbox");
System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());
/*Open the inbox using store.*/
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox*/
Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try
{
printAllMessages(messages);
inbox.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (NoSuchProviderException e)
{
e.printStackTrace();
System.exit(1);
}
catch (MessagingException e)
{
e.printStackTrace();
System.exit(2);
}
}
流动的是我得到的错误代码。 java.net.UnknownHostException
javax.mail.MessagingException: imap.gmail.com;
nested exception is:
java.net.UnknownHostException: imap.gmail.com
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:665)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javaapplication1.MailReader.<init>(MailReader.java:29)
at javaapplication1.MailReader.main(MailReader.java:149)
Caused by: java.net.UnknownHostException: imap.gmail.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:110)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:632)
... 4 more
【问题讨论】:
-
为什么不呢?会发生什么?
-
请给我们看代码
-
亲爱的@SLaks 我得到 java.net.UnknownHostException 。 IE。 java无法与gmail服务器通信。
-
最终您的 DNS 服务器不知道或不会为您提供有关 imap.gmail.com 的信息。这意味着您将永远无法连接,因为它找不到您需要的 IP 地址。在您的环境中,由于 DNS 问题,您不太可能解决此问题。
-
@stdunbar 我的浏览器和widows邮件怎么能通过同一个代理系统连接到gmail?
标签: java gmail jakarta-mail