java 代理服务(proxy server)

Send mail through firewall

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author HappyQin
 */
public class MailService {
 private static boolean ready = false;
 private static Properties prop = null;

 private static String smtpHost;
 private static String smtpAuth;
 private static String smtpUser;
 private static String smtpPassword;
 private static String mailAddress;

 private MailService() {
  init();
 }

 private static void init() {
  prop = new Properties();

  FileInputStream fis = null;
  try {
   URL url = MailService.class.getClassLoader().getResource(
     "smtp.config");
   if (url != null) {
    fis = new FileInputStream(url.getFile());
    //System.out.println(url.getFile());
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  if (fis != null) {
   try {
    prop.load(fis);
   } catch (IOException e1) {
    e1.printStackTrace();
   }
  }
  mailAddress = prop.getProperty("SMTP_EMAIL");
  smtpPassword = prop.getProperty("SMTP_PWD");
  smtpAuth = prop.getProperty("SMTP_AUTH");
  smtpHost = prop.getProperty("SMTP_HOST");
  smtpUser = prop.getProperty("SMTP_USER");

  ProxyConfig proxy = new ProxyConfig();
  if ((proxy.getSocksProxyEnabled() != null)
    && ("true".equalsIgnoreCase(proxy.getSocksProxyEnabled()))) {
   System.setProperty("proxyHost", proxy.getSocksProxyHost());
   System.setProperty("proxyPort", proxy.getSocksProxyPort());
   System.setProperty("proxyUserName", smtpUser);
   System.setProperty("proxyPassword", smtpPassword);

   System.setProperty("socksProxySet", "true");
   System.setProperty("socksProxyHost", proxy.getSocksProxyHost());
   System.setProperty("socksProxyPort", proxy.getSocksProxyPort());
   System.setProperty("socksProxyUserName", smtpUser);
   System.setProperty("socksProxyPassword", smtpPassword);

   System.setProperty("socks.useProxy", "true");
   System.setProperty("socks.proxyHost", proxy.getSocksProxyHost());
   System.setProperty("socks.proxyPort", proxy.getSocksProxyPort());
   System.setProperty("socks.proxyUserName", smtpUser);
   System.setProperty("socks.proxyPassword", smtpPassword);

   if (proxy.getHttpNonProxyHosts() != null)
    System.setProperty("socksNonProxyHosts", proxy
      .getSocksNonProxyHosts());
  }

  ready = true;
 }

 public static void sendMail(String to, String subject, String content) {
  if (!ready) {
   init();
  }

  Properties props = new Properties();

  props.put("mail.smtp.host", smtpHost);
  props.put("mail.smtp.port", "" + 25);
  props.put("mail.smtp.auth", smtpAuth);
  props.put("mail.smtp.user", smtpUser);

  //props.put("mail.store.protocol", "pop3");
  //props.put("mail.transport.protocol", "smtp");

  Session session = Session.getDefaultInstance(props,
    new Authenticator() {
     public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(smtpUser,
        smtpPassword);
     }
    });
  MimeMessage message = new MimeMessage(session);
  try {
   message.setFrom(new InternetAddress(mailAddress));
   message.setRecipient(Message.RecipientType.TO, new InternetAddress(
     to));
   message.setSubject(subject);
   message.setContent(content, "text/plain;charset=gb2312");
   Transport.send(message);
  } catch (AddressException e) {
   e.printStackTrace();
  } catch (MessagingException e) {
   e.printStackTrace();
  }
 }

 public static void main(String args[]) {
  MailService.sendMail("test@yourcompany.com", "Subject Test", "Mail Content");
 }
}

 

----------------------------------------------------------

Where is  the ProxyConfig.java?

ProxyConfig.java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

/**
 * @author HappyQin
 */
public class ProxyConfig {
 private String httpProxyEnabled;
 private String httpProxyHost;
 private String httpProxyPort;
 private String httpNonProxyHosts;
 private String httpProxyAuthNeeded;
 private String httpProxyUser;
 private String httpProxyPwsd;

 private String socksProxyEnabled;
 private String socksProxyHost;
 private String socksProxyPort;
 private String socksNonProxyHosts;
 private String socksProxyAuthNeeded;
 private String socksProxyUser;
 private String socksProxyPwsd;

 Properties prop = new Properties();

 /**
  * @return Returns the httpNonProxyHosts.
  */
 public String getHttpNonProxyHosts() {
  return prop.getProperty("http.nonProxyHosts");
 }
 /**
  * @return Returns the httpProxyAuthNeeded.
  */
 public String getHttpProxyAuthNeeded() {
  return prop.getProperty("http.proxyAuthNeeded");
 }
 /**
  * @return Returns the httpProxyEnabled.
  */
 public String getHttpProxyEnabled() {
  return prop.getProperty("http.proxyEnabled");
 }
 /**
  * @return Returns the httpProxyHost.
  */
 public String getHttpProxyHost() {
  return prop.getProperty("http.proxyHost");
 }
 /**
  * @return Returns the httpProxyPort.
  */
 public String getHttpProxyPort() {
  return prop.getProperty("http.proxyPort");
 }
 /**
  * @return Returns the httpProxyPwsd.
  */
 public String getHttpProxyPwsd() {
  return prop.getProperty("http.proxyPwsd");
 }
 /**
  * @return Returns the httpProxyUser.
  */
 public String getHttpProxyUser() {
  return prop.getProperty("http.proxyUser");
 }

 /**
  * @return Returns the socksNonProxyHosts.
  */
 public String getSocksNonProxyHosts() {
  return prop.getProperty("socks.nonProxyHosts");
 }
 /**
  * @return Returns the socksProxyAuthNeeded.
  */
 public String getSocksProxyAuthNeeded() {
  return prop.getProperty("socks.proxyAuthNeeded");
 }
 /**
  * @return Returns the socksProxyEnabled.
  */
 public String getSocksProxyEnabled() {
  return prop.getProperty("socks.proxyEnabled");
 }
 /**
  * @return Returns the socksProxyHost.
  */
 public String getSocksProxyHost() {
  return prop.getProperty("socks.proxyHost");
 }
 /**
  * @return Returns the socksProxyPort.
  */
 public String getSocksProxyPort() {
  return prop.getProperty("socks.proxyPort");
 }
 /**
  * @return Returns the socksProxyPwsd.
  */
 public String getSocksProxyPwsd() {
  return prop.getProperty("socks.proxyPwsd");
 }
 /**
  * @return Returns the socksProxyUser.
  */
 public String getSocksProxyUser() {
  return prop.getProperty("socks.proxyUser");
 }
 /**
  * 
  */
 public ProxyConfig() {
  init();
 }

 private void init() {
  prop = new Properties();

  FileInputStream fis = null;
  try {
   URL url = getClass().getClassLoader().getResource(
     "proxy.config");
   if (url != null) {
    fis = new FileInputStream(url.getFile());
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  if (fis != null) {
   try {
    prop.load(fis);
   } catch (IOException e1) {
    e1.printStackTrace();
   }
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java 代理服务器代码示例: ```java import java.net.*; public class ProxyServer { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8080); while (true) { Socket clientSocket = serverSocket.accept(); Thread thread = new Thread(new ProxyThread(clientSocket)); thread.start(); } } } class ProxyThread implements Runnable { private Socket clientSocket; public ProxyThread(Socket clientSocket) { this.clientSocket = clientSocket; } public void run() { try { // 读取客户端请求 BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine; String url = ""; while ((inputLine = in.readLine()) != null) { if (inputLine.startsWith("GET ")) { url = inputLine.substring(4, inputLine.indexOf(" ", 4)); } } // 发送请求到目标服务器 URL targetUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection(); connection.setRequestMethod("GET"); // 将目标服务器响应转发给客户端 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String responseLine; OutputStreamWriter out = new OutputStreamWriter(clientSocket.getOutputStream()); while ((responseLine = reader.readLine()) != null) { out.write(responseLine + "\r\n"); } out.flush(); // 关闭连接 reader.close(); in.close(); out.close(); clientSocket.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个代理服务器监听 8080 端口,接受客户端请求并将请求转发到目标服务器。在这个示例中,我们只处理 HTTP GET 请求,并将目标服务器响应直接转发给客户端。你可以根据需要修改代码以处理其他类型的请求和响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值