2012-07-12 149 views
0

代理的用戶名/密碼,我在使用JavaMail發送郵件(SMTP協議)如下如何設置襪子在Java郵件

String host = "smtp.gmail.com"; 
: 
props.put("mail.smtp.auth", "true"); 

props.put("mail.smtp.socks.host","sock_proxy_host"); 
props.put("mail.smtp.socks.port","sock_proxy_port"); 

Session session = Session.getInstance(props,new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("..", ".."); 
     } 
}); 

但是我使用socks代理需要基本身份驗證。我想設置憑據作爲

System.setProperty("java.net.socks.username", "socks_username");    
System.setProperty("java.net.socks.password", "socks_passwd"); 

是否有任何其他的方式來設置socks代理的用戶名/密碼(使用JavaMail API)?

+0

JavaMail使用java.net.Proxy類來提供SOCKS支持,但不幸的是,它不允許指定代理的用戶名和密碼。按照下面的建議希望設置一個Authenticator將會起作用。 – 2014-04-02 02:00:42

回答

0

您應該java.net.Authenticator中定義一個類實現:

java.net.Authenticator authenticator = new java.net.Authenticator() { 

protected java.net.PasswordAuthentication getPasswordAuthentication() { 
     return new java.net.PasswordAuthentication(username, password.toCharArray()); 
     } 
}; 

System.setProperty("java.net.socks.username", username); 
System.setProperty("java.net.socks.password", password); 
java.net.Authenticator.setDefault(authenticator); 
0

的JavaMail不支持代理身份驗證,只有匿名SOCKS代理。我不知道任何java庫,除了Simple Java Mail,這是開源的。

簡單的Java郵件增加了支持使用a trick認證代理:它運行一個臨時的匿名SOCKS服務器用來爲JavaMail連接到同一臺主機上,然後通過中繼驗證手動到外部SOCKS代理的JavaMail外的連接。

這裏是再次你的代碼,但使用簡單的Java郵件此時:

Mailer mailer = new Mailer(
     new ServerConfig("smtp.gmail.com", thePort, "..", ".."), 
     TransportStrategy.SMTP_TLS, 
     new ProxyConfig("sock_proxy_host", "sock_proxy_port", socks_username, socks_passwd) 
); 

mailer.sendMail(email); 

你不需要設置任何財產或其他配置,這一切都照顧。

相關問題