一个发送e-mail的程序,有点问题,大家帮忙看看

hewenhao 2007-09-07 01:32:00
rt!
程序如下:
import java.net.*;
import java.io.*;

public class SendMail
{
static PrintStream ps=null;
static DataInputStream dis=null;

public static void send(String str) throws IOException
{
ps.println(str);
ps.flush();
System.out.println("local send:"+str);
}

public static void receive() throws IOException
{
String readStr=dis.readLine();
System.out.println("STMP respose:"+readStr);
}

public static void main(String[] args)
{
String HELO="HELO";
String MAIL_FROM="MAIL_FROM: he.wenhao@163.com";
String RECP_TO="RCPT_TO: he.wenhao@163.com";
String SUBJECT="SUBJECT:Java is cool!";
String DATA="DATA";
String BODY="Java send this!\r\n.\r\n";
Socket smtp=null;

try
{
smtp=new Socket("smtp.163.com",25);
OutputStream os=smtp.getOutputStream();
ps=new PrintStream(os);
InputStream is=smtp.getInputStream();
dis=new DataInputStream(is);

}
catch(IOException e)
{
System.out.println(e);
}

try
{
String loc=InetAddress.getLocalHost().getHostName();
send(HELO+loc);
receive(); //得到smtp响应
send(MAIL_FROM); //从地址发出
receive(); //得到smtp响应
send(RECP_TO); //发送smtp给谁
receive(); //得到smtp响应
send(DATA); //发送smtp开始
receive(); //得到smtp响应
send(SUBJECT); //发送smtp标题
receive(); //得到smtp响应
send(BODY); //发信息主体
receive(); //得到smtp响应
smtp.close(); //关闭连接
}
catch(IOException e)
{ System.out.println("error");
}
System.out.println("send over");
}
}

输出结果如下:
local send:HELOHowell
STMP respose:220 163.com Anti-spam GT for Coremail System (163com[20050206])
local send:MAIL_FROM: he.wenhao@163.com
STMP respose:500 Error: bad syntax
local send:RCPT_TO: he.wenhao@163.com
STMP respose:503 Error: send HELO/EHLO first
local send:DATA
STMP respose:503 Error: send HELO/EHLO first
local send:SUBJECT:Java is cool!
STMP respose:503 Error: send HELO/EHLO first
local send:Java send this!
.

STMP respose:502 Error: command not implemented
send over
...全文
843 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
achao818 2007-09-07
  • 打赏
  • 举报
回复
up
hewenhao 2007-09-07
  • 打赏
  • 举报
回复
帮顶一下子

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧