![](/img/trans.png)
[英]How to send email from a Gmail account behind HTTP proxy which requires authentication in JAVA?
[英]Java read email from gmail behind a proxy server
我正在開發一個程序,該程序在具有代理系統來監視用戶活動的組織中從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
JavaMail當前不支持通過Web代理服務器訪問郵件服務器。
...
如果您的代理服務器支持SOCKS V4或V5協議( http://www.socks.nec.com/aboutsocks.html,RFC1928 )並允許匿名連接,並且您使用的是JDK 1.5或更高版本以及JavaMail 1.4.5或較新的版本,您可以按照com.sun.mail.smtp軟件包的javadocs中的說明設置“ mail.smtp.socks.host”屬性,以每個會話,每個協議為基礎配置SOCKS代理。 對於“ imap”和“ pop3”協議,存在類似的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.