⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 email-kehuduanyuandaima.txt

📁 一个完整的email客户端Java源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
一个完整的email客户端Java源代码

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 
send("RCPT TO: " + _to.getText()); 
     send("DATA"); 
     _out.println("Subject:" + _subject.getText()); 
     _out.println(_body.getText()); 
     send("."); 
     s.close(); 

   } catch (Exception e) { 
     _model.addElement("Error: " + e); 
   } 

 } 

 /** 
  * Called when cancel is clicked. End the application. 
  * 
  * @param event 
  *            The event. 
  */ 
 void Cancel_actionPerformed(java.awt.event.ActionEvent event) { 
   System.exit(0); 

 } 
}   
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 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -