Java Network Programming 3: SMTP-based mail sending client

Source: Internet
Author: User

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 ();}}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.