使用javamail通过代理发送邮件

    我们是没办法使用javamail通过一般的代理服务器发送邮件的,比如下面的代码是没有效果的:
        Properties props  =  System.getProperties();
        props.setProperty(
" proxySet " , " true " );
        props.setProperty(
" ProxyHost " , " 192.168.155.1 " );
        props.setProperty(
" ProxyPort " , " 1080 " );
        // 或者这样,也是没用
        // Properties props = System.getProperties();
        // props.setProperty("proxySet","true");
        // props.setProperty("http.proxyHost","192.168.155.1");
        // props.setProperty("http.proxyPort","808");

        props.put(
" mail.smtp.auth " " true " );
        props.put(
" mail.debug " " true " );
        
//  props.put("mail.store.protocol", "pop3");
        
//  props.put("mail.transport.protocol", "smtp");
         final  String username  = "username"
        
final  String password  = "password"
        Session session 
=  Session.getDefaultInstance(props,
                
new  Authenticator() {
                    
protected  javax.mail.PasswordAuthentication getPasswordAuthentication() {
                        
return   new  javax.mail.PasswordAuthentication(username,
                                password);
                    }
                });
        MimeMessage message 
=   new  MimeMessage(session);
       
 
   
    不过可以通过socks网关来访问外网的email服务器,当然,前提是你安装了一个socks服务器。完整的使用代理发送邮件代码如下:

    
package  net.rubyeye.testjms;

import  java.util.Date;
import  java.util.Properties;
import  java.security.Security;
import  javax.mail.Address;
import  javax.mail.Authenticator;
import  javax.mail.Session;
import  javax.mail.Transport;
import  javax.mail.internet.InternetAddress;
import  javax.mail.internet.MimeMessage;

public   class  MailTest {

    
/**
     * 
@param  args
     
*/
    
public   static   void  main(String[] args)  throws  Exception {
        
//  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        
//  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        
// 设置代理服务器
        Properties props  =  System.getProperties();
        props.setProperty(
" proxySet " " true " );
        props.setProperty(
" socksProxyHost " " 192.168.155.1 " );
        props.setProperty(
" socksProxyPort " " 1081 " );
        props.setProperty(
" mail.smtp.host " " smtp.126.com " );
        
        
//  props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        
//  props.setProperty("mail.smtp.socketFactory.fallback", "false");
        
//  props.setProperty("mail.smtp.port", "465");
        
//  props.setProperty("mail.smtp.socketFactory.port", "465");
        props.put( " mail.smtp.auth " " true " );
        props.put(
" mail.debug " " true " );
        
//  props.put("mail.store.protocol", "pop3");
        
//  props.put("mail.transport.protocol", "smtp");
         final  String username  =   " username " ;
        
final  String password  =   " password " ;
        
        
// 使用验证
        Session session  =  Session.getDefaultInstance(props,
                
new  Authenticator() {
                    
protected  javax.mail.PasswordAuthentication getPasswordAuthentication() {
                        
return   new  javax.mail.PasswordAuthentication(username,
                                password);
                    }
                });
        MimeMessage message 
=   new  MimeMessage(session);
        Address address 
=   new  InternetAddress( " [email protected] " );
        Address toAaddress 
=   new  InternetAddress( " [email protected] " );
        
        message.setFrom(address);
        message.setRecipient(MimeMessage.RecipientType.TO, toAaddress);
        message.setSubject(
" 测试 " );
        message.setText(
" test " );
        message.setSentDate(
new  Date());
        
        Transport.send(message);
        System.out.println(
" 邮件发送! " );

    }

}

你可能感兴趣的:(Security,.net,SUN,java)