2016-04-25 3 views
0

Ich entwickle ein Programm, das E-Mails von Gmail liest, in einer Organisation, die ein Proxy-System hat, um Benutzeraktivität zu überwachen. Ich habe versucht, alle möglichen Lösungen zu verwenden, aber keine scheint zu funktionieren. Jede Hilfe würde wirklich geschätzt werden. Vielen Dank im Voraus für Ihre Hilfe.Java lesen E-Mail von Gmail hinter einem Proxy-Server

Code:

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); 
} 
} 

Der fließende ist der Fehlercode, die ich bekomme. 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 
+0

Warum nicht? Was geschieht? – SLaks

+0

zeigen Sie uns bitte den Code –

+0

Sehr geehrte @SLaks Ich bekomme java.net.UnknownHostException. dh. Das Java kann nicht mit dem Google Mail-Server kommunizieren. – Anonymous

Antwort

0

Here oracle say:

Java Mail derzeit nicht unterstützt den Zugriff auf Mail-Server über einen Web-Proxy-Server.
...
Wenn Ihr Proxy-Server das SOCKS V4- oder V5-Protokoll unterstützt (http://www.socks.nec.com/aboutsocks.html, RFC1928) und anonyme Verbindungen zulässt und Sie JDK 1.5 oder neuer und JavaMail 1.4.5 oder neuer verwenden, können Sie einen SOCKS konfigurieren Proxy pro Sitzung und pro Protokoll, indem Sie die Eigenschaft "mail.smtp.socks.host" wie in den Javadocs für das Paket com.sun.mail.smtp beschrieben festlegen. Ähnliche Eigenschaften existieren für die Protokolle "imap" und "pop3".