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

📄 mailtest.java

📁 char44-3 发送邮件 提供了本书第4章的发送邮件实例的源程序;
💻 JAVA
字号:
//MailTest.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

/**
本程序显示如何通过套接字来传输文本邮件
*/
public class MailTest
{
	public static void main(String[] args)
	{
        JFrame frame = new MailTestFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	}
}

/**
邮件界面的设计
*/
class MailTestFrame extends JFrame
{
	public MailTestFrame()
	{
	//frame的大小
        setSize(WIDTH, HEIGHT);
        setTitle("MailTest");
		//frame的层次设计
		getContentPane().setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 0;
        gbc.weighty = 0;
		
        gbc.weightx = 0;
		add(new JLabel("From:"), gbc, 0, 0, 1, 1);
		gbc.weightx = 100;
        from = new JTextField(20);
		add(from, gbc, 1, 0, 1, 1);
		
        gbc.weightx = 0;
		add(new JLabel("To:"), gbc, 0, 1, 1, 1);
		gbc.weightx = 100;
        to = new JTextField(20);
		add(to, gbc, 1, 1, 1, 1);
		
        gbc.weightx = 0;
		add(new JLabel("SMTP server:"), gbc, 0, 2, 1, 1);
        gbc.weightx = 100;
        smtpServer = new JTextField(20);
		add(smtpServer, gbc, 1, 2, 1, 1);
		
		gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 100;
	//此文本框是用来输入邮件内容的
        message = new JTextArea();
		add(new JScrollPane(message), gbc, 0, 3, 2, 1);
		//通信区域
		communication = new JTextArea();
		add(new JScrollPane(communication), gbc, 0, 4, 2, 1);
		
        gbc.weighty = 0;
		JButton sendButton = new JButton("Send");
		sendButton.addActionListener(new
            ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				new Thread()
				{
					public void run()
					{
						sendMail();
					}
				}.start();
			}
		});
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(sendButton);
        add(buttonPanel, gbc, 0, 5, 2, 1);
	}
	
	/**
为窗口添加组件
	*/
	private void add(Component c, GridBagConstraints gbc,
		int x, int y, int w, int h)
	{
        gbc.gridx = x;
        gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
        getContentPane().add(c, gbc);
	}
	
    /**
	发送邮件内容
    */
	public void sendMail()
	{
        try
		{
			Socket s = new Socket(smtpServer.getText(), 25);
			
			out = new PrintWriter(s.getOutputStream());
			in = new BufferedReader(new
				InputStreamReader(s.getInputStream()));
			
			String hostName
				= InetAddress.getLocalHost().getHostName();
			
			receive();
			send("HELO " + hostName);
			receive();
			send("MAIL FROM: <" + from.getText() +">");
			receive();
			send("RCPT TO: <" + to.getText() +">");
			receive();
			send("DATA");
			receive();
			StringTokenizer tokenizer = new StringTokenizer(
				message.getText(), "\n");
			while (tokenizer.hasMoreTokens())
				send(tokenizer.nextToken());
			send(".");
			receive();
			s.close();
		}
		catch (IOException exception)
		{
			communication.append("Error: " + exception);
		}
	}
	
	/**
	向套接字发送字符串,同时得到反馈
    */
    public void send(String s) throws IOException
	{
		communication.append(s);
        communication.append("\n");
		out.print(s);
        out.print("\r\n");
        out.flush();
	}
	
	/**
	 从套接字得到字符串并且打印出来
	*/
    public void receive() throws IOException
	{
        String line = in.readLine();
		if (line != null)
		{
			communication.append(line);
			communication.append("\n");
		}
    }
	
	private BufferedReader in;
	private PrintWriter out;
	private JTextField from;
	private JTextField to;
    private JTextField smtpServer;
	private JTextArea message;
	private JTextArea communication;
	
    public static final int WIDTH = 300;
	public static final int HEIGHT = 300;
  }

⌨️ 快捷键说明

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