场景说明:
应用在内网服务器A上面部署,互联网出口服务器B部署Nginx,服务器B可以连接163邮箱服务。
服务器A的应用需要通过服务器B上的Nginx代理163的POP协议,通过代理获取163邮箱内的邮件信息。
Nginx的安装
Nginx官网下载,然后编译的时候增加mail、stream
如下命令:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-mail --with-stream
Nginx配置
打开Nginx的配置文件nginx.conf文件。
默认是有两个配置项
events {}
http{}
增加一个stream配置项
监听端口 995可以改为其他端口,我这里暂不修改,与163邮箱提供的SSL端口一致。
proxy_pass 填写被代理的地址需要加端口
stream{
server{
listen 995;
proxy_pass pop.163.com:995;
}
}
增加完成后如图所示:
代码调试
1.IP模式
POP3Store pop3Store = null;
Session session = setCollectProperties();
pop3Store = (POP3Store) session.getStore("pop3");
pop3Store.connect("150.***.***.152", 995, "email-user", "auth-code");
2.域名模式
注意域名模式需要提前将域名结下到对应的IP。
POP3Store pop3Store = null;
Session session = setCollectProperties();
pop3Store = (POP3Store) session.getStore("pop3");
pop3Store.connect("mail.exp.com", 995, "email-user", "auth-code");
均可以获取到邮箱数据
上面是POP协议的配置,SMTP及IMAP的配置同理。