public void converse(String sender,String recipients,String subject,String body) /*throw SMTPException*/{
if(!expect_reply("220")) die(/*EX_PROTOCOL*/4,"did not get SMTP greeting");
send_cmd("HELO","[email protected]"); if(!expect_reply("250")) die(/*EX_PROTOCOL*/4,"did not get ack our HELO"); send_cmd("RSET"); if(!expect_reply("250")) die (/*EX_PROTOCOL*/4,"not reset");
send_cmd("AUTH LOGIN"); if(!expect_reply("334")) die (/*EX_PROTOCOL*/4,"not reset");
send_cmd(enc.encode(user.getBytes())); if(!expect_reply("334")) die (/*EX_PROTOCOL*/4,"not reset");
send_cmd(enc.encode(psw.getBytes())); if(!expect_reply("334")) die (/*EX_PROTOCOL*/4,"not reset");
send_cmd("MAIL","From:<"+sender+">"); if(!expect_reply("250")) die (/*EX_PROTOCOL*/4,"did not ack our MAIL command");
import java.net.*;
import java.io.*;
import java.util.*;
//import javax.mail.*;
//import javax.mail.internet.*;public class SmtpTalk {
BufferedReader is;
PrintStream os;
private boolean debug=true;
private String host;
sun.misc.BASE64Encoder enc;
private String user;
private String psw;
public static void main(String[] argv){
if(argv.length!=2){
System.out.println("Usage:java SmtpTalk host user");
System.exit(1);
}
try{
SmtpTalk st=new SmtpTalk(argv[0]);
System.out.println("Smtp Talker ready");
st.converse("[email protected]",argv[1],"Test message",
"helo there");
}catch(Exception ig){
System.err.println(ig.getMessage());
System.exit(1);
}
}
SmtpTalk(String Server){
host=Server;
enc = new sun.misc.BASE64Encoder();
user=new String("madass");
psw=new String ("xinxin");
try{
Socket s=new Socket(host,25);
is=new BufferedReader(new InputStreamReader(s.getInputStream()));
os=new PrintStream(s.getOutputStream());
}
catch(NoRouteToHostException e){
die(/*EX_TEMPFAIL*/1,"No route to host "+host);
}
catch(ConnectException e){
die(/*EX_TEMPFAIL*/1,"Connection Refused by"+host);
}
catch(UnknownHostException e){
die(/*EX_NOHOST*/2,"Unknown host "+host);
}
catch(IOException e){
die(/*EX_IOERR*/3,"I/O error setting up socket stream\n"+e);
}
}
protected void send_cmd (String cmd,String oprnd){
send_cmd(cmd+" "+oprnd);
}
protected void send_cmd(String cmd){
if(debug)
System.out.println(">>>"+cmd);
os.print(cmd+"\r\n");
}
public void send_text(String text){
os.print(text+"\r\n");
}
protected boolean expect_reply(String rspNum) /*throw SMTPException*/{
String s=null;
try{
s=is.readLine();
}catch(IOException e){
die(/*EX_IOERR*/3,"I/o error reading from host"+host+" "+e);
}
if(debug) System.out.println("<<<"+s);
return s.startsWith(rspNum+" ");
}
protected void die(int ret,String msg)/* throw SMTPException*/{
//throw new SMTPException (ret,msg);
}
public void converse(String sender,String recipients,String subject,String body) /*throw SMTPException*/{
if(!expect_reply("220")) die(/*EX_PROTOCOL*/4,"did not get SMTP greeting");
send_cmd("HELO","[email protected]");
if(!expect_reply("250")) die(/*EX_PROTOCOL*/4,"did not get ack our HELO");
send_cmd("RSET");
if(!expect_reply("250")) die (/*EX_PROTOCOL*/4,"not reset");
send_cmd("AUTH LOGIN");
if(!expect_reply("334")) die (/*EX_PROTOCOL*/4,"not reset");
send_cmd(enc.encode(user.getBytes()));
if(!expect_reply("334")) die (/*EX_PROTOCOL*/4,"not reset");
send_cmd(enc.encode(psw.getBytes()));
if(!expect_reply("334")) die (/*EX_PROTOCOL*/4,"not reset");
send_cmd("MAIL","From:<"+sender+">");
if(!expect_reply("250")) die (/*EX_PROTOCOL*/4,"did not ack our MAIL command");
//StringTokenizer st=new StringTokenizer(recipients);
//while(st.hasMoreTokens()){
//String r=st.nextToken();
send_cmd("RCPT","To:<"+recipients+">");
if(!expect_reply("250"))
die(/*EX_PROTOCOL*/4,"did not ack RECP");
//}
send_cmd("DATA");
if(!expect_reply("354")) die(/*EX_PROTOCOL*/4,"did not want our data");
send_text("From: "+sender);
send_text("To: "+recipients);
send_text("Subject: "+subject);
send_text("");
send_text(body+"\r");
send_cmd(".");
if(!expect_reply("250")) die(/*EX_PROTOCOL*/4,"mail not accepted");
send_cmd("QUIT");
if(!expect_reply("221")) die(/*EX_PROTOCOL*/4,"Other end not closing down");
}
}