smtp mail using socket(mail content is not displaying)
843790Aug 13 2008 — edited Aug 14 2008hi
I created a mail client using sockets.But the received mail doestnot show the mail body.code is given below.
import java.net.*;
import java.io.*;
import java.util.*;
public class EmailClient implements Runnable {
private Socket sc;
private InputStream is;
private OutputStream os;
private String from, to, subject, msg;
public EmailClient
(String from, String to, String subject, String msg) {
// parent = m;
this.from = from;
this.to = to;
this.subject = subject;
this.msg = msg;
}
public void run() {
try {System.out.println("hai");
sc=new Socket("hostname",25);
//sc = (Socket)Connector.open("socket://"+smtpServerAddress+":25");
is = sc.getInputStream();
os = sc.getOutputStream();
os.write(("HELO www.marinebiztv.com"+ "\r\n").getBytes());
os.write(("MAIL FROM: "+ from +"\r\n").getBytes());
os.write(("RCPT TO: "+ to + "\r\n").getBytes());
os.write("DATA\r\n".getBytes());
// stamp the msg with date
os.write(("Date: " + new Date() + "\r\n").getBytes());
os.write(("From: "+from+"\r\n").getBytes());
os.write(("To: "+to+"\r\n").getBytes());
os.write(("Subject: "+subject+"\r\n").getBytes());
os.write(("Hai hw r u\r\n").getBytes()); // message body
os.write(".\r\n".getBytes());
os.write("QUIT\r\n".getBytes());
// debug
StringBuffer sb = new StringBuffer();
int c = 0;
while (((c = is.read()) != -1) ) {
sb.append((char) c);
}
System.out.println(sb);
} catch(IOException e)
{
}
finally
{
try
{
if(is != null)
{
is.close();
}
if(os != null)
{
os.close();
}
}catch(Exception e){}
}
}
public static void main(String args[])
{
EmailClient email=new EmailClient("from","to","subject","message");
System.out.println("hai");
Thread t = new Thread(email);
t.start();
}
}
can anyone tell me whats the problem?