运到盛时须警醒,境当逆处要从容

我的微博http://weibo.com/chrisfuz
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

简单邮件发送

Posted on 2010-11-21 13:46  Chris_Fu  阅读(242)  评论(0)    收藏  举报

1.需要从txt file读地址

2.发送

3.发送结果写入 另一个txt file里 

开发平台:windows 7
开发工具:Myeclipse 6.0

4.包

    activation.jar    common-mail.jar  mail.jar

5. Related Knowledge:

 (1)I/O    (2)smtp协议      (3)单线程
师傅讲的如何去掉乱码http://www.zaidaow.com/videos/detail/L201011112000017B.html

 

代码
1 package cn.java.com;
2
3 import java.io.BufferedReader;//从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。


4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileNotFoundException;
7 import java.io.FileReader;
8 import java.io.FileWriter;
9 import java.io.IOException;
10 import java.io.UnsupportedEncodingException;
11 import org.apache.commons.mail.EmailException;
12 import org.apache.commons.mail.SimpleEmail;
13
14 public class Mail {
15 //write into file
16 public void writeFileLines(String newStr) throws FileNotFoundException, EmailException,InterruptedException, UnsupportedEncodingException
17 {
18 String filein = newStr + "\r\n";
19 try{
20 FileWriter newfile = new FileWriter("g:/log.txt", true);
21 BufferedWriter bw = new BufferedWriter(newfile);
22 bw.newLine();
23 bw.write(filein);
24 bw.close();
25 }catch(IOException e1){
26 e1.printStackTrace();
27 }
28
29 }
30
31 //read from file
32 public void readFileByLines(String fileName) throws FileNotFoundException, EmailException, InterruptedException, UnsupportedEncodingException
33 {
34 File file=new File(fileName);
35
36 BufferedReader reader=null;
37
38 FileReader fileReader=new FileReader(file);
39
40 try
41 {
42 reader=new BufferedReader(fileReader);
43 String tempString=null;
44 int line=1;
45 int counts=1;
46 while((tempString=reader.readLine())!=null)
47 {
48 System.out.println(""+line+""+":"+tempString);
49 writeFileLines(""+line+""+":"+tempString);
50 //开始发送
51 sendMessage(tempString);
52 counts++;
53 //logSupport.
54 System.out.println("正准备发送第"+counts+"封邮件,请等待一分钟..................");
55 //停止一分钟
56 Thread.sleep(10*1000);
57
58 line++;
59
60 }
61 System.out.println("你的邮件已经全部都发完了,共发了"+(counts-1)+"");
62 writeFileLines("你的邮件已经全部都发完了,共发了"+(counts-1)+"");
63 reader.close();
64 }catch(IOException ex)
65 {
66 ex.printStackTrace();
67 }finally
68 {
69 if(reader!=null)
70 {
71 try {
72 reader.close();
73 } catch (IOException e) {
74 // TODO Auto-generated catch block
75 e.printStackTrace();
76 }
77 }
78 }
79 }
80
81 //开始发送
82
83 public void sendMessage(String towho) throws EmailException, FileNotFoundException, UnsupportedEncodingException, InterruptedException
84 {
85 SimpleEmail email = new SimpleEmail();
86
87 email.setHostName("smtp.126.com");//邮件服务器
88 email.setAuthentication("fgangu", "13081015681");//smtp认证的用户名和密码
89 email.addTo(towho,"巨人");//收信者
90
91 email.setFrom("fgangu@126.com","小汪");//发信者 :小汪为发邮件的显示姓名
92 email.setSubject("猫咪的测试邮件");//标题
93 email.setCharset("UTF-8");//编码格式
94 StringBuffer sb=new StringBuffer();
95 sb.append("尊敬的用户你好:\r\n"+"");
96 sb.append("这是一封猫咪的测试邮件");
97
98 email.setMsg(sb.toString());//内容
99 email.send();//发送
100
101 System.out.println("发送成功");
102 writeFileLines("success");
103
104 }
105
106 public static void main(String[] args) throws EmailException {
107
108 Mail mail=new Mail();
109 try {
110 mail.readFileByLines("g:/1.txt");
111 } catch (FileNotFoundException e) {
112 // TODO Auto-generated catch block
113 e.printStackTrace();
114 } catch (UnsupportedEncodingException e) {
115 // TODO Auto-generated catch block
116 e.printStackTrace();
117 } catch (InterruptedException e) {
118 // TODO Auto-generated catch block
119 e.printStackTrace();
120 }
121
122
123 }
124
125 }
126