📄 guestbook.java
字号:
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("正在与" + 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("正在打开输入流" + "..."); 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("正在打开输出流" + "..."); 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("获取本地主机名称" + "..."); try{ helohost = InetAddress.getLocalHost().toString(); }catch (Exception e){ throw new Exception("Network Error: Unknown Local Host."); } scrollOK(); display("本地主机名称:" + 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("正在通过SMTP发送消息..."); sendData(subject,message); } catch(Exception e){ throw e; }finally{ try { if(socket != null)socket.close(); } catch(Exception e){} } scrollOK(); display("消息已被成功发送-谢谢!"); display("请单击『退出』按钮关闭本窗口"); }}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 by Ali"; TextField tf1,tf2; TextArea ta1, ta2; tButton sendButton; smtpSend smtp; gbFrame(String title, smtpSend smtp, Image image, boolean threeD) { super(title); this.smtp=smtp; setFont(new Font("System",Font.PLAIN,14)); GridBagLayout gridbag=new GridBagLayout(); GridBagConstraints c=new GridBagConstraints(); setLayout(gridbag); if(null != image){ c.insets=new Insets(5,5,8,5); c.weightx=1;c.weighty=1; c.fill=GridBagConstraints.NONE; c.gridwidth=GridBagConstraints.REMAINDER; logoPanel lp=new logoPanel(image, threeD); gridbag.setConstraints(lp,c); add(lp); } c.weightx=1;c.weighty=1; c.insets=new Insets(3,5,0,0); c.gridwidth=1; c.fill=GridBagConstraints.NONE; c.anchor=GridBagConstraints.EAST; Label label = new Label("用户姓名(可选项):"); gridbag.setConstraints(label,c); add(label);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -