A Client to Send SMTP Mail

import java.awt.*;
import javax.swing.*;

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(736312);
    setVisible(false);
    JLabel1.setText("From:");
    getContentPane().add(JLabel1);
    JLabel1.setBounds(12123612);
    JLabel2.setText("To:");
    getContentPane().add(JLabel2);
    JLabel2.setBounds(12483612);
    JLabel3.setText("Subject:");
    getContentPane().add(JLabel3);
    JLabel3.setBounds(12844812);
    JLabel4.setText("SMTP Server:");
    getContentPane().add(JLabel4);
    JLabel4.setBounds(121208412);
    getContentPane().add(_from);
    _from.setBounds(961230024);
    getContentPane().add(_to);
    _to.setBounds(964830024);
    getContentPane().add(_subject);
    _subject.setBounds(968430024);
    getContentPane().add(_smtp);
    _smtp.setBounds(9612030024);
    getContentPane().add(_scrollPane2);
    _scrollPane2.setBounds(12156384108);
    _body.setText("Enter your message here.");
    _scrollPane2.getViewport().add(_body);
    _body.setBounds(00381105);
    Send.setText("Send");
    Send.setActionCommand("Send");
    getContentPane().add(Send);
    Send.setBounds(6027613224);
    Cancel.setText("Cancel");
    Cancel.setActionCommand("Cancel");
    getContentPane().add(Cancel);
    Cancel.setBounds(21627612024);
    getContentPane().add(_scrollPane);
    _scrollPane.setBounds(40812312288);
    getContentPane().add(_output);
    _output.setBounds(40812309285);
    //}}

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

  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值