使用javamail經過代理髮送郵件

     經過socks網關來訪問外網的email服務器,前提是你安裝了一個socks服務器。完整的使用代理髮送郵件代碼以下:
package mail

import javax.mail.internet.{InternetAddress, MimeMessage}
import java.util.{Date, Properties}
import javax.mail.{Message, Session, Transport}


/**
 *
 * User: 劉永健
 * Date: 12-5-29
 * Time: 上午10:05
 * To change this template use File | Settings | File Templates.
 */

object JavaMailDemo {

  import scala.collection.JavaConversions._

  def main(args: Array[String]): Unit = {
    val conf = Map("proxySet" -> "true",
      "socksProxyHost" -> "10.123.74.137", "socksProxyPort" -> "1080",
      "mail.smtp.host" -> "smtpav.travelsky.com", "mail.smtp.port" -> "25", "mail.transport.protocol" -> "smtp",
      "mail.pop3.host" -> "pop3.travelsky.com", "mail.pop3.port" -> "110", "mail.store.protocol" -> "pop3",
      "mail.smtp.auth" -> "false", "mail.debug" -> "true")
    val props = new Properties()
    props.putAll(conf)

    val session = Session.getDefaultInstance(props);
    // 須要smtp驗證
    //    val session = Session.getDefaultInstance(props, new Authenticator(){
    //      override def getPasswordAuthentication = new javax.mail.PasswordAuthentication(props.getProperty("mail.smtp.user"), props.getProperty("mail.smtp.password"))
    //    })
    val message = new MimeMessage(session)
    val fromAddr = new InternetAddress("liuyongjian@travelsky.com")
    val toAddr = new InternetAddress("liuyongjian@travelsky.com")
    message.setFrom(fromAddr)
    message.setRecipient(Message.RecipientType.TO, toAddr)
    message.setSubject("test")
    message.setText("測試")
    message.setSentDate(new Date())
    Transport.send(message)
    System.out.println("mail sent")

  }
}

我使用的java mail 版本是 1.4.3 . java

注意,當前java mail api不支持經過web proxy server訪問mail server。 詳細請參閱:http://stackoverflow.com/a/10086843/351545


web

相關文章
相關標籤/搜索