📄 sendmail.java
字号:
import java.awt.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
import javax.swing.*;
class SendMail extends javax.swing.JFrame {
boolean frameSizeAdjusted = false;
javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
javax.swing.JLabel JLabel4 = new javax.swing.JLabel();
javax.swing.JLabel JLabel5 = new javax.swing.JLabel();
javax.swing.JTextField _from = new javax.swing.JTextField();
javax.swing.JTextField _to = new javax.swing.JTextField();
javax.swing.JTextField _subject = new javax.swing.JTextField();
javax.swing.JTextField _smtp = new javax.swing.JTextField();
javax.swing.JScrollPane _scrollPane2 = new javax.swing.JScrollPane();
javax.swing.JTextArea _body = new javax.swing.JTextArea();
javax.swing.JButton Send = new javax.swing.JButton();
javax.swing.JButton Cancel = new javax.swing.JButton();
javax.swing.JScrollPane _scrollPane = new javax.swing.JScrollPane();
javax.swing.JList _output = new javax.swing.JList();
javax.swing.DefaultListModel _model = new javax.swing.DefaultListModel();
java.io.BufferedReader _in;
BufferedWriter _out;
public SendMail() {
setTitle("计科0401班 吴立雪 04281019 韦锦想 04281017【SMTP客户端】");
getContentPane().setLayout(null);
setSize(736, 348);
setVisible(false);
JLabel1.setText("发件人:");
getContentPane().add(JLabel1);
JLabel1.setBounds(12, 17, 48, 12);
JLabel2.setText("收件人:");
getContentPane().add(JLabel2);
JLabel2.setBounds(12, 53, 48, 12);
JLabel3.setText("主题:");
getContentPane().add(JLabel3);
JLabel3.setBounds(12, 89, 48, 12);
JLabel4.setText("SMTP服务器:");
getContentPane().add(JLabel4);
JLabel4.setBounds(12, 125, 90, 12);
JLabel5.setText("邮件内容:");
getContentPane().add(JLabel5);
JLabel5.setBounds(12, 161, 90, 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, 178, 384, 118);
_body.setText("");
_scrollPane2.getViewport().add(_body);
_body.setBounds(0, 0, 381, 105);
Send.setText("发送");
Send.setActionCommand("Send");
getContentPane().add(Send);
Send.setBounds(60, 312, 132, 24);
Cancel.setText("取消");
Cancel.setActionCommand("Cancel");
getContentPane().add(Cancel);
Cancel.setBounds(216, 312, 120, 24);
getContentPane().add(_scrollPane);
_scrollPane.setBounds(408, 12, 312, 308);
getContentPane().add(_output);
_output.setBounds(408, 12, 309, 285);
SymAction lSymAction = new SymAction();
Send.addActionListener(lSymAction);
Cancel.addActionListener(lSymAction);
_output.setModel(_model);
_model.addElement("服务器响应:");
_scrollPane.getViewport().setView(_output);
_scrollPane2.getViewport().setView(_body);
}
public void setVisible(boolean b) {
if (b)
setLocation(50, 50);
super.setVisible(b);
}
static public void main(String args[]) {
SendMail sendmail = new SendMail();
sendmail.show();
sendmail.setDefaultCloseOperation(SendMail.EXIT_ON_CLOSE);
}
public void addNotify() {
Dimension size = getSize();
super.addNotify();
if (frameSizeAdjusted)
return;
frameSizeAdjusted = true;
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);
}
class SymAction implements java.awt.event.ActionListener {
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);
}
}
protected void send(String s) throws java.io.IOException {
if (s != null) {
_model.addElement("C:" + s);
_out.write(s);
_out.newLine();
_out.flush();
}
String line = _in.readLine();
if (line != null) {
_model.addElement("S:" + line);
}
}
void Send_actionPerformed(java.awt.event.ActionEvent event) {
try {
java.net.Socket socket = new java.net.Socket(_smtp.getText(), 25);
_in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
_out = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
send("hello " + _smtp.getText());
send("mail from:" + _from.getText() + "");
send("rcpt to:" + _to.getText() + "");
send("data");
_out.write("From: " + _from.getText());
_out.newLine();
_out.write("To: " + _to.getText());
_out.newLine();
_out.write("Subject: " + _subject.getText());
_out.newLine();
_out.newLine();
_out.write(_body.getText());
_out.newLine();
send(".");
send("quit");
} catch (Exception e) {
_model.addElement("Error: " + e);
}
}
void Cancel_actionPerformed(java.awt.event.ActionEvent event) {
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -