1. SMTP Introduction
2. Sending Process
3. source code example
Package demo.net; import Java. AWT. borderlayout; import Java. AWT. gridlayout; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. io. ioexception; import Java. io. printwriter; import java.net. inetaddress; import java.net. socket; import Java. util. export; import javax. swing. *;/** sending email client */public class Sendmail extends jframe {private jtextfield from; private jtextfield to; private jtextfield server; private jtextarea message; private jtextarea comm; private region in; private printwriter out; Public Sendmail () {// set framesetsize (500,600); settitle ("send mail"); setdefaclocloseoperation (jframe. exit_on_close); setvisible (true); this. setlocationrelativeto (null); // Add the component jpanel p1 = new jpanel (New gridlayout (3, 2) to the frame; p1.add (New jlabel ("from :")); from = new jtextfield (20); p1.add (from); p1.add (New jlabel ("to:"); To = new jtextfield (20); p1.add (); p1.add (New jlabel ("SMTP server:"); server = new jtextfield (20); p1.add (server); add (P1, borderlayout. north); jpanel P2 = new jpanel (New gridlayout (2, 1); message = new jtextarea (); p2.add (New jscrollpane (Message )); comm = new jtextarea (); Comm. seteditable (false); p2.add (New jscrollpane (Comm); add (P2, borderlayout. center); jpanel P3 = new jpanel (); jbutton sendbutton = new jbutton ("send"); p3.add (sendbutton); add (P3, borderlayout. south); // register the listener sendbutton for the send button. addactionlistener (New actionlistener () {@ overridepublic void actionreceivmed (actionevent e) {comm. settext (""); Sendmail (server. gettext (). trim (), from. gettext (). trim (),. gettext (). trim (), message. gettext () ;}}) ;}// send public void Sendmail (string server, string from, string to, string message) {try {socket S = new socket (server, 25); try {In = new Sockets (S. getinputstream (); out = new printwriter (S. getoutputstream (), true/* Indicates automatic refresh */); string hostname = inetaddress. getlocalhost (). gethostname (); receive (); send ("hello" + hostname); // handshakes. If the handshake succeeds, 220 is returned, indicating that the service is ready. Receive (); send ("mail from: <"+ from +"> "); // sender email receive (); send (" rcpt to: <"+ to +"> "); // recipient's email address receive (); send ("data"); // indicates that the content of the email is receive (); send (Message ); // send your content to send (". "); receive (); send (" quit ");} finally {S. close () ;}} catch (ioexception e) {comm. append ("error:" + E) ;}// send private void send (string s) {comm. append (S + '\ n'); out. print (s); out. print ("\ r \ n"); out. flush ();} // receives private void receive () {comm. append (in. nextline () + '\ n');} public static void main (string [] ARGs) {New Sendmail ();}}