设为首页
收藏本站
最近更新

文章搜索
本类热门

 

首页 >> 网络编程 >> JSP >> JSP实例 >> 新闻正文 [字体:  ] [打印文档
在servlet中直接写端口发送邮件的例子

文章作者:
责任编辑: 录入时间:2004-11-5 16:04:46 来源:
频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意. 

/**
*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()
{
}
}

推荐好友 | 频道收藏 | 打印文档 | 报告错误  
相关连接
·在应用中加入全文检索功能——基于Java的全文索引引擎Lucene简介
·JSP中的字符替换函数str_replace()实现
·JSP编程进度条设计实例
·使用JSP开发WebMail系统
·在线考试系统代码
·JSP记事本程序
·用JSP创建WAP应用
·简易成绩查询系统
同一专题
·无相关专题
发表评论 版权声明:除部分特别声明不要转载,或者授权我站独家播发的文章外,大家可以自由转载我站点的原创文章,但原作者和来自我站的链接必须保留(非我站原创的,按照原来自一节,自行链接)。文章版权归我站和作者共有
转载
要求转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印,亦不能抹去我站点水印。
共有评论查看评论
姓名: