Hi Tram
I have attached some code I put together by "borrowing" bits of code I have
found.
Shafiek
"Nguyen, Tram N." wrote:
> The following example is a very simple sendmail tool using 'mailto:'
> protocol, you can write your own mail handler for smtp protocol or other
> ultilizing net, io package .
>
> import java.io.*;
> import java.net.*;
>
> /**
> * This program sends e-mail using a mailto: URL
> **/
> public class SendMail {
>
> private String to, message;
> private String from, subject;
> final static String EmailServer = "company.com";
>
> private URL u;
> private URLConnection c;
> private PrintWriter out;
>
> public SendMail (String t, String m)
> {
> to = t;
>
> message = new String ("You mesage ");
> subject = new String ("Your subject");
> from = new String ("Sender name");
>
> }
> public int send(){
> int rtnStatus=0;
> try {
> System.getProperties().put("mail.host", EmailServer);
> u = new URL("mailto:" + to);
> c = u.openConnection();
> c.setDoInput(false);
> c.setDoOutput(true);
> System.out.flush();
> c.connect();
> out = new PrintWriter(new OutputStreamWriter(c.getOutputStream()));
>
> out.println ("From: \"" + from + "\" <");
> out.println ("To: " + to);
> out.println ("Subject: " + subject);
> out.println ();
>
> out.println (message);
>
> //out.close();
> //System.out.println("Message was sent");
> System.out.flush();
> rtnStatus = 1; //if sucessful return 1
> }catch (Exception e)
> {
> System.err.println(e);
> }finally {out.close();return rtnStatus;}
> }
> }
>
> > -----Original Message-----
> > From: Matt Zagni [SMTP:[EMAIL PROTECTED]]
> > Sent: Thursday, October 22, 1998 3:45 AM
> > To: [EMAIL PROTECTED]
> > Subject: request for a sendmail java script
> >
> >
> > Hi,
> >
> > Please could someone mail me an example of a sendmail java class file
> > and .java file or even a url where examples are located.
> >
> > Many thanks
> >
> > Matt
mm
package au.com.savahl.util;
import java.io.*;
import java.net.*;
// ------------ My util files
import au.com.savahl.personal.util.*;
public class JavaMail
implements ThreadExceptionListenerI
{
public void sendMail(String subject,
String destination,
String message,
String from,
String mailServerName)
throws IOException,
ProtocolException,
UnknownHostException
{
SendMailThread sendMailThread = new SendMailThread(subject,
destination,
message,
from,
mailServerName);
SmartThread theThread = new SmartThread(sendMailThread,destination);
theThread.addThreadExceptionListener(this);
theThread.start();
}
//This method is called whenever an exception occurs on a secondary thread that
this object has
//been registered a listener for.
public void exceptionOccured(Runnable sourceRunnable,
Thread sourceThread,
Throwable threadException)
{
//If we get a CheckedThreadException, we need to figure out exactly what type
of exception it is.
if (threadException instanceof CheckedThreadException)
{
System.out.println("Thread: "+ Thread.currentThread().getName() +
". Caught CheckedThreadException "+
((CheckedThreadException)threadException).getThreadException());
if (((CheckedThreadException)threadException).getThreadException()
instanceof ProtocolException)
{
System.out.println("Thread: "+ Thread.currentThread().getName() + "
Fail");
}
if (((CheckedThreadException)threadException).getThreadException()
instanceof UnknownHostException)
{
System.out.println("Thread: "+ Thread.currentThread().getName() + "
Fail");
}
if (((CheckedThreadException)threadException).getThreadException()
instanceof IOException)
{
System.out.println("Thread: "+ Thread.currentThread().getName() + "
Fail");
}
}
else
{
System.out.println("Thread: "+ Thread.currentThread().getName() + ".
Caught exception "+ threadException);
//Call back into our runnable object to execute some cleanup code before
this secondary thread
//is terminated by the system.
System.out.println("Thread: "+ Thread.currentThread().getName() +
". Calling cleanupOnException()");
((SendMailThread)sourceRunnable).cleanupOnException(threadException);
}
}
}
class SendMailThread
implements Runnable,
ThreadExceptionCleanupI
{
private PrintWriter send = null;
private DataInputStream reply = null;
private String subject = null;
private String destination = null;
private String message = null;
private String from = null;
private String mailServer = null;
private static final int DEFAULT_PORT = 25;
private static final String EOL = "\r\n"; // network end of line
private Socket sock = null;
public SendMailThread(String m_subject,
String m_destination,
String m_message,
String m_from,
String m_mailServer)
{
subject = m_subject;
destination = m_destination;
message = m_message;
from = m_from;
mailServer = m_mailServer;
}
public void run()
{
try
{
connectToMailServer(mailServer);
sendMessage();
disconnectFromMailServer();
}
catch(ProtocolException pe)
{
System.out.println("ProtocolException " + pe.toString());
pe.printStackTrace(System.out);
throw new CheckedThreadException(Thread.currentThread(), pe);
}
catch(UnknownHostException uhe)
{
System.out.println("UnknownHostException " + uhe.toString());
uhe.printStackTrace(System.out);
throw new CheckedThreadException(Thread.currentThread(), uhe);
}
catch(IOException ioe)
{
System.out.println("IOException " + ioe.toString());
ioe.printStackTrace(System.out);
throw new CheckedThreadException(Thread.currentThread(), ioe);
}
}
public void sendMessage()
throws ProtocolException, UnknownHostException, IOException
{
String rstr;
String sstr;
InetAddress local;
try {
local = InetAddress.getLocalHost();
}
catch (UnknownHostException ioe) {
System.err.println("No local IP address found - is your network up?");
throw ioe;
}
String host = local.getHostName();
send.print("HELO " + host);
send.print(EOL);
send.flush();
rstr = reply.readLine();
if (!rstr.startsWith("250"))
throw new ProtocolException(rstr);
sstr = "MAIL FROM: " + from;
send.print(sstr);
send.print(EOL);
send.flush();
rstr = reply.readLine();
if (!rstr.startsWith("250"))
throw new ProtocolException(rstr);
sstr = "RCPT TO: " + destination;
send.print(sstr);
send.print(EOL);
send.flush();
rstr = reply.readLine();
if (!rstr.startsWith("250"))
throw new ProtocolException(rstr);
send.print("DATA");
send.print(EOL);
send.flush();
rstr = reply.readLine();
if (!rstr.startsWith("354"))
throw new ProtocolException(rstr);
send.print("From: " + from);
send.print(EOL);
send.print("To: " + destination);
send.print(EOL);
send.print("Subject: " + subject);
send.print(EOL);
// Sending a blank line ends the header part.
send.print(EOL);
// Now send the message proper
send.print(message);
send.print(EOL);
send.print(".");
send.print(EOL);
send.flush();
}
public void cleanupOnException(Throwable threadException)
{
disconnectFromMailServer();
}
private void connectToMailServer(String mailServerName)
throws ProtocolException, IOException
{
sock = new Socket( mailServerName, DEFAULT_PORT);
reply = new DataInputStream( sock.getInputStream() );
send = new PrintWriter( sock.getOutputStream() );
String rstr = reply.readLine();
if (!rstr.startsWith("220"))
throw new ProtocolException(rstr);
while (rstr.indexOf('-') == 3)
{
rstr = reply.readLine();
if (!rstr.startsWith("220")) throw new ProtocolException(rstr);
}
}
private void disconnectFromMailServer()
{
try
{
send.print("QUIT");
send.print(EOL);
send.flush();
sock.close();
}
catch (IOException ioe)
{
// As though there's anything I can do about it now...
}
}
}