📄 mailto.java
字号:
package MailTO;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import SendMail.MailSend;
import com.swtdesigner.SwingResourceManager;
public class MailTo extends JFrame {
private JTextField textField_2;
private JTextField textField_3;
private JTextArea textArea;
private JTextField textField;
private static String name="";
private static String password="";
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
MailTo frame = new MailTo(name, password);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public MailTo(String user,String pass) {
super();
setBounds(100, 100, 452, 367);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name=user;password=pass;
//System.out.print(name+"test");
//System.out.print(password+"test2");
MailSend.setNamePass(name,password);
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.NORTH);
final JLabel label = new JLabel();
label.setIcon(SwingResourceManager.getIcon(MailTo.class, "/image/mail1.jpg"));
panel.add(label, BorderLayout.NORTH);
final JLabel label_1 = new JLabel();
label_1.setIcon(SwingResourceManager.getIcon(MailTo.class, "/image/mail3.jpg"));
panel.add(label_1);
final JPanel panel_1 = new JPanel();
panel_1.setLayout(new BorderLayout());
panel.add(panel_1, BorderLayout.SOUTH);
final JPanel panel_2 = new JPanel();
panel_2.setBackground(new Color(215, 242, 255));
panel_1.add(panel_2, BorderLayout.NORTH);
final JLabel label_2 = new JLabel();
label_2.setIcon(SwingResourceManager.getIcon(MailTo.class, "/image/mail5.gif"));
label_2.setText("收件人");
panel_2.add(label_2);
textField = new JTextField();
textField.setPreferredSize(new Dimension(315, 20));
panel_2.add(textField);
final JPanel panel_4 = new JPanel();
panel_4.setLayout(new BorderLayout());
panel_4.setBackground(new Color(215, 242, 255));
getContentPane().add(panel_4);
final JPanel panel_7 = new JPanel();
panel_7.setBackground(new Color(215, 242, 255));
panel_4.add(panel_7);
final JLabel label_4 = new JLabel();
label_4.setIcon(SwingResourceManager.getIcon(MailTo.class, "/image/mail6.gif"));
label_4.setText("附件 ");
panel_7.add(label_4);
textField_3 = new JTextField();
textField_3.setPreferredSize(new Dimension(240, 20));
panel_7.add(textField_3);
final JButton button = new JButton();
button.setText("浏览...");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
buttonActionPerformed();
}
});
panel_7.add(button);
final JPanel panel_8 = new JPanel();
panel_8.setBackground(new Color(215, 242, 255));
panel_4.add(panel_8, BorderLayout.NORTH);
final JLabel label_6 = new JLabel();
label_6.setIcon(SwingResourceManager.getIcon(MailTo.class, "/image/mail8.gif"));
label_6.setText("主题 ");
panel_8.add(label_6);
textField_2 = new JTextField();
textField_2.setPreferredSize(new Dimension(313, 20));
panel_8.add(textField_2);
final JPanel panel_5 = new JPanel();
panel_5.setLayout(new BorderLayout());
getContentPane().add(panel_5, BorderLayout.SOUTH);
textArea = new JTextArea(5,5);
textArea.setText("在此填写发送正文:");
JScrollPane jsp=new JScrollPane(textArea);
panel_5.add(jsp, BorderLayout.NORTH);
final JLabel label_5 = new JLabel();
label_5.setIcon(SwingResourceManager.getIcon(MailTo.class, "/image/mail3.jpg"));
panel_5.add(label_5);
final JPanel panel_6 = new JPanel();
panel_6.setBackground(new Color(215, 242, 255));
panel_5.add(panel_6, BorderLayout.SOUTH);
final JButton button_1 = new JButton();
button_1.setText("发送");
button_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
button_1ActionPerformed();
}
});
panel_6.add(button_1);
final JButton button_2 = new JButton();
button_2.setText("退出");
button_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
button_2ActionPerformed();
}
});
panel_6.add(button_2);
}
void button_1ActionPerformed(){
String subject=textField_2.getText();
if(subject.equals ("")){
if(JOptionPane.showConfirmDialog(this,"你不需要设置主题吗?","系统提示",0)!=0)
return;
}
MailSend.setSubject(subject);
String body=textArea.getText();
MailSend.setBody(body);
String to=textField.getText();
if(to.indexOf ("@")==-1) {
JOptionPane.showMessageDialog(this, "无效的收信人地址!", "error", JOptionPane.ERROR_MESSAGE);
return;
}
MailSend.setTo(to);
/*String copyTo=textField_1.getText();
if(!copyTo.trim().equals("")){
MailSend.setCopyTo(copyTo);
}*/
String fileAffix=textField_3.getText();
if(fileAffix.trim().equals("")&&body.trim().equals("")){
JOptionPane.showMessageDialog(this, "附件和内容不能同时为空!", "警告", JOptionPane.ERROR_MESSAGE);
return;
}
if(!fileAffix.equals("")){
MailSend.addFileAffix(fileAffix);
}
if(MailSend.sendout()==false){
JOptionPane.showMessageDialog(this, "邮件发送失败", "error", JOptionPane.ERROR_MESSAGE);
textArea.setText("邮件发送失败,请检查您的用户名,密码及网络连接是否正常!");
return;
}
else{
JOptionPane.showMessageDialog(this, "邮件发送成功", "系统提示",JOptionPane.INFORMATION_MESSAGE);
textArea.setText("恭喜您,您的邮件已成功发送!");
}
}
void button_2ActionPerformed(){
System.exit(0);
}
void buttonActionPerformed(){
FileDialog filedialog=new FileDialog(this,"附件");
filedialog.setVisible(true);
String file=filedialog.getDirectory()+filedialog.getFile();
textField_3.setText(file);
textField_3.setEditable(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -