📄 mainui.java
字号:
package org.serain.shmily.mail;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.mail.Message;
import javax.swing.*;
public class MainUI extends JFrame {
private Email email;
private ReceiveMail receiveMail;
private JTextField to;
private JTextField from;
private JTextField cc;
private JTextField bcc;
private JTextField subject;
private JTextArea text;
private JTextField attachment;
private JTextField mailhost;
private JTextField username;
private JTextField password;
private JTextField proxyHost;
private JTextField proxyPort;
private JPanel buttonPanel;
private JTextArea console;
public MainUI(){
setSize(400,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Mail发送系统");
setLayout(new GridBagLayout());
add(new JLabel("收件地址(*):"),new GBC(0,0).setFill(GBC.HORIZONTAL));
to=new JTextField();
add(to,new GBC(1,0,3,1).setFill(GBC.HORIZONTAL).setWeight(100, 0));
add(new JLabel("发件地址(*):"),new GBC(0,1).setFill(GBC.HORIZONTAL));
from=new JTextField();
add(from,new GBC(1,1,3,1).setFill(GBC.HORIZONTAL).setWeight(100, 0));
add(new JLabel("抄送地址:"),new GBC(0,2).setFill(GBC.HORIZONTAL));
cc=new JTextField();
add(cc,new GBC(1,2,3,1).setFill(GBC.HORIZONTAL).setWeight(100, 0));
add(new JLabel("暗送地址:"),new GBC(0,3).setFill(GBC.HORIZONTAL));
bcc=new JTextField();
add(bcc,new GBC(1,3,3,1).setFill(GBC.HORIZONTAL).setWeight(100, 0));
add(new JLabel("邮件主题(*):"),new GBC(0,4).setFill(GBC.HORIZONTAL));
subject=new JTextField();
add(subject,new GBC(1,4,3,1).setFill(GBC.HORIZONTAL).setWeight(100, 0));
add(new JLabel("附件路径:"),new GBC(0,5).setFill(GBC.HORIZONTAL));
attachment=new JTextField();
add(attachment,new GBC(1,5,3,1).setFill(GBC.HORIZONTAL).setWeight(100, 0));
text=new JTextArea();
add(new JScrollPane(text),new GBC(0,6,4,1).setFill(GBC.BOTH).setWeight(100, 100));
add(new JLabel("服务器(*):"),new GBC(0,7).setFill(GBC.HORIZONTAL));
mailhost=new JTextField();
add(mailhost,new GBC(1,7,3,1).setFill(GBC.HORIZONTAL).setWeight(100, 0));
add(new JLabel("用户(*):"),new GBC(0,8).setFill(GBC.HORIZONTAL));
username=new JTextField();
add(username,new GBC(1,8).setFill(GBC.HORIZONTAL).setWeight(50, 0));
add(new JLabel("密码(*):"),new GBC(2,8).setFill(GBC.HORIZONTAL));
password=new JTextField();
add(password,new GBC(3,8).setFill(GBC.HORIZONTAL).setWeight(50, 0));
add(new JLabel("代理:"),new GBC(0,9).setFill(GBC.HORIZONTAL));
proxyHost=new JTextField();
add(proxyHost,new GBC(1,9).setFill(GBC.HORIZONTAL).setWeight(50, 0));
add(new JLabel("端口:"),new GBC(2,9).setFill(GBC.HORIZONTAL));
proxyPort=new JTextField();
add(proxyPort,new GBC(3,9).setFill(GBC.HORIZONTAL).setWeight(50, 0));
buttonPanel=new JPanel();
add(buttonPanel,new GBC(0,10,4,1).setFill(GBC.HORIZONTAL));
JButton send=new JButton("发送");
buttonPanel.add(send);
JButton cancel=new JButton("收信");
buttonPanel.add(cancel);
console=new JTextArea();
console.setEditable(false);
add(new JScrollPane(console),new GBC(0,11,4,1).setFill(GBC.BOTH).setWeight(100, 100));
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
sendButtonAction();
}
});
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cancelButtonAction();
}
});
}
public void sendButtonAction(){
if(to.getText().equals("")||from.getText().equals("")||mailhost.getText().equals("")||subject.getText().equals("")||text.getText().equals("")||username.getText().equals("")||password.getText().equals("")){
JOptionPane.showConfirmDialog(buttonPanel,"不能为空的请填写","警告",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
}else{
email=new Email(to.getText(),from.getText(),cc.getText(),bcc.getText(),mailhost.getText(),subject.getText(),text.getText(),attachment.getText(),username.getText(),password.getText());
email.send();
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new PrintWriter(System.err));
console.append(System.out.toString());
}
}
public void cancelButtonAction(){
receiveMail=new ReceiveMail(mailhost.getText(),username.getText(),password.getText());
try {
Message[] message=receiveMail.receive();
for(int i=0;i<message.length;i++){
console.append(message[i].getSubject());
console.append("\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[]args){
java.awt.EventQueue.invokeLater(new Runnable(){
public void run(){
new MainUI().setVisible(true);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -