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

📄 email-kehuduanyuandaima.txt

📁 一个完整的email客户端Java源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
  */ 
 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); 
   } 
 } 

 /** 
  * Called when the send button is clicked. Actually sends the mail message. 
  * 
  * @param event 
  *            The event. 
  */ 
 void Send_actionPerformed(java.awt.event.ActionEvent event) { 
   try { 

     java.net.Socket s = new java.net.Socket(_smtp.getText(), 25); 
     _out = new java.io.PrintWriter(s.getOutputStream()); 
     _in = new java.io.BufferedReader(new java.io.InputStreamReader(s 
         .getInputStream())); 

     send(null); 
     send("HELO " + java.net.InetAddress.getLocalHost().getHostName()); 
     send("MAIL FROM: " + _from.getText()); 
* 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; 
* 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 
* 

⌨️ 快捷键说明

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