在servlet中直接寫埠傳送郵件的例子 (轉)

worldblog發表於2007-12-04
在servlet中直接寫埠傳送郵件的例子 (轉)[@more@]

/**
*Class TestE
*@author yancheng(j-share.onchina)
*@version 1.0.0
*
* use write socket to send .
*/
import x..*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;

public class TestEmail extends HttpServlet
{
 
  private static final String CONTENT_TYPE = "text/html";
 
 
  public void init(ServletConfig config) throws ServletException
  {
  super.init(config);
  }
 
  public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
  {
  response.setContentType(CONTENT_TYPE);
 
  PrintWriter out = response.getWriter();
 
   Socket s = new Socket("smtp.163.net",25);
   
   PrintWriter ou = new PrintWriter(s.getOutputStream(),true);
   BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
     
  out.println("");
  out.println("

TestEmail");
  out.println("");
 
  String instr = null;
 boolean isOk = false;
 
  try{
  ou.println("HELO:smtp.163.net");
  out.println(in.readLine());

  ou.println("MAIL ;
  instr = in.readLine();
  out.println(instr);
  if(instr.startsWith("250")) isOk = true;
  else isOk=false;

 if(isOk){
  ou.println("RCPT ;
  instr = in.readLine();
  out.println(instr);
  if(instr.startsWith("250"))  isOk=true;
  else isOk = false;
 }
 
 if(isOk){
  ou.println("DATA");
  instr = in.readLine();
  out.println(instr);
  if(instr.startsWith("250"))  isOk = true;
  else isOk=false;
 }
 
  ou.println("這是一個測試");
  ou.println(".");
  instr = in.readLine();
  out.println(instr);
  if(instr.startsWith("250"))  isOk=true;
  else isOk=false;
 ou.println("QUIT");
  s.close();
   }
   catch(IOException x)
   {
   x.printStackTrace(System.err);
   }
 
 
  out.println("

ok

");
 
  out.println("");
  }
 
  public void destroy()
  {
  }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987861/,如需轉載,請註明出處,否則將追究法律責任。

相關文章