Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Feb 13, 2002

Send an HTML Email Without Using the JavaMail API


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
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date