JavaMail .java
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* @author 李 世贵
* 来源:http://blog.csdn.net/lishiguui
* 欢迎转接,请保留作者和来源,谢谢!
*
*/
public class JavaMail extends JFrame{
private Container container;
private JTextField txtServeraddress;
private JTextField txtAccount;
private JPasswordField pasPassowrd;
private JTextField txtAddress;
private JTextField txtAffixPath;
private JLabel labTitle;
public JavaMail() {
super("邮件附件批量发送系统");
container = getContentPane();
container.setLayout(null);
container.setBackground(new Color(0xE2F2F5));
labTitle = new JLabel("欢迎使用李世贵附件批量发送系统");
labTitle.setForeground(Color.blue);
labTitle.setFont(new Font("华文新魏",Font.PLAIN,25));
JLabel labServeraddress = new JLabel("你的邮箱服务器:");
JLabel labFrame1 = new JLabel();
labFrame1.setForeground(Color.black);
labFrame1.setBorder(BorderFactory.createEtchedBorder());
labFrame1.setBounds(new Rectangle(50,80,370,220));
JLabel labFrame2 = new JLabel();
labFrame2.setForeground(Color.black);
labFrame2.setBorder(BorderFactory.createEtchedBorder());
labFrame2.setBounds(new Rectangle(50,330,370,100));
JLabel labAccount = new JLabel("你的帐号:");
JLabel labPassword = new JLabel("邮箱密码:");
JLabel labAdddress = new JLabel("收信人邮箱:");
JLabel labAffix = new JLabel("附件地址:");
JLabel labAffixCue=new JLabel("注:所有附件必须放入同一个文件夹中");
JButton butDir = new JButton("...");
JButton butOK = new JButton("发送");
txtServeraddress = new JTextField(40);
txtAccount = new JTextField(40);
pasPassowrd = new JPasswordField(40);
txtAddress = new JTextField(40);
txtAffixPath = new JTextField(40);
labTitle.setBounds(new Rectangle(50,30,400,30));
labServeraddress.setBounds(new Rectangle(60,100,140,30));
labAccount.setBounds(new Rectangle(100,150,80,30));
labPassword.setBounds(new Rectangle(100,200,80,30));
labAdddress.setBounds(new Rectangle(90,250,80,30));
txtServeraddress.setBounds(new Rectangle(190,100,200,30));
txtAccount.setBounds(new Rectangle(190,150,200,30));
pasPassowrd.setBounds(new Rectangle(190,200,200,30));
txtAddress.setBounds(new Rectangle(190,250,200,30));
txtAffixPath.setBounds(new Rectangle(140,350,200,30));
labAffix.setBounds(new Rectangle(60,350,80,30));
butDir.setBounds(new Rectangle(350,350,60,30));
labAffixCue.setBounds(new Rectangle(120,380,240,30));
butOK.setBounds(new Rectangle(220,450,80,30));
txtServeraddress.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(!txtServeraddress.getText().matches("smtp.//w[^.]+.//w+")){
JOptionPane.showMessageDialog( JavaMail.this,
"非法的邮件服务器,例如:smtp.126.com", "错误",
JOptionPane.ERROR_MESSAGE );
}else{
txtServeraddress.setFocusable(false);
txtAccount.setFocusable(true);
txtServeraddress.setFocusable(true);
}
}
});
txtAccount.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(!txtAccount.getText().matches("//w+@//w[^.]+.//w+")){
JOptionPane.showMessageDialog( JavaMail.this,
"非法的电子邮件", "错误",
JOptionPane.ERROR_MESSAGE );
}else{
txtAccount.setFocusable(false);
pasPassowrd.setFocusable(true);
txtAccount.setFocusable(true);
}
}
});
pasPassowrd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
pasPassowrd.setFocusable(false);
txtAddress.setFocusable(true);
pasPassowrd.setFocusable(true);
}
});
txtAddress.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(!txtAddress.getText().matches("//w+@//w[^.]+.//w+")){
JOptionPane.showMessageDialog( JavaMail.this,
"非法的电子邮件", "错误",
JOptionPane.ERROR_MESSAGE );
}else{
txtAddress.setFocusable(false);
txtAffixPath.setFocusable(true);
txtAddress.setFocusable(true);
}
}
});
butDir.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.DIRECTORIES_ONLY);
int result = fileChooser.showOpenDialog( JavaMail.this );
if ( result == JFileChooser.CANCEL_OPTION )
return;
File file = fileChooser.getSelectedFile();
if ( file == null ||file.getName().equals( "" ) ){
JOptionPane.showMessageDialog( JavaMail.this,
"请选择一个目录", "错误",
JOptionPane.ERROR_MESSAGE );
return;
}else{
txtAffixPath.setText(file.getAbsolutePath());
}
}
});
butOK.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if ( txtAffixPath.getText().equals( "" ) ){
JOptionPane.showMessageDialog( JavaMail.this,
"附件目录不能为空", "错误",
JOptionPane.ERROR_MESSAGE );
return;
}else{
new Thread(new SendMail(txtServeraddress.getText(),
txtAccount.getText(),pasPassowrd.getPassword().toString(),
txtAddress.getText(),txtAffixPath.getText())).start();
}
}
}
);
container.add(labFrame1);
container.add(labFrame2);
container.add(labServeraddress);
container.add(labAccount);
container.add(labPassword);
container.add(labAdddress);
container.add(labAffix);
container.add(butDir);
container.add(labAffixCue);
container.add(butOK);
container.add(txtServeraddress);
container.add(txtAccount);
container.add(pasPassowrd);
container.add(txtAddress);
container.add(txtAffixPath);
container.add(labTitle);
this.setResizable(false);
setSize(500,600);
setVisible(true);
}
public static void main(String[] args) {
new JavaMail().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
SendMail .java
import java.awt.Container;
import java.awt.Rectangle;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
/**
* @author 李 世贵
* 来源:http://blog.csdn.net/lishiguui
* 欢迎转接,请保留作者和来源,谢谢!
*
*/
public class SendMail extends JDialog implements Runnable{
private String serveraddress = "";
private String from = "";
private String password = "";
private String to = "";
private String affixpath = "";
private Vector<String> vecAffix = new Vector<String>();
private long countfiles=0;
private JProgressBar bar=new JProgressBar();
private Container container;
public SendMail(String serveraddress, String from, String password, String to, String affixpath) {
super();
container = getContentPane();
container.setLayout(null);
this.serveraddress = serveraddress;
this.from = from;
this.password = password;
this.to = to;
this.affixpath = affixpath;
bar.setBounds(new Rectangle(10,40,180,20));
container.add(bar);
setSize(200,100);
setVisible(true);
}
public void run(){
//获取所有附件
VisitFile(affixpath);
bar.setMaximum(vecAffix.size());
bar.setStringPainted(true);
//发送所有附件
for(int i = 0; i < vecAffix.size(); i++ ){
send(vecAffix.get(i),0);
bar.setValue(i);
container.update(this.getGraphics());
}
}
//发送附件
public void send(String affixfile,int count){
try {
//设置系统属性
Properties properties=System.getProperties();
properties.put("mail.smtp.host", serveraddress);
properties.put("mail.smtp.auth", "true");
//邮件会话对象
Session session=Session.getDefaultInstance(properties, new Email_auth(from, password));
//MIME邮件对象
MimeMessage mimemessage = new MimeMessage(session);
//设置发邮件地址
mimemessage.setFrom(new InternetAddress(from));
//设置收邮件地址
mimemessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
//设置邮件主题
mimemessage.setSubject(new File(affixfile).getName(), "GBK");
//生成MimeBodyPart对象存放邮件内容
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("欢迎使用李世贵附件批量发送系统","GBK");
生成MimeBodyPart对象存放附件存放附件
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(affixfile);
//添加附件内容
mbp2.setDataHandler(new DataHandler(fds));
//添加附件标题
mbp2.setFileName(fds.getName());
//Multipart对象是存放生成的MimeBodyPart对象
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
mimemessage.setContent(mp);
//设置发送时邮件时间
mimemessage.setSentDate(new Date());
//发送邮件
Transport.send(mimemessage);
} catch (MessagingException e) {
if(count == 10){
JOptionPane.showMessageDialog( this,
"发送失败", "提示",
JOptionPane.INFORMATION_MESSAGE );
SendMail.this.dispose();
}else{
send(affixfile, count+1);
}
}
}
//获取所有附件
public void VisitFile(String affixpath){
String[] directroy = new File(affixpath).list();
for(int i = 0; i < directroy.length; i++){
File f = new File(affixpath+"//"+directroy[i]);
if(f.isDirectory()){
VisitFile(f.toString());
}else{
countfiles++;
vecAffix.add(f.toString());
}
}
}
}
Email_auth .java
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* @author 李 世贵
* 来源:http://blog.csdn.net/lishiguui
* 欢迎转接,请保留作者和来源,谢谢!
*
*/
public class Email_auth extends Authenticator {
String auth_user;
String auth_password;
public Email_auth() {
super();
}
public Email_auth(String user, String password) {
super();
setUsername(user);
setUserpass(password);
}
public void setUsername(String username){
auth_user=username;
}
public void setUserpass(String userpass){
auth_password=userpass;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(auth_user,auth_password);
}
}
运行程序后的界面: