首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

javamail不支持署理

2013-12-23 
javamail不支持代理Spring框架提供了JavaMailSender接口及其实现类JavaMailSenderImpl,基于这个类可以更加

javamail不支持代理
Spring框架提供了JavaMailSender接口及其实现类JavaMailSenderImpl,基于这个类可以更加方便实现发送邮件功能。

在web工程中,可以把JavaMailSender交由Spring IOC管理。如下面的配置:



Xml代码 
1.<bean id="javaMailSender" value="${email.encoding}"></property> 
3.        <property name="host" value="${email.host}"></property> 
4.        <property name="username" value="${email.username}"></property> 
5.        <property name="password" value="${email.password}"></property> 
6.        <property name="protocol" value="${email.protocal}"></property> 
7.        <property name="javaMailProperties"> 
8.            <props> 
9.                <!-- 让服务器检验用户密码是否正确 --> 
10.                <prop key="mail.smtp.auth">true</prop> 
11.                <prop key="mail.smtp.timeout">25000</prop> 
12.                <prop key="mail.debug">true</prop> 
13.            </props> 
14.        </property> 
15.    </bean> 
<bean id="javaMailSender" value="${email.encoding}"></property>
<property name="host" value="${email.host}"></property>
<property name="username" value="${email.username}"></property>
<property name="password" value="${email.password}"></property>
<property name="protocol" value="${email.protocal}"></property>
<property name="javaMailProperties">
<props>
<!-- 让服务器检验用户密码是否正确 -->
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean> 使用时,只需在Spring容器中获取到JavaMailSenderImpl实例,调用JavaMailSender.send()方法,即可实现发送邮件的功能。

由于,公司使用的网络不能直接访问外网,而发送邮件的却是对外的。所以需要设置代理服务器。而java.mail是不知道http代理的,只能通过socks V4 或者 V5代理发送邮件。官方原文:

Q: How do I configure JavaMail to work through my proxy server?
A: JavaMail does not currently support accessing mail servers through a web proxy server. 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. The firewall will typically block most access to the Internet, but will allow requests from the proxy server to pass through. 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.

If your proxy server supports the SOCKS V4 or V5 protocol (http://www.socks.nec.com/aboutsocks.html, RFC1928) and allows anonymous connections, and you're using JDK 1.5 or newer and JavaMail 1.4.5 or newer, you can configure a SOCKS proxy on a per-session, per-protocol basis by setting the "mail.smtp.socks.host" property as described in the javadocs for the com.sun.mail.smtp package. Similar properties exist for the "imap" and "pop3" protocols.

If you're using older versions of the JDK or JavaMail, you can tell the Java runtime to direct all TCP socket connections to the SOCKS server. See the Networking Properties guide for the latest documentation of the socksProxyHost and socksProxyPort properties. These are system-level properties, not JavaMail session properties. 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. Note that setting these properties directs all TCP sockets to the SOCKS proxy, which may have negative impact on other aspects of your application.

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. JavaMail does not support access through a HTTP proxy web server.
所以在调用send()方法发送邮件之前,必须设置代理服务器。

这里用两种方式实现代理,1、设置jvm的参数,2、在程序中通过Properties实现。使用的参数可参照官网文档:Networking Properties,本文主要与大家交流第二种方式。

在web 启动是,加载一个自定义的Servlet,该Servlet就是实现了,设置代理服务器的功能。如部分代码:



Java代码 
1.System.getProperties().put("proxySet", true);  
2.            System.getProperties().put("http.proxyHost", Config.getProperties("http.proxyHost"));  
3.            System.getProperties().put("http.proxyPort", Config.getProperties("http.proxyPort"));  
4.            System.getProperties().put("socksProxySet", true);  
5.            System.getProperties().put("socksProxyHost", Config.getProperties("http.proxyHost"));  
6.            System.getProperties().put("socksProxyPort", Config.getProperties("http.proxyPort")); 
System.getProperties().put("proxySet", true);
System.getProperties().put("http.proxyHost", Config.getProperties("http.proxyHost"));
System.getProperties().put("http.proxyPort", Config.getProperties("http.proxyPort"));
System.getProperties().put("socksProxySet", true);
System.getProperties().put("socksProxyHost", Config.getProperties("http.proxyHost"));
System.getProperties().put("socksProxyPort", Config.getProperties("http.proxyPort")); 同样,在java mail的官方文档中也有提到,如果使用的是jdk 1.5以上和java.mail 1.4.5,可以通过javaMailProperties配置,更多配置项,参考 com.sun.mail.smtp package。 <!-- javaMailSender -->

Java代码 
1.<bean id="javaMailSender" value="${email.encoding}"></property>  
3.    <property name="host" value="${email.host}"></property>  
4.    <property name="username" value="${email.username}"></property>  
5.    <property name="password" value="${email.password}"></property>  
6.    <property name="protocol" value="${email.protocal}"></property>  
7.    <property name="javaMailProperties">  
8.        <props>  
9.            <!-- 让服务器检验用户密码是否正确 -->  
10.            <prop key="mail.smtp.auth">true</prop>  
11.            <prop key="mail.smtp.timeout">25000</prop>  
12.            <prop key="mail.debug">true</prop>  
13.            <prop key="mail.smtp.ssl.enable">true</prop>  
14.            <prop key="mail.smtp.socks.host">cmproxy.gmcc.net</prop>  
15.            <prop key="mail.smtp.socks.port">8081</prop>  
16.            <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>  
17.        </props>  
18.    </property>  
19.</bean>

热点排行