动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
电脑书籍下载:程序设计书籍 | 数据库教程书籍 | 平面与多媒体书籍 | 网络通讯书籍 | 系统管理书籍 | 网络安全书籍 | 认证考试书籍
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ Java教程 ]的信息



本月文章推荐
.用SAAJ1.2发送接收二进制Web服务.
.从URL获取文件保存到本地的代码.
.关于 Eclipse Communi.
.经典Java开发工具介绍(4):Bor.
.使用开源工具提高J2EE系统的质量.
.ThreadLocal的设计与使用.
.Spring2.0技巧之活用FactoryBean.
.JavaWebStudio 2005新感觉.
.违例控制:解决错误.
.java基础 java中模式和框架的介绍.
.使用JAVA中的动态代理实现数据库.
.权限系统:分散实现、关注变化.
.使用UML编写Java应用程序 (1).
.如何定义类的结构.
.在Java2环境中应用IP地址封装对象.
.详解java源文件涉及到的package问.
.Struts开发指南之其他Web构架介绍.
.利用Java实现串口全双工通讯 (转.
.用Java快速开发Linux GUI应用.
.Halloway:对Ruby VS Java误区的深.

SMTP邮件发送的客户端

发表日期:2008-1-5 |


SMTP邮件发送的客户端
点击查看大图



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 {
    // 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());
      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);

  }
}

上一篇:JNDI查找示例 人气:424
下一篇:使用Socket通道读取web页面 人气:504
浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-7-19 Roclog v3.1.4 SP1
2008-7-19 马克斯CMS(Maxcms) v1.1 Build 0
2008-7-19 乐学LBBS无限级目录图片直读系统
2008-7-19 PHP在线考试系统PPExam v1.2.5
2008-7-19 APJE私服发布系统 v2.0
2008-7-19 寻觅图书管理系统 v1.12
2008-7-19 寻觅学校管理系统
2008-7-19 义乌网站目录baidu新闻订阅 v1.0
2008-7-19 牛仔IIS日志蜘蛛爬行记录分析器
2008-7-19 UltraEdit 简体中文增强版 14.10
2008-7-19 CentOS 5.2 i386 LiveCD
2008-7-19 Snapture多功能相机 v1.4
2008-7-19 iAcces中文输入法 v1.0Build016
2008-7-19 Cookbook烹饪秘籍 v2.5
2008-7-19 苹果专用DVD转换工具 v1.1.59汉化
2008-7-19 Modem修复软件ZiPhone修改版04.0
2008-7-19 AgileMessenger即时通讯工具美化
2008-7-19 Sketches画图软件 v0.7b6破解版


  发表评论
姓 名: 验证码:
内 容:
[ 汉字翻译拼音 ] [ 广告代码 ] [ 符号对照表 ] [ 进制转换 ] [ 经典小工具 ] [ 个税计算 ] [ 汉字简繁转换 ] [ 普通单位换算 ] [ 公制单位换算 ]
[ 生辰老黄历 ] [ 国内电话区号 ] [ 国家代码与域名缩写 ] [ 文字加密解密 ] [ 健康查询 ] [ 万年历 ] [ 手机号码查询 ] [ ip搜索 ] [ Google PR查询 ]
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵