一个发送e-mail的程序,有点问题,大家帮忙看看
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
[解决办法]
up