邮件附件批量发送系统二

  1. JavaMail .java
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.Font;
  5. import java.awt.Rectangle;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.File;
  9. import java.util.Vector;
  10. import javax.swing.BorderFactory;
  11. import javax.swing.JButton;
  12. import javax.swing.JFileChooser;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPasswordField;
  17. import javax.swing.JTextField;
  18. /**
  19.  * @author 李 世贵
  20.  * 来源:http://blog.csdn.net/lishiguui
  21.  * 欢迎转接,请保留作者和来源,谢谢!
  22.  *
  23.  */
  24. public class JavaMail extends JFrame{
  25.     private Container container;
  26.     private JTextField txtServeraddress;
  27.     private JTextField txtAccount;
  28.     private JPasswordField pasPassowrd;
  29.     private JTextField txtAddress;
  30.     private JTextField txtAffixPath;
  31.     
  32.     private JLabel labTitle;
  33.     
  34.     
  35.     public JavaMail() {
  36.         super("邮件附件批量发送系统");
  37.         container = getContentPane();
  38.         container.setLayout(null);
  39.         container.setBackground(new Color(0xE2F2F5));
  40.         
  41.         labTitle = new JLabel("欢迎使用李世贵附件批量发送系统");
  42.         labTitle.setForeground(Color.blue);
  43.         labTitle.setFont(new Font("华文新魏",Font.PLAIN,25));
  44.         JLabel labServeraddress = new JLabel("你的邮箱服务器:");
  45.         JLabel labFrame1 = new JLabel();
  46.         labFrame1.setForeground(Color.black);
  47.         labFrame1.setBorder(BorderFactory.createEtchedBorder());
  48.         labFrame1.setBounds(new Rectangle(50,80,370,220));
  49.         
  50.         JLabel labFrame2 = new JLabel();
  51.         labFrame2.setForeground(Color.black);
  52.         labFrame2.setBorder(BorderFactory.createEtchedBorder());
  53.         labFrame2.setBounds(new Rectangle(50,330,370,100));
  54.         
  55.         
  56.         JLabel labAccount = new JLabel("你的帐号:");
  57.         JLabel labPassword = new JLabel("邮箱密码:");
  58.         JLabel labAdddress = new JLabel("收信人邮箱:");
  59.         JLabel labAffix = new JLabel("附件地址:");
  60.         JLabel labAffixCue=new JLabel("注:所有附件必须放入同一个文件夹中");
  61.         JButton butDir = new JButton("...");
  62.         JButton butOK = new JButton("发送");
  63.         
  64.         txtServeraddress = new JTextField(40);
  65.         txtAccount = new JTextField(40);
  66.         pasPassowrd = new JPasswordField(40);
  67.         txtAddress = new JTextField(40);
  68.         txtAffixPath = new JTextField(40);
  69.         
  70.         labTitle.setBounds(new Rectangle(50,30,400,30));
  71.         labServeraddress.setBounds(new Rectangle(60,100,140,30));
  72.         labAccount.setBounds(new Rectangle(100,150,80,30));
  73.         labPassword.setBounds(new Rectangle(100,200,80,30));
  74.         labAdddress.setBounds(new Rectangle(90,250,80,30));
  75.         
  76.         
  77.         txtServeraddress.setBounds(new Rectangle(190,100,200,30));
  78.         txtAccount.setBounds(new Rectangle(190,150,200,30));
  79.         pasPassowrd.setBounds(new Rectangle(190,200,200,30));
  80.         txtAddress.setBounds(new Rectangle(190,250,200,30));
  81.         txtAffixPath.setBounds(new Rectangle(140,350,200,30));
  82.         labAffix.setBounds(new Rectangle(60,350,80,30));
  83.         butDir.setBounds(new Rectangle(350,350,60,30));
  84.         labAffixCue.setBounds(new Rectangle(120,380,240,30));
  85.         butOK.setBounds(new Rectangle(220,450,80,30));
  86.         
  87.         
  88.         txtServeraddress.addActionListener(
  89.                 new ActionListener(){
  90.                     public void actionPerformed(ActionEvent arg0){
  91.                         if(!txtServeraddress.getText().matches("smtp.//w[^.]+.//w+")){
  92.                             JOptionPane.showMessageDialog( JavaMail.this
  93.                                     "非法的邮件服务器,例如:smtp.126.com""错误",
  94.                                     JOptionPane.ERROR_MESSAGE );
  95.                         }else{
  96.                             txtServeraddress.setFocusable(false);
  97.                             txtAccount.setFocusable(true);
  98.                             txtServeraddress.setFocusable(true);
  99.                         }
  100.                     }
  101.                 });
  102.         
  103.         txtAccount.addActionListener(new ActionListener(){
  104.             public void actionPerformed(ActionEvent arg0) {
  105.                 if(!txtAccount.getText().matches("//w+@//w[^.]+.//w+")){
  106.                 JOptionPane.showMessageDialog( JavaMail.this
  107.                         "非法的电子邮件""错误",
  108.                         JOptionPane.ERROR_MESSAGE );
  109.                 }else{
  110.                     txtAccount.setFocusable(false);
  111.                     pasPassowrd.setFocusable(true);
  112.                     txtAccount.setFocusable(true);
  113.                 }
  114.             }
  115.             
  116.         });
  117.         
  118.         pasPassowrd.addActionListener(new ActionListener(){
  119.             public void actionPerformed(ActionEvent arg0) {
  120.                 pasPassowrd.setFocusable(false);
  121.                 txtAddress.setFocusable(true);
  122.                 pasPassowrd.setFocusable(true);
  123.             }
  124.         });
  125.         
  126.         txtAddress.addActionListener(new ActionListener(){
  127.             public void actionPerformed(ActionEvent arg0) {
  128.                 if(!txtAddress.getText().matches("//w+@//w[^.]+.//w+")){
  129.                 JOptionPane.showMessageDialog( JavaMail.this
  130.                         "非法的电子邮件""错误",
  131.                         JOptionPane.ERROR_MESSAGE );
  132.                 }else{
  133.                     txtAddress.setFocusable(false);
  134.                     txtAffixPath.setFocusable(true);
  135.                     txtAddress.setFocusable(true);
  136.                 }
  137.             }
  138.         });
  139.         
  140.         butDir.addActionListener(new ActionListener(){
  141.             public void actionPerformed(ActionEvent arg0) {
  142.                 JFileChooser fileChooser = new JFileChooser();
  143.                   fileChooser.setFileSelectionMode(
  144.                      JFileChooser.DIRECTORIES_ONLY);
  145.                   int result = fileChooser.showOpenDialog( JavaMail.this );
  146.                   if ( result == JFileChooser.CANCEL_OPTION )
  147.                      return;
  148.                   File file = fileChooser.getSelectedFile();
  149.                   if ( file == null ||file.getName().equals( "" ) ){
  150.                          JOptionPane.showMessageDialog( JavaMail.this
  151.                             "请选择一个目录""错误",
  152.                             JOptionPane.ERROR_MESSAGE );
  153.                          return;
  154.                   }else{
  155.                       txtAffixPath.setText(file.getAbsolutePath());
  156.                   }
  157.             }
  158.         });
  159.         
  160.         butOK.addActionListener(
  161.             new ActionListener(){
  162.                 public void actionPerformed(ActionEvent arg0) {
  163.                     if ( txtAffixPath.getText().equals( "" ) ){
  164.                          JOptionPane.showMessageDialog( JavaMail.this
  165.                             "附件目录不能为空""错误",
  166.                             JOptionPane.ERROR_MESSAGE );
  167.                          return;
  168.                   }else{
  169.                       new Thread(new SendMail(txtServeraddress.getText(),
  170.                               txtAccount.getText(),pasPassowrd.getPassword().toString(),
  171.                               txtAddress.getText(),txtAffixPath.getText())).start();
  172.                   }
  173.                 }
  174.                 
  175.             }
  176.         );
  177.     
  178.         container.add(labFrame1);
  179.         container.add(labFrame2);
  180.         container.add(labServeraddress);
  181.         container.add(labAccount);
  182.         container.add(labPassword);
  183.         container.add(labAdddress);
  184.         container.add(labAffix);
  185.         container.add(butDir);
  186.         container.add(labAffixCue);
  187.         container.add(butOK);
  188.         container.add(txtServeraddress);
  189.         container.add(txtAccount);
  190.         container.add(pasPassowrd);
  191.         container.add(txtAddress);
  192.         container.add(txtAffixPath);
  193.         container.add(labTitle);
  194.     
  195.         this.setResizable(false);
  196.         setSize(500,600);
  197.         setVisible(true);
  198.         
  199.     }
  200.     public static void main(String[] args) {
  201.         new JavaMail().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  202.     }
  203. }
  204. SendMail .java
  205. import java.awt.Container;
  206. import java.awt.Rectangle;
  207. import java.io.File;
  208. import java.util.Date;
  209. import java.util.Properties;
  210. import java.util.Vector;
  211. import javax.activation.DataHandler;
  212. import javax.activation.FileDataSource;
  213. import javax.mail.Message;
  214. import javax.mail.MessagingException;
  215. import javax.mail.Multipart;
  216. import javax.mail.Session;
  217. import javax.mail.Transport;
  218. import javax.mail.internet.InternetAddress;
  219. import javax.mail.internet.MimeBodyPart;
  220. import javax.mail.internet.MimeMessage;
  221. import javax.mail.internet.MimeMultipart;
  222. import javax.swing.JDialog;
  223. import javax.swing.JOptionPane;
  224. import javax.swing.JProgressBar;
  225. /**
  226.  * @author 李 世贵
  227.  * 来源:http://blog.csdn.net/lishiguui
  228.  * 欢迎转接,请保留作者和来源,谢谢!
  229.  *
  230.  */
  231. public class SendMail extends JDialog implements Runnable{
  232.     
  233.     private String serveraddress = "";
  234.     private String from = "";
  235.     private String password = "";
  236.     private String to = "";
  237.     private String affixpath = "";
  238.     private Vector vecAffix = new Vector();
  239.     private long countfiles=0;
  240.     private JProgressBar bar=new JProgressBar();
  241.     private Container container;
  242.     public SendMail(String serveraddress, String from, String password, String to, String affixpath) {
  243.         super();
  244.         container = getContentPane();
  245.         container.setLayout(null);
  246.         this.serveraddress = serveraddress;
  247.         this.from = from;
  248.         this.password = password;
  249.         this.to = to;
  250.         this.affixpath = affixpath;
  251.         bar.setBounds(new Rectangle(10,40,180,20));
  252.         container.add(bar);
  253.         setSize(200,100);
  254.         setVisible(true);
  255.     }
  256.     
  257.     public void run(){
  258.         //获取所有附件
  259.         VisitFile(affixpath);
  260.         bar.setMaximum(vecAffix.size());
  261.         bar.setStringPainted(true);
  262.         //发送所有附件
  263.         for(int i = 0; i < vecAffix.size(); i++ ){
  264.             send(vecAffix.get(i),0);
  265.             bar.setValue(i);
  266.             container.update(this.getGraphics());
  267.         }
  268.         
  269.     }
  270.     //发送附件
  271.     public void send(String affixfile,int count){
  272.         try {
  273.             //设置系统属性
  274.             Properties properties=System.getProperties();
  275.             properties.put("mail.smtp.host", serveraddress);
  276.             properties.put("mail.smtp.auth""true");
  277.             //邮件会话对象
  278.             Session session=Session.getDefaultInstance(properties, new Email_auth(from, password));
  279.             //MIME邮件对象
  280.             MimeMessage mimemessage = new MimeMessage(session);
  281.             //设置发邮件地址
  282.             mimemessage.setFrom(new InternetAddress(from));
  283.             //设置收邮件地址
  284.             mimemessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  285.             //设置邮件主题
  286.             mimemessage.setSubject(new File(affixfile).getName(), "GBK");
  287.             //生成MimeBodyPart对象存放邮件内容
  288.             MimeBodyPart mbp1 = new MimeBodyPart();
  289.             mbp1.setText("欢迎使用李世贵附件批量发送系统","GBK");
  290.             生成MimeBodyPart对象存放附件存放附件
  291.             MimeBodyPart mbp2 = new MimeBodyPart();
  292.             FileDataSource fds = new FileDataSource(affixfile);
  293.             //添加附件内容
  294.             mbp2.setDataHandler(new DataHandler(fds));
  295.             //添加附件标题
  296.             mbp2.setFileName(fds.getName());
  297.             //Multipart对象是存放生成的MimeBodyPart对象
  298.             Multipart mp = new MimeMultipart();
  299.             mp.addBodyPart(mbp1);
  300.             mp.addBodyPart(mbp2);
  301.             mimemessage.setContent(mp);
  302.             //设置发送时邮件时间
  303.             mimemessage.setSentDate(new Date());
  304.             //发送邮件
  305.             Transport.send(mimemessage);
  306.         } catch (MessagingException e) {
  307.             if(count == 10){
  308.                 JOptionPane.showMessageDialog( this
  309.                         "发送失败""提示",
  310.                         JOptionPane.INFORMATION_MESSAGE );
  311.                 SendMail.this.dispose();
  312.             }else{
  313.                 send(affixfile, count+1);
  314.             }
  315.         }
  316.     }
  317.     //获取所有附件
  318.     public void VisitFile(String affixpath){
  319.         String[] directroy = new File(affixpath).list();
  320.         for(int i = 0; i < directroy.length; i++){
  321.           File f = new File(affixpath+"//"+directroy[i]);
  322.           if(f.isDirectory()){
  323.             VisitFile(f.toString());
  324.           }else{
  325.             countfiles++;
  326.             vecAffix.add(f.toString());
  327.           }     
  328.         }                   
  329.     }
  330. }
  331. Email_auth .java
  332. import javax.mail.Authenticator;
  333. import javax.mail.PasswordAuthentication;
  334. /**
  335.  * @author 李 世贵
  336.  * 来源:http://blog.csdn.net/lishiguui
  337.  * 欢迎转接,请保留作者和来源,谢谢!
  338.  *
  339.  */
  340. public class Email_auth extends Authenticator {
  341.     
  342.     String auth_user;
  343.     String auth_password;
  344.     public Email_auth() {
  345.         super();
  346.     }
  347.     public Email_auth(String user, String password) {
  348.         super();
  349.         setUsername(user);
  350.         setUserpass(password);
  351.         
  352.     }
  353.     public void setUsername(String username){
  354.         auth_user=username;
  355.     }
  356.     public void setUserpass(String userpass){
  357.         auth_password=userpass;
  358.     }
  359.     public PasswordAuthentication getPasswordAuthentication(){
  360.         return new PasswordAuthentication(auth_user,auth_password);
  361.         
  362.     }
  363.     
  364. }

运行程序后的界面:

邮件附件批量发送系统二_第1张图片

你可能感兴趣的:(原创java)