使用Spring mail通过socks代理发送邮件

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

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

 

<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="defaultEncoding" 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就是实现了,设置代理服务器的功能。如部分代码:

 

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 -->

	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="defaultEncoding" 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>
				<prop key="mail.smtp.ssl.enable">true</prop>
				<prop key="mail.smtp.socks.host">cmproxy.gmcc.net</prop>
				<prop key="mail.smtp.socks.port">8081</prop>
				<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
			</props>
		</property>
	</bean>
 

你可能感兴趣的:(java mail,spring mail,java 代理,JavaMailSender)