Send an email without using
JavaMail API, by using the Java Core packages(java.net.*;java.io.*) to
pass three parameters. The first parameter is the SMTP mail server, the
second parameter is the Sender Address (From address), and the third
parameter is the Recipent Address (To Address).
Here's the code:
import java.net.Socket;
import java.io.*;
public class Test
{
static BufferedReader br;
static OutputStream os;
public static void main(String asdf[])
{
try
{
if(asdf.length == 3)
{
Socket socket=new Socket(asdf[0],25);
br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
os = socket.getOutputStream();
smtpCommand("HELO "+asdf[0]);
smtpCommand("MAIL FROM:"+asdf[1]);
smtpCommand("RCPT TO:"+asdf[2]);
smtpCommand("DATA");
smtpCommand("From:"+asdf[1]+
"\nTo: "+asdf[2]+
"\nContent-Type:text/html;\nSubject:"+
" test\n<HTML><B>This is in BOLD tag</B><BR><I>"+
"This is in Italic tag</I><BR>"+
"<U>This is in UnderLine Tag</U></HTML>\n.\n");
System.out.println("\nMessage Sent Successfully to"
+asdf[1].substring(0,asdf[1].indexOf("@")));
}
else
{
System.out.println("\nERROR\n\nUsage : java <FileName>"+
" <SMTP Server IP> <From Address> <Senders Address>");
System.out.println("\nExample : java Test smtp.mail.yahoo.com"+
" my_id@yahoo.com your_id@yahoo.com");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
// This method will communicate with SMTP server
private static void smtpCommand(String command) _
throws Exception
{
br.readLine();
os.write((command+"\r\n").getBytes());
}
}
Pulavarthy
Satish Kumar