天天看點

Java郵件群發系統

最近工作需要,做一個郵件群發系統,功能實作郵件群發,右鍵删除全選等功能,大緻設計如下:

smtp設定視窗:

發送頁面:

導入功能設計:

 final JButton importButton = new JButton("導入位址");

        importButton.addActionListener(new ActionListener(){

         //添加事件

            public void actionPerformed(ActionEvent e){

             //TODO 讀取excel檔案,寫入list

             JFileChooser fc=new JFileChooser();  

             int i=fc.showOpenDialog(scrollPane);  

             if(i==JFileChooser.APPROVE_OPTION){   

              File file=fc.getSelectedFile();

              if(file.toString().endsWith("xls")){

               String URL=file.getPath();   

                  try{

                   List<List<String>> list = ExcelCommonUtil.readExcelCommon(file.toString(),true);

                   if(list==null || list.size()==0){

                    JOptionPane.showMessageDialog(null,"電子表格内容為空!");

                    return;

                   }

                   int listLength = list.size();

                       String [][] tableVales= new String[listLength][list.get(0).size()]; //資料

                       for(int j = 0; j < listLength;j++){

                          if(list.get(j).size()>0){

                           String id = OperateProperties.genId();

                       //tableVales[j][1]=list.get(j).get(excelCloumnId);

                       tableVales[j][1]=id;

                       if(list.get(j).size()>1){

                         tableVales[j][2]=list.get(j).get(excelCloumnEmail);

                        }else{

                         tableVales[j][2]= "";

                        }

                          }

                          int rowCount = table.getRowCount() +1;   //行數加上1

                       Object[] temp = {String.valueOf(rowCount),tableVales[j][1],tableVales[j][2],true,INITSTATUS};

                       tableModel.addRow(temp);

                        }

                       table.updateUI();

                  }catch(Exception e1){

                   e1.printStackTrace();

                   JOptionPane.showMessageDialog(null,"讀取電子表格檔案異常!");

                  }

              }else{

               JOptionPane.showMessageDialog(null,"必須是excel2003電子表格檔案!");

              }

             }

            }

        });

        panel.add(importButton);

編輯發送功能:

大緻功能就這些。

  final JButton sendButton = new JButton("編輯發送");

        sendButton.addActionListener(new ActionListener(){

              username = OperateProperties.ReadProperties(filename, "username");

              password = OperateProperties.ReadProperties(filename, "password");

              smtp = OperateProperties.ReadProperties(filename, "smtp");

              String excelCloumn = OperateProperties.ReadProperties(filename, "excelCloumnEmail");

              excelCloumnEmail = Integer.parseInt(excelCloumn);

             if(table.getRowCount()==0){

              JOptionPane.showMessageDialog(null,"電子郵件位址為空!");

              return;

               //TODO 擷取複選框值 tempList 供調用

           for(int i = 0;i<table.getRowCount();i++){

            if((Boolean)tableModel.getValueAt(i,3)){

             int key = i;

             String value = (String)tableModel.getValueAt(i, 2);

             tempMap.put(key, value);

            }

           }

           if(tempMap.size()<=0){

            JOptionPane.showMessageDialog(null,"請選擇要發送的郵件位址!");

            return;

           new Message();

           title = Message.getEmailCap();

                 content = Message.getContent();

                 fujian = Message.getFujian();

                 color = new String[table.getRowCount()];

                 if(title!=null&& content!=null){

                  buttonActionPerformed(e);

                 }

                 Message.setEmailCap(null);

                 Message.setContent(null);

                 Message.setFujian(null);

        panel.add(sendButton);

關鍵代碼:

1.右鍵功能:

聲明郵件菜單:

private JPopupMenu popupMenu;

初始化郵件菜單内容:

 //右鍵

        popupMenu = new JPopupMenu();

     del = new JMenuItem("删除");

     slectAll = new JMenuItem("全選");

     popupMenu.add(del);

     popupMenu.add(slectAll);

添加事件:

//删除事件

     del.addActionListener(new ActionListener() {     

      public void actionPerformed(ActionEvent e) {    

        int selectedRow = table.getSelectedRow();//獲得選中行的索引

                 if(selectedRow!=-1)  //存在選中行

                 {

                  String id = (String) tableModel.getValueAt(selectedRow, 0);

                  String genId = (String) tableModel.getValueAt(selectedRow, 1);

                  String email = (String) tableModel.getValueAt(selectedRow, 2);

                  boolean select = (Boolean) tableModel.getValueAt(selectedRow, 3);

                  String status = (String) tableModel.getValueAt(selectedRow, 4);

                  Object[] temp = {id,genId,email,select,status};

                     tableModel.removeRow(temp);  //删除行

                     //強制重新整理界面

         table.updateUI();

                 }else{

                  JOptionPane.showMessageDialog(null,"請選擇一行");

                 }

     }});