网页功能: 加入收藏 设为首页 网站搜索  
在servlet中直接写端口发送邮件的例子
发表日期:2004-07-26作者:[转贴] 出处:  


/**
*Class TestEmail
*@author yancheng(j-share.onchina.net)
*@version 1.0.0
*
* use write socket to send email.
*/
import javax.servlet.*;
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("<html>");
    out.println("<head><title>TestEmail</title></head>");
    out.println("<body>");
   
    String instr = null;
  boolean isOk = false;
     
    try{
    ou.println("HELO:smtp.163.net");
    out.println(in.readLine());

    ou.println("MAIL FROM:showtop@163.net");
    instr = in.readLine();
    out.println(instr);
    if(instr.startsWith("250")) isOk = true;
    else isOk=false;

  if(isOk){
    ou.println("RCPT TO:ycshowtop@21cn.com");
    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("<p>ok</p>");
       
    out.println("</body></html>");
  }
 
    public void destroy()
  {
  }
}


我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 在servlet中直接写端口发送邮件的例子
本类热点文章
  Java读取文件中含有中文的解决办法
  Java读取文件中含有中文的解决办法
  简单加密/解密方法包装, 含encode(),de..
  EJB 3.0规范全新体验
  java简单的获取windows系统网卡mac地址
  让Java程序带着JRE一起上路
  抢先体验"野马"J2SE6.0
  Java连接各种数据库的实例
  Java连接各种数据库的实例
  JAVA的XML编程实例解析
  Java学习从入门到精通(附FAQ)
  新手必读:Java学习的捷径
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.01111