Hi
I am giving the SMTP implementation.
------------------------------------------------------------
/*=======================================================================
*Name of the Bean file: SendMail.java
*Description: This bean acceps the server and other mail parameters from
the jsp.
*It then connects to the SMTP server and send a mail. A sample mail is
send in the following format.
* telnet main.xxx 25
* helo main.xxx
* mail from: <gandhi@m...>
* rcpt to: <gandhi@m...> //For To
* rcpt to: <gandhi@m...> /For CC
* data
* From: email address
* Subject: subject of the mail
* To: email address
* Cc: email address
* Content-Type: text/html
* mail message
* .
* quit
*
*@Date: 1 st Aug 2000
*Version: 1.0
*Author : Vikas Gandhi <gandhivikas@y...>
*Modification history:
*MM/DD/YYYY NAME Description
=========================================================================*/
package com.xxxinternet.util;
import java.io.*;
import java.net.*;
public class SendMail
{
//Define all the variables that have to be used to establish a
connection and mail.
private Socket smtpPipe = null; //Variable for socket connecion
private DataInputStream inn = null; //Variable for input steam
private DataOutputStream outt = null; //Variable for output stream
private String StrMessage = null; //Variable for message
private String StrSubject = null; //Variable for subject
private String StrFrom = null; //Variable for from
private String StrTo = ""; //Variable for To
private String StrCc=""; //Variable for CC
private boolean BHtml = false; //Variable for checking whether the
page is html or not
private String StrMailserver = null; //Variable for mailserver
//Function that return StrFrom variable
public String getFrom()
{
return StrFrom;
}
//Function that return StrMailserve variable
public String getMailserver()
{
return StrMailserver;
}
//Function that return BHtml variable
public boolean getHtml()
{
return BHtml;
}
//Function that return StrMessage variable
public String getMessage()
{
return StrMessage;
}
//Function that return StrSubject variable
public String getSubject()
{
return StrSubject;
}
//Function that return StrTo variable
public String getTo()
{
return StrTo;
}
//Function that return StrCc variable
public String getCC()
{
return StrCc;
}
/*
Make a connection with smtp server identified by StrMailserver on port
25
If proper connection is established datainput stream (inn) and
dataoutputstream (outt) thru
socket connection. Next Send "helo " and name of the smtp server to the
outt. Recieve status code
If status code is 250 then ok else return false.
*/
public boolean isConnect()
{
String lStrIn = null;
try
{
smtpPipe = new Socket(StrMailserver, 25); //Make a new smtp
connection on port 25
}
catch(IOException ioexception)
{
System.out.println(" Error " + ioexception);
return false;
}
if(smtpPipe == null)
return false;
try
{
//Establish new input and output streams with the socket connection
inn = new DataInputStream(new
BufferedInputStream(smtpPipe.getInputStream()));
outt = new DataOutputStream(new
BufferedOutputStream(smtpPipe.getOutputStream()));
}
catch(IOException _ex)
{
System.out.println(" In false in 1");
return false;
}
//If connection is not established, return false
if(inn == null || outt == null)
{
System.out.println(" In false in 2");
return false;
}
try
{
lStrIn = inn.readLine(); //Read status code returned by
server
System.out.println("Message" + lStrIn);
}
catch(IOException ioexception1)
{
System.out.println("Err 2" + ioexception1);
}
try
{
outt.writeBytes("helo "+ StrMailserver+"\r\n");//Write helo
and server name to the output stream
outt.flush();
}
catch(IOException ioexception2)
{
System.out.println("error" + ioexception2);
}
try
{
lStrIn = inn.readLine();
System.out.println("Message" + lStrIn); //Read status code
returned by server
}
catch(IOException _ex) { }
return lStrIn.trim().startsWith("250");
}
/*Write "DATA\r\n" to the output stream to the output stream.
This sends message body and data to the reciepient
*/
public boolean isDataSend()
{
try
{
outt.writeBytes("DATA\r\n");
outt.flush();
}
catch(IOException _ex) { }
String s = null;
try
{
s = inn.readLine();
System.out.println(s);
}
catch(IOException _ex) { }
return s.trim().startsWith("354");
}
//Write mail contents after "data :" to he output stream
public boolean isMailContents(String msg)
{
try
{
String lStrMsg="";
lStrMsg = "From: " + StrFrom + "\r\n" + "To: " +
StrTo+"\r\n";
lStrMsg=lStrMsg+ "Cc: " + StrCc + "\r\n" + "Subject: " +
StrSubject + "\r\n";
//Check whether the mail has to be send in html format or
not.
//If yes then write the content type to be
if (BHtml)
{
lStrMsg = lStrMsg+"Content-Type: text/html"+"\r\n\r\n";
}
lStrMsg=lStrMsg+msg;
outt.writeBytes(lStrMsg);
outt.flush();
}
catch(IOException _ex) { }
return true;
}
//End the mail by writing "\r\n.\r\nquit" to the output stream
public boolean isMailEnd()
{
try
{
outt.writeBytes("\r\n.\r\nquit");
outt.flush();
}
catch(IOException _ex) { }
String s = null;
try
{
s = inn.readLine();
System.out.println(s);
}
catch(IOException _ex) { }
return s.trim().startsWith("250");
}
/*Write "mail from" to he output stream
Then read return status code. If code is 250 then it is ok */
public boolean isMailFrom(String lStrFrom)
{
try
{
outt.writeBytes("mail from: <" + lStrFrom + ">\r\n");
outt.flush();
}
catch(IOException _ex) { }
/*Read return status code. If code is 250 then it is ok and
return true else return false*/
String lStrIn = null;
try
{
lStrIn = inn.readLine();
System.out.println(lStrIn);
}
catch(IOException _ex) { }
return lStrIn.trim().startsWith("250");
}
/*Write "rcpt to" to the output stream (Whether it is from cc or to)
Then read return status code. If code is 250 then it is ok */
public boolean isRCPT(String lStrMailAdd)
{
try
{
outt.writeBytes("rcpt to: <" + lStrMailAdd + ">\r\n");
outt.flush();
}
catch(IOException ioexception)
{
System.out.println(" RCPT error " + ioexception);
}
/*Read return status code. If code is 250 then it is ok and
return true else return false*/
String lStrIn = null;
try
{
lStrIn = inn.readLine();
System.out.println(lStrIn);
}
catch(IOException _ex) { }
return lStrIn.trim().startsWith("250");
}
/*Make the connection and send the mail to the reciepient
Check if CC has any value then mak the mail to cc also.*/
public boolean isSendMail()
{
if (StrCc.equals("")) //Do not send the mail to CC
{
return isConnect() && isMailFrom(StrFrom) && isRCPT(StrTo) &&
isSubject(StrSubject) && isDataSend() && isMailContents(StrMessage) &&
isMailEnd();
}
else //Mail to CC also
{
return isConnect() && isMailFrom(StrFrom) && isRCPT(StrTo) &&
isRCPT(StrCc) && isSubject(StrSubject) && isDataSend() &&
isMailContents(StrMessage) && isMailEnd();
}
}
//Write the value of the subject on the socket
public boolean isSubject(String lStrSubject)
{
try
{
outt.writeBytes("Subject:" + lStrSubject + "\r\n");
outt.flush();
}
catch(IOException _ex) { }
try
{
String lStrRead = inn.readLine();
System.out.println(lStrRead);
}
catch(IOException _ex) { }
return true;
}
//Set the value of the private variable StrFrom
public void setFrom(String lStrFrom)
{
StrFrom = lStrFrom;
}
//Set the value of the private variable StrMailserver
public void setMailserver(String lStrMailserver)
{
StrMailserver = lStrMailserver;
}
//Set the value of the private variable StrSubject
public void setMessage(String lStrMessage)
{
StrMessage = lStrMessage;
}
//Set the value of the private variable StrSubject
public void setSubject(String lStrSubject)
{
StrSubject = lStrSubject;
}
//Set the value of the private variable StrTo
public void setTo(String lStrTo)
{
StrTo = lStrTo;
}
//Set the value of the private variable StrCc
public void setCc(String lStrCc)
{
StrCc = lStrCc;
}
//Set the value of the private boolean variable BHtml which sets
whether the mail is in HTMl format or not
public void setHtml(boolean BHtm)
{
BHtml = BHtm;
}
}
-------------------------------------------------------------
ghkazi wrote:
>
> will u plz send me the links regarding java servlet implementation in SMTP
> or FTP or anyother protocol than HTTP
>
> ----- Original Message -----
> From: Dmitry <dnevozhai@f...>
> To: Servlets <servlets@p...>
> Sent: Monday, June 25, 2001 11:34 AM
> Subject: [servlets] Re: Java mail
>
> > Hi, Ajay.
> > I have some links for you (several pure java implementations of SMTP,
> POP3,
> > IMAP). If you are still interested in this stuff, feel free to mail me.
> And also I have a
> > question for you too, may be you will help me...
> >
> > Regards,
> > Dmitry.
>