最终成品度娘盘链接:https://pan.baidu.com/s/1HkjbrFCej7Q28fZOY10L0A
//带附件的邮件发送器,废话少说,直接上源码;本来有加载图片功能,但是不同分辨率的图片不好处理,预览的时候容易把界面挡住,所以就删掉了。WindowBuilder可运行。
//发送QQ邮箱邮件时附件文件名过长会导致乱码,在main函数中加上这句就行
System.setProperty("mail.mime.splitlongparameters","false");
原因是QQ邮箱会把名字分段重新编码,导致后缀名也被改了,变成一个.bin文件
public class APP {
private JFrame frame;
JTextField txtqqcom;
JTextField textField;
JTextField textField_2;
JTextField txtSmtpqqcom;
TextArea textArea;
JTextField textField_4;
JTextField textField_5;
JTextField textField_6;
JTextField textField_7;
JTextField textField_1;
public static void main(String[] args) {
System.setProperty("mail.mime.splitlongparameters", "false");
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
APP window = new APP();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public APP() {
initialize();
}
public void initialize() {
frame = new JFrame();
frame.setTitle("\u7B80\u6613\u90AE\u4EF6\u53D1\u9001\u5668");
frame.setBounds(100, 100, 881, 608);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
textField_1 = new JTextField();
textField_1.setBounds(92, 521, 520, 37);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
txtqqcom = new JTextField();
txtqqcom.setText("@qq.com");
txtqqcom.setCaretPosition(0);
txtqqcom.setBounds(128, 11, 727, 30);
frame.getContentPane().add(txtqqcom);
txtqqcom.setColumns(10);
JLabel lblNewLabel = new JLabel("\u53D1\u4EF6\u4EBA\u90AE\u7BB1:");
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 15));
lblNewLabel.setBounds(29, 10, 89, 30);
frame.getContentPane().add(lblNewLabel);
JLabel label = new JLabel("\u53D1\u4EF6\u4EBA\u90AE\u7BB1\u5BC6\u7801:");
label.setFont(new Font("宋体", Font.BOLD, 15));
label.setBounds(29, 48, 132, 30);
frame.getContentPane().add(label);
textField = new JTextField();
textField.setText("邮箱密码不是QQ密码,开启STMP邮箱服务后会有单独的密码");
textField.setBounds(154, 51, 701, 27);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\u6536\u4EF6\u4EBA\u90AE\u7BB1:");
label_1.setFont(new Font("宋体", Font.BOLD, 15));
label_1.setBounds(29, 132, 112, 30);
frame.getContentPane().add(label_1);
textField_2 = new JTextField();
textField_2.setBounds(128, 134, 727, 27);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
JLabel lblstmp = new JLabel("\u53D1\u4EF6\u4EBA\u90AE\u7BB1SMTP\u5730\u5740:");
lblstmp.setForeground(Color.RED);
lblstmp.setBackground(Color.RED);
lblstmp.setFont(new Font("宋体", Font.BOLD, 15));
lblstmp.setBounds(29, 90, 157, 32);
frame.getContentPane().add(lblstmp);
txtSmtpqqcom = new JTextField();
txtSmtpqqcom.setText("smtp.qq.com");
txtSmtpqqcom.setBounds(196, 88, 163, 29);
frame.getContentPane().add(txtSmtpqqcom);
txtSmtpqqcom.setColumns(10);
textField_4 = new JTextField();
textField_4.setText("465");
textField_4.setBounds(447, 88, 147, 30);
frame.getContentPane().add(textField_4);
textField_4.setColumns(10);
JButton button = new JButton("\u8BE6\u60C5");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "QQ邮箱的SMTP服务器地址为:smtp.qq.com;网易163邮箱的SMTP服务器地址为:smtp.163.com;\nQQ邮箱的SMTP(SLL)端口为465或587,非 SSL连接的端口一般默认为 25,可以不添加;\n如果开启了SSL连接,QQ邮箱的SMTP(SLL)端口为465或587,其他邮箱自行去查看...");
}
});
button.setFont(new Font("宋体", Font.PLAIN, 15));
button.setBounds(614, 88, 89, 30);
frame.getContentPane().add(button);
TextArea textArea = new TextArea();
textArea.setBounds(10, 224, 845, 291);
frame.getContentPane().add(textArea);
JLabel lblSmtp = new JLabel("SMTP\u7AEF\u53E3:");
lblSmtp.setFont(new Font("宋体", Font.BOLD, 15));
lblSmtp.setForeground(Color.RED);
lblSmtp.setBounds(369, 88, 81, 26);
frame.getContentPane().add(lblSmtp);
JLabel label_2 = new JLabel("\u90AE\u4EF6\u4E3B\u9898:");
label_2.setFont(new Font("宋体", Font.BOLD, 15));
label_2.setBounds(39, 172, 76, 27);
frame.getContentPane().add(label_2);
textField_5 = new JTextField();
textField_5.setBounds(128, 172, 165, 27);
frame.getContentPane().add(textField_5);
textField_5.setColumns(10);
textField_7 = new JTextField();
textField_7.setBounds(661, 171, 194, 25);
frame.getContentPane().add(textField_7);
textField_7.setColumns(10);
JLabel label_3 = new JLabel("\u53D1\u4EF6\u4EBA\u6635\u79F0:");
label_3.setFont(new Font("宋体", Font.BOLD, 15));
label_3.setBounds(300, 171, 89, 28);
frame.getContentPane().add(label_3);
textField_6 = new JTextField();
textField_6.setBounds(397, 171, 155, 28);
frame.getContentPane().add(textField_6);
textField_6.setColumns(10);
JLabel label_4 = new JLabel("\u6536\u4EF6\u4EBA\u6635\u79F0:");
label_4.setFont(new Font("宋体", Font.BOLD, 15));
label_4.setBounds(562, 171, 89, 28);
frame.getContentPane().add(label_4);
Button button_2 = new Button("\u6DFB\u52A0\u9644\u4EF6");
button_2.setFont(new Font("华文新魏", Font.BOLD, 15));
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
if(file.isDirectory()){
JOptionPane.showMessageDialog(null,"请选择文件!");
}else if(file.isFile()){
JOptionPane.showMessageDialog(null,"选择成功!");
String path=file.getPath();
textField_1.setText(path);
}
}
});
button_2.setBounds(10, 521, 76, 37);
frame.getContentPane().add(button_2);
Button button_1 = new Button("\u53D1\u9001");
button_1.setBackground(Color.LIGHT_GRAY);
button_1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,"发送中,请稍后...");
String myEmailAccount = txtqqcom.getText().toString();
String myEmailPassword = textField.getText().toString();
String myEmailSMTPHost = txtSmtpqqcom.getText().toString();
String receiveMailAccount = textField_2.getText().toString();
String smtpPort = textField_4.getText().toString();
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", myEmailSMTPHost);
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port",smtpPort);
Session session= Session.getInstance(props);
try {
MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount);
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
JOptionPane.showMessageDialog(null,"发送成功!");
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,"发送失败!");
}
}
MimeMessage createMimeMessage(Session session, String myEmailAccount, String receiveMailAccount) throws Exception {
MimeMessage message = new MimeMessage(session);
MimeBodyPart text = new MimeBodyPart();
text.setContent(textArea.getText().toString(), "text/html;charset=UTF-8");
MimeBodyPart attachment = new MimeBodyPart();
DataHandler dh2 = new DataHandler(new FileDataSource(textField_1.getText().toString()));
attachment.setDataHandler(dh2);
attachment.setFileName(MimeUtility.encodeText(dh2.getName()));
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(attachment);
mm.setSubType("mixed");
message.setFrom(new InternetAddress(myEmailAccount, textField_6.getText().toString(), "UTF-8"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMailAccount,textField_7.getText().toString(),"UTF-8"));
message.setSubject(textField_5.getText().toString(), "UTF-8");
message.setContent( mm , "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
return message;
}
});
button_1.setFont(new Font("Dialog", Font.BOLD, 15));
button_1.setBounds(649, 521, 108, 48);
frame.getContentPane().add(button_1);
JButton btnNewButton = new JButton("\u53D1\u9001\u5931\u8D25?!");
btnNewButton.setForeground(Color.RED);
btnNewButton.setBackground(Color.LIGHT_GRAY);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"邮箱账号必须要开启SMTP服务,在浏览器网页登录邮箱后一般在邮箱的“设置--账户”中可以开启");
}
});
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 13));
btnNewButton.setBounds(735, 88, 120, 30);
frame.getContentPane().add(btnNewButton);
JLabel label_5 = new JLabel("\u6B63\u6587\uFF1A");
label_5.setBounds(10, 209, 54, 15);
frame.getContentPane().add(label_5);
JLabel lblNewLabel_1 = new JLabel("BY \u4FE1\u5B891701 \u8BB8\u4EAE");
lblNewLabel_1.setBounds(763, 564, 112, 15);
frame.getContentPane().add(lblNewLabel_1);
}
}