一个简单的发送信息到邮箱的程序

04-08-17 zhencai

各位朋友!麻烦过目 一个实现发送信息到邮箱的程序,请指出错误!

import java.io.*;

import java.net.*;

import java.util.*;

public class sendMail extends Object

{

private Socket socket;

private PrintWriter mailOut;

private BufferedReader mailIn;

//发送命令到主机的方法sendcommend()

public static void sendCommend (Parameters param,String cmd) throws IOException

{

param.mailOut.print(cmd+"\r\n");

param.mailOut.flush();

String line=param.mailIn.readLine();

}

//发送数据到主机的方法sendData()

public static void sendData(Parameters param,String to,String subject,String message)

{ message="to:"+to+"\r\n"+"subject:"+subject+"\r\n"+message;

param.mailOut.print(message+"\r\n\r\n");

param.mailOut.flush();

}

public static void main(String[] str) throws UnknownHostException,IOException

{ String host="smtp.263.net", //SMTP服务器是 smtp.263.net

from="maicai305@tom.com", //发送方

to="mikchain@yahoo.com.cn", //接受方

name="maicai",

subject="happy! from experiment!",//主题

message="留言:"+name+"test sendmail!";//内容

Parameters param=new Parameters();

param.socket=new Socket(host,25);

param.mailOut=new PrintWriter(param.socket.getOutputStream(),true);

param.mailIn=new BufferedReader( new InputStreamReader( param.soccket.getInputStream()));

sendCommend(param,"HELLO"+host);//命令为“HELLO”+host

sendCommend(param,"Mail from:"+from);//命令为:"Mail from:"+from

sendCommend(param,"Rcpt to:"+to); //命令为:"Rcpt to:"+to

sendData(param,to,subject,message);要发送数据的接受方,主题,内容依次为: to,subject,message

sendCommend(param,"QUIT");//命令:QUIT

param.socket.close();//关闭所指定的socket

}

}

banq
2004-08-21 17:34
不要在J2EE中使用线程,使用JMS,参考我的书籍

猜你喜欢