📄 gb2.java
字号:
/* gb2.java * * Version 2.0 * Copyright(C) 1996 by Bill Giel * * E-mail: rvdi@usa.nai.net * WWW: http://www.nai.net/~rvdi/home.htm * *************************************************************************** * Abstract * -------- * A simple SMTP mail sender with a guest book interface. * * Version 2.0a adds preferredSize and minimumSize Methods to logoPanel class * *************************************************************************** * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. ***************************************************************************//* Documentation addendum from Version 1 * * NOTE: TO RECEIVE GUEST BOOK ENTRIES, YOU MUST SPECIFY YOUR E-MAIL * ADDRESS AS THE 'RECEIVER' PARAMETER IN THE APPLETS HTML TAG. * * YOUR LOCAL HOST MUST SUPPORT SMTP MESSAGES ON PORT 25 * * * ADDENDUM TO DOCUMENTATION (7 Feb 1996) - Some of you have been * unable to get your guestbook applets to work on your own home * pages, while others have been successful. * * If you have a chance to study the code of the mailMessage method * of the 'send' class, you'll see that guestbook first establishes * a socket with the 'mailhost' at port 25, in accordance with * TCP/SMTP specs (see RFC 821). * * Then, guestbook waits for the 220 'service ready' message transmission * over the socket. After successfully receiving 220, it then obtains the * 'helohost' domain name from the server and transmits the HELO <domain> * command over the socket. If accepted, the receiver-SMTP returns a 250 * (OK) reply. * * Following that , guestbook sends MAIL FROM: <sender>, then RECPT TO: * <receiver>, expecting 250 replies after each. Note that in guestbook, * the sender and receiver addresses are the same. * * It then sends the DATA command, and expects 354 intermediate reply from * the SMTP server. All that follows DATA is the standard text message in * the format defined in RFC 822, with a standard 'Subject:' field, followed * by a blank line. What follows the blank line is the actual text (your guest's * optional name, optional email address and comments or suggestions.) * * After the message is transmitted, guestbook transmits a single '\r\n . \r\n', * signifying end of message, waits for a 250 (OK) reply, then transmits a final * QUIT command, and anticipates receipt of 221, indicating the server is closing * the SMTP channel. * * The mailMessage method then closes the socket before returning true, only if * all of the above were successful. mailMessage returns false on any errors, or * if the receiver parameter is null. * * Guestbook should work just fine on your homepage, so long as your www host * supports the above SMTP protocol. You may otherwise have to experiment * to get guestbook to work with custom settings. That could actually be * fun... but since guestbook works as-is on my www server, I have no need * to mess with it :-( * * Please let me know if you manage to get guestbook working by revising it * for conditions other than those it is designed for. * * I hope this additional information is helpful. * ---------------------------------------------------------------------------- * smtpSend class built up from code demonstated in sendmail.java * by Godmar Back, University of Utah, Computer Systems Lab, 1996 * (a simple applet that sends you mail when your page is accessed) * ---------------------------------------------------------------------------- */import java.applet.*;import java.awt.*;import java.io.*;import java.net.*;import java.util.*;class smtpSend{ static final short PORT = 25; String lastline; DataInputStream in; PrintStream p; String mailhost, receiver, sender; TextArea scroller = null; Socket socket = null; smtpSend( String mailhost, String receiver) { this.mailhost = mailhost; this.receiver = receiver; this.sender = receiver; } public void setScroller(TextArea scroller) { this.scroller = scroller; } void expect(String expected, String errorMsg) throws Exception { lastline = in.readLine(); if (!lastline.startsWith(expected)) throw new Exception("Error: " + errorMsg + " (Expected " + expected + ")"); while (lastline.startsWith(expected + "-")) lastline = in.readLine(); } private void scrollOK() { scroller.appendText("OK"); } private void openSocket() throws Exception { display("Connecting to " + mailhost + "..."); try{ socket = new Socket(mailhost, PORT); } catch (Exception e){ throw new Exception("Socket Error: Can't connect!"); } scrollOK(); } private void openInputStream() throws Exception { display("Opening input stream" + "..."); try{ in = new DataInputStream(socket.getInputStream()); }catch (Exception e){ throw new Exception("Connection Error: Cannot open for input."); } scrollOK(); } private PrintStream openOutputStream() throws Exception { display("Opening output stream" + "..."); try{ p = new PrintStream(socket.getOutputStream()); }catch (Exception e){ throw new Exception("Connection Error: Cannot open for output."); } scrollOK(); return p; } private String getHeloHost() throws Exception { String helohost; display("Getting Local Host Name" + "..."); try{ helohost = InetAddress.getLocalHost().toString(); }catch (Exception e){ throw new Exception("Network Error: Unknown Local Host."); } scrollOK(); display("Local Host: " + helohost + "\r\n"); return helohost; } private void display(String string) { if (null != scroller) scroller.appendText("\r\n" + string); System.out.println(string); } private void sendData(String subject, String message) throws Exception { try{ String helohost = getHeloHost(); p.print("HELO " + helohost + "\r\n"); expect("250", "HELO"); int pos; String hello = "Hello "; if ((pos = lastline.indexOf(hello)) != -1) { helohost = lastline.substring(pos + hello.length()); helohost = helohost.substring(0, helohost.indexOf(' ')); } p.print("MAIL FROM: " + sender + "\r\n"); expect("250", "MAIL FROM:"); p.print("RCPT TO: " + receiver + "\r\n"); expect("250", "RCPT TO:"); p.print("DATA\r\n"); expect("354", "DATA"); p.print("Subject: " + subject); p.print(" (" + helohost + ")"); p.print("\r\n\r\n"); DataInputStream is = new DataInputStream(new StringBufferInputStream(message)); while (is.available() > 0) { String ln = is.readLine(); if (ln.equals(".")) ln = ".."; p.println(ln); } p.print(new Date().toGMTString()); p.print("\r\n.\r\n"); expect("250", "end of data"); p.print("QUIT\r\n"); expect("221", "QUIT"); }catch(Exception e){ throw e; } } public void mailMessage(String subject,String message) throws Exception { if(null==receiver)throw new Exception("Parameter Error: No RECEIVER"); try{ openSocket(); openOutputStream(); openInputStream(); expect("220", "No greeting"); display("Sending message via SMTP..."); sendData(subject,message); } catch(Exception e){ throw e; }finally{ try { if(socket != null)socket.close(); } catch(Exception e){} } scrollOK(); display("Message Sent - Thank You!"); display("Press 'Quit' to close this window."); }}class logoPanel extends Canvas{ Image image = null; boolean threeD; int iWidth, iHeight; logoPanel(Image image, boolean threeD) { this.image = image; this.threeD = threeD; if(null != image){ iWidth=image.getWidth(this); iHeight=image.getHeight(this); } else{ iWidth=400; iHeight=100; } if(threeD) resize(iWidth+4,iHeight+4); else resize(iWidth,iHeight); } public void paint(Graphics g) { int x,y; g.setColor(Color.lightGray); if(threeD){ x=y=2; g.fill3DRect(0,0,size().width,size().height,false); } else{ x=y=0; g.fillRect(0,0,size().width,size().height); } if(null != image) g.drawImage(image,x,y,this); } public Dimension minimumSize() { if(threeD) return(new Dimension(iWidth+4,iHeight+4)); else return(new Dimension(iWidth,iHeight)); } public Dimension preferredSize() { return this.minimumSize(); }}class gbFrame extends tFrame{ static final String COPYRIGHT = "Guestbook II Copyright (C) 1996 by Bill Giel"; TextField tf1,tf2; TextArea ta1, ta2; tButton sendButton; smtpSend smtp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -