简体   繁体   English

使用Javamail通过imap访问gmail(位于代理之后)

[英]Accessing gmail via imap using Javamail (behind a proxy)

I am using code from on the earlier threads(have pasted the code below as well): 我在较早的线程上使用了代码(也粘贴了下面的代码):

Getting mail from GMail into Java application using IMAP 使用IMAP将邮件从GMail传输到Java应用程序

How do I make this code work if I am behind a proxy? 如果我在代理后面,如何使此代码起作用?

The connection is getting timed out, I have tried to search for a solution but to no avail. 连接超时,我尝试寻找解决方案,但无济于事。

public static void main(String args[]) {

    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imaps");
        try {
            Session session = Session.getDefaultInstance(props, null);
            Store store = session.getStore("imaps");
            store.connect("imap.gmail.com", email, password);
            System.out.println(store);

            Folder inbox = store.getFolder("Inbox");
            inbox.open(Folder.READ_ONLY);
            Message messages[] = inbox.getMessages();
            for(Message message:messages) {
            System.out.println(message);
        }
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (MessagingException e) {
        e.printStackTrace();
        System.exit(2);
    }

}

Thanks, Chris. 谢谢,克里斯。

Behind the proxy doesn't stop you to connect to IMAP 代理背后并没有阻止您连接到IMAP

There could be 2 reasons 可能有两个原因

  1. The IMAP port is block by your network administrator IMAP端口已被您的网络管理员阻止
  2. your IP is blocked/banned by Google 您的IP被Google阻止/禁止

Thanks 谢谢

Check out the Java Mail FAQ : 查看Java Mail常见问题解答

Q: How do I configure JavaMail to work through my proxy server? 问:如何配置JavaMail通过代理服务器工作?

A: JavaMail does not currently support accessing mail servers through a web proxy server. 答: JavaMail当前不支持通过Web代理服务器访问邮件服务器。 One of the major reasons for using a proxy server is to allow HTTP requests from within a corporate network to pass through a corporate firewall. 使用代理服务器的主要原因之一是允许来自公司网络内的HTTP请求通过公司防火墙。 The firewall will typically block most access to the Internet, but will allow requests from the proxy server to pass through. 防火墙通常会阻止大多数对Internet的访问,但会允许来自代理服务器的请求通过。 In addition, a mail server inside the corporate network will perform a similar function for email, accepting messages via SMTP and forwarding them to their ultimate destination on the Internet, and accepting incoming messages and sending them to the appropriate internal mail server. 此外,公司网络内部的邮件服务器将执行类似的电子邮件功能,通过SMTP接收邮件并将其转发到Internet上的最终目的地,并接受传入邮件并将其发送到适当的内部邮件服务器。

If your proxy server supports the SOCKS V4 or V5 protocol (http://www.socks.nec.com/aboutsocks.html, RFC1928) and allows anonymous connections, you can tell the Java runtime to direct all TCP socket connections to the SOCKS server. 如果您的代理服务器支持SOCKS V4或V5协议(http://www.socks.nec.com/aboutsocks.html,RFC1928)并允许匿名连接,则可以告诉Java运行时将所有TCP套接字连接定向到SOCKS。服务器。 See the Networking Properties guide for the latest documentation of the socksProxyHost and socksProxyPort properties. 有关socksProxyHost和socksProxyPort属性的最新文档,请参见网络属性指南。 These are system-level properties, not JavaMail session properties. 这些是系统级属性,而不是JavaMail会话属性。 They can be set from the command line when the application is invoked, for example: java -DsocksProxyHost=myproxy .... This facility can be used to direct the SMTP, IMAP, and POP3 communication from JavaMail to the SOCKS proxy server. 可以在调用应用程序时从命令行设置它们,例如:java -DsocksProxyHost = myproxy...。此功能可用于将SMTP,IMAP和POP3通信从JavaMail定向到SOCKS代理服务器。 Note that setting these properties directs all TCP sockets to the SOCKS proxy, which may have negative impact on other aspects of your application. 请注意,设置这些属性会将所有TCP套接字定向到SOCKS代理,这可能会对应用程序的其他方面产生负面影响。

Without such a SOCKS server, if you want to use JavaMail to directly access mail servers outside the firewall, the firewall will need to be configured to allow such access. 如果没有这样的SOCKS服务器,如果要使用JavaMail直接访问防火墙外部的邮件服务器,则需要将防火墙配置为允许这种访问。 JavaMail does not support access through a HTTP proxy web server. JavaMail不支持通过HTTP代理Web服务器进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM