import java.awt.*; import javax.swing.*;
/** * example program from chapter 1 programming spiders, bots and aggregators in * java copyright 2001 by jeff heaton * * sendmail is an example of client sockets. this program presents a simple * dialog box that prompts the user for information about how to send a mail. * * @author jeff heaton * @version 1.0 */
/* * using the smtp program * * to use the program in listing 1.2, you must know the address of an smtp * server usually provided by your isp. if you are unsure of your smtp server, * you should contact your isp's customer service. in order for outbound e-mail * messages to be sent, your e-mail program must have this address. once it * does, you can enter who is sending the e-mail (if you are sending it, you * would type your e-mail address in) and who will be on the receiving end. this * is usually entered under the reply to field of your e-mail program. both of * these addresses must be valid. if they are invalid, the e-mail may not be * sent. after you have entered these addresses, you should continue by entering * the subject, writing the actual message, and then clicking send. note * * for more information on how to compile examples in this book, see appendix e * 'how to compile examples under windows.' * * as stated earlier, to send an e-mail with this program, you must enter who is * sending the message. you may be thinking that you could enter any e-mail * address you want here, right? yes, this is true; as long as the smtp server * allows it, this program will allow you to impersonate anyone you enter into * the to address field. however, as previously stated, a savvy internet user * can tell whether the e-mail address is fake. * * after the mention of possible misrepresentation of identity on the sender's * end, you may now be asking yourself, is this program dangerous? this * program is no more dangerous than any e-mail client (such as microsoft * outlook express or eudora) that also requires you to tell it who you are. in * general, all e-mail programs must request both your identity and that of the * smtp server. examining the smtp server * * you will now be shown how this program works. we will begin by looking at how * a client socket is created. when the client socket is first instantiated, you * must specify two parameters. first, you must specify the host to connect to; * second, you must specify the port number (e.g., 80) you would like to connect * on. these two items are generally passed into the constructor. the following * line of code (from listing 1.2) accomplishes this: * * java.net.socket s =new java.net.socket( _smtp.gettext(),25 ); * * this line of code creates a new socket, named s. the first parameter to the * constructor, _smtp .gettext(), specifies the address to connect to. here it * is being read directly from a text field. the second parameter specifies the * port to connect to. (the port for smtp is 25.) table 1.1 shows a listing of * the ports associated with most internet services. the hostname is retrieved * from the _smtp class level variable, which is the jtextfield control that the * smtp hostname is entered into. * * if any errors occur while you are making the connection to the specified * host, the socket constructor will throw an ioexception. once this connection * is made, input and output streams are obtained from the socket.getinputstream * and socket.getoutputstream methods. this is done with the following lines of * code from listing 1.2: * * _out = new java.io.printwriter(s.getoutputstream()); * _in = new java.io.bufferedreader(new java.io.inputstreamreader(s.getinputstream())); * * these low-level stream types are only capable of reading binary data. because * this data is needed in text format, filters are used to wrap the lower-level * input and output streams obtained from the socket. * * in the code above, the output stream has been wrapped in a printwriter * object. this is because printwriter allows the program to output text to the * socket in a similar manner to the way an application would write data to the * system.out object by using the print and println methods. the application * presented here uses the println method to send commands to the smtp server. * as you can see in the code, the inputstream object has also been wrapped; in * this case, it has been wrapped in a bufferedreader. before this could happen, * however, this object must first have been wrapped in an inputstreamreader * object as shown here: * * _in = new java.io.bufferedreader(new * java.io.inputstreamreader(s.getinputstream())); * * this is done because the bufferedreader object provides reads that are made * up of lines of text instead of individual bytes. this way, the program can * read text up to a carriage return without having to parse the individual * characters. this is done with the readline method. * * you will now be shown how each command is sent to the smtp server. each of * these commands that is sent results in a response being issued from the smtp * server. for the protocol to work correctly, each response must be read by the * smtp client program. these responses start with a number and then they give a * textual description of what the result was. a full-featured smtp client * should examine these codes and ensure that no error has occurred. * * for the purposes of the sendmail example, we will simple ignore these * responses because most are informational and not needed. instead, for our * purposes, the response will be read in and displayed to the _output list box. * commands that have been sent to the server are displayed in this list with a * c: prefix to indicate that they are from the client. responses returned from * the smtp server will be displayed with the s: prefix. * * to accomplish this, the example program will use the send method. the send * method accepts a single string parameter to indicate the smtp command to be * issued. once this command is sent, the send method awaits a response from the * smtp host. the portion of listing 1.2 that contains the send method is * displayed here: * * protected void send(string s) throws java.io.ioexception { // send the smtp command if(s!=null) { _model.addelement("c:"+s); _out.println(s); _out.flush(); } // wait for the response string line = _in.readline(); if(line!=null) { _model.addelement("s:"+line); } } * * as you can see, the send method does not handle the exceptions that might * occur from its commands. instead, they are thrown to the calling method as * indicated by the throws clause of the function declaration. the variable s is * checked to see if it is null. if s is null, then no command is to be sent and * only a response is sought. if s is not null, then the value of s is logged * and then sent to the socket. after this happens, the flush command is given * to the socket to ensure that the command was actually sent and not just * buffered. once the command is sent, the readline method is called to await * the response from the server. if a response is sent, then it is logged. * * once the socket is created and the input and output objects are created, the * smtp session can begin. the following commands manage the entire smtp * session: * * send(null); * send("helo " + java.net.inetaddress.getlocalhost().gethostname() ); * send("mail from: " + _from.gettext() ); send("rcpt to: " + _to.gettext() ); * send("data"); * _out.println("subject:" + _subject.gettext()); _out.println( * _body.gettext() ); send("."); s.close(); * * tip * * refer to table 1.4 in the preceding section to review the details of what * each of the smtp commands actually means. * * the rest of the sendmail program (as seen in listing 1.2) is a typical swing * application. the graphical user interface (gui) layout for this application * was created using visualcaf?. the visualcaf? comments have been left in to * allow the form's gui layout to be edited by visualcaf? if you are using it. * if you are using an environment other than visualcaf?, you may safely delete * the visualcaf? comments (lines starting in //). the visualcaf? code only * consists of comments and does not need to be deleted to run on other * platforms. * */
public class sendmail extends javax.swing.jframe {
/** * the constructor. do all basic setup for this application. */ public sendmail() { //{{init_controls settitle("sendmail example"); getcontentpane().setlayout(null); setsize(736, 312); setvisible(false); jlabel1.settext("from:"); getcontentpane().add(jlabel1); jlabel1.setbounds(12, 12, 36, 12); jlabel2.settext("to:"); getcontentpane().add(jlabel2); jlabel2.setbounds(12, 48, 36, 12); jlabel3.settext("subject:"); getcontentpane().add(jlabel3); jlabel3.setbounds(12, 84, 48, 12); jlabel4.settext("smtp server:"); getcontentpane().add(jlabel4); jlabel4.setbounds(12, 120, 84, 12); getcontentpane().add(_from); _from.setbounds(96, 12, 300, 24); getcontentpane().add(_to); _to.setbounds(96, 48, 300, 24); getcontentpane().add(_subject); _subject.setbounds(96, 84, 300, 24); getcontentpane().add(_smtp); _smtp.setbounds(96, 120, 300, 24); getcontentpane().add(_scrollpane2); _scrollpane2.setbounds(12, 156, 384, 108); _body.settext("enter your message here."); _scrollpane2.getviewport().add(_body); _body.setbounds(0, 0, 381, 105); send.settext("send"); send.setactioncommand("send"); getcontentpane().add(send); send.setbounds(60, 276, 132, 24); cancel.settext("cancel"); cancel.setactioncommand("cancel"); getcontentpane().add(cancel); cancel.setbounds(216, 276, 120, 24); getcontentpane().add(_scrollpane); _scrollpane.setbounds(408, 12, 312, 288); getcontentpane().add(_output); _output.setbounds(408, 12, 309, 285); //}}
//{{init_menus //}}
//{{register_listeners symaction lsymaction = new symaction(); send.addactionlistener(lsymaction); cancel.addactionlistener(lsymaction); //}}
_output.setmodel(_model); _model.addelement("server output displayed here:"); _scrollpane.getviewport().setview(_output); _scrollpane2.getviewport().setview(_body); }
/** * moves the app to the correct position when it is made visible. * * @param b * true to make visible, false to make invisible. */ public void setvisible(boolean b) { if (b) setlocation(50, 50); super.setvisible(b); }
/** * the main function basically just creates a new object, then shows it. * * @param args * command line arguments. not used in this application. */ static public void main(string args[]) { (new sendmail()).show(); }
/** * created by visualcafe. sets the window size. */ public void addnotify() { // record the size of the window prior to // calling parents addnotify. dimension size = getsize();
super.addnotify();
if (framesizeadjusted) return; framesizeadjusted = true;
// adjust size of frame according to the // insets and menu bar insets insets = getinsets(); javax.swing.jmenubar menubar = getrootpane().getjmenubar(); int menubarheight = 0; if (menubar != null) menubarheight = menubar.getpreferredsize().height; setsize(insets.left + insets.right + size.width, insets.top + insets.bottom + size.height + menubarheight); }
// used by addnotify boolean framesizeadjusted = false;
//{{declare_controls
/** * a label. */ javax.swing.jlabel jlabel1 = new javax.swing.jlabel();
/** * a label. */ javax.swing.jlabel jlabel2 = new javax.swing.jlabel();
/** * a label. */ javax.swing.jlabel jlabel3 = new javax.swing.jlabel();
/** * a label. */ javax.swing.jlabel jlabel4 = new javax.swing.jlabel();
/** * who this message is from. */ javax.swing.jtextfield _from = new javax.swing.jtextfield();
/** * who this message is to. */ javax.swing.jtextfield _to = new javax.swing.jtextfield();
/** * the subject of this message. */ javax.swing.jtextfield _subject = new javax.swing.jtextfield();
/** * the smtp server to use to send this message. */ javax.swing.jtextfield _smtp = new javax.swing.jtextfield();
/** * a scroll pane. */ javax.swing.jscrollpane _scrollpane2 = new javax.swing.jscrollpane();
/** * the body of this email message. */ javax.swing.jtextarea _body = new javax.swing.jtextarea();
/** * the send button. */ javax.swing.jbutton send = new javax.swing.jbutton();
/** * the cancel button. */ javax.swing.jbutton cancel = new javax.swing.jbutton();
/** * a scroll pain. */ javax.swing.jscrollpane _scrollpane = new javax.swing.jscrollpane();
/** * the output area. server messages are displayed here. */ javax.swing.jlist _output = new javax.swing.jlist();
//}}
/** * the list of items added to the output list box. */ javax.swing.defaultlistmodel _model = new javax.swing.defaultlistmodel();
/** * input from the socket. */ java.io.bufferedreader _in;
/** * output to the socket. */ java.io.printwriter _out;
//{{declare_menus //}}
/** * internal class created by visualcafe to route the events to the correct * functions. * * @author visualcafe * @version 1.0 */ class symaction implements java.awt.event.actionlistener {
/** * route the event to the correction method. * * @param event * the event. */ public void actionperformed(java.awt.event.actionevent event) { object object = event.getsource(); if (object == send) send_actionperformed(event); else if (object == cancel) cancel_actionperformed(event); } }
/** * called to actually send a string of text to the socket. this method makes * note of the text sent and the response in the jlist output box. pass a * null value to simply wait for a response. * * @param s * a string to be sent to the socket. null to just wait for a * response. * @exception java.io.ioexception */ protected void send(string s) throws java.io.ioexception { //&n
扫描关注微信公众号
|