posts - 0,  comments - 5,  trackbacks - 0
  1 /**
  2  *  @author  wufalong
  3  * Create on 2007-8-7
  4  * TODO To change the template for this generated type comment go to
  5  * Window - Preferences - Java - Code Style - Code Templates
  6   */

  7 import  java.io.BufferedReader;
  8 import  java.io.FileInputStream;
  9 import  java.io.FileReader;
 10 import  java.io.IOException;
 11 import  java.io.InputStream;
 12 import  java.util.Properties;
 13
 14 import  javax.mail.Authenticator;
 15 import  javax.mail.BodyPart;
 16 import  javax.mail.Message;
 17 import  javax.mail.Multipart;
 18 import  javax.mail.PasswordAuthentication;
 19 import  javax.mail.Session;
 20 import  javax.mail.Transport;
 21 import  javax.mail.internet.InternetAddress;
 22 import  javax.mail.internet.MimeBodyPart;
 23 import  javax.mail.internet.MimeMessage;
 24 import  javax.mail.internet.MimeMultipart;
 25
 26 import  jxl.Cell;
 27 import  jxl.Workbook;
 28
 29 import  org.dom4j.Document;
 30 import  org.dom4j.Element;
 31
 32 import  ReadXml.LoadRootElementByConfigFile;
 33
 34 public   class  SendMail  {
 35      public   static   void  main(String agrs[])
 36      {
 37         Document doc  =  LoadRootElementByConfigFile.getRootElement();
 38         Element e  =  doc.getRootElement().element( " sendEmail " );
 39         System.out.println( " ===begin read excel=== " );
 40         String contentFile  =  e.elementText( " content " );
 41          try
 42          {
 43             String content  =  getContet(contentFile);
 44             String sourcefile  =  e.elementText( " excelFile " );
 45             InputStream is  =   new  FileInputStream(sourcefile);
 46             jxl.Workbook rwb  =  Workbook.getWorkbook(is);
 47             jxl.Sheet rs  =  rwb.getSheet( 0 ); 
 48              int  num  =  rs.getRows(); // 得到总行数
 49              for ( int  j = 0 ;j < num;j ++ )
 50              {
 51                 Cell[] cell  =  rs.getRow(j);
 52                 String value  =  cell[ 0 ].getContents();
 53                 System.out.println( "  Send the  " + (j + 1 ) + "  Email Values: " + value);
 54                 sendMail(content,e,value);
 55             }

 56         }

 57          catch (Exception ex)
 58          {
 59             ex.printStackTrace();
 60         }

 61         System.out.println( " ===send end=== " );
 62     }

 63       public   static   void  sendMail(String content,Element e,String email) 
 64       {
 65           try  
 66           {
 67            String username = e.elementText( " userEmail " );
 68            String password = e.elementText( " password " );
 69            String title  =  e.elementText( " title " );
 70            String smtp_server = " smtp.163.com " ;
 71            String from_mail_address = username;
 72            String to_mail_address = email;
 73            Authenticator auth  =   new  PopupAuthenticator(username,password);
 74            Properties mailProps  =   new  Properties();
 75            mailProps.put( " mail.smtp.auth " " true " );
 76            mailProps.put( " username " , username);
 77            mailProps.put( " password " , password);
 78            mailProps.put( " mail.smtp.host " , smtp_server);
 79            Session mailSession  =  Session.getDefaultInstance(mailProps, auth);
 80            MimeMessage  message  =   new  MimeMessage(mailSession);
 81            message.setFrom( new  InternetAddress(from_mail_address));
 82            message.setRecipient(Message.RecipientType.TO,  new  InternetAddress(to_mail_address));
 83            message.setSubject(title);
 84            Multipart mp  =   new  MimeMultipart();
 85            BodyPart textBodyPart  =   new  MimeBodyPart();
 86            textBodyPart.setContent(content, " text/html; charset=GBK " );
 87            mp.addBodyPart(textBodyPart);
 88            message.setContent(mp);
 89            message.saveChanges();
 90            Transport.send(message);
 91          }
  catch  (Exception ex) 
 92           {
 93            System.err.println( " 邮件发送失败的原因是: "   +  ex.getMessage());
 94            System.err.println( " 具体错误原因: " );
 95            ex.printStackTrace(System.err);
 96          }

 97      }

 98      
 99       public   static  String getContet(String contentFile)
100       {
101          StringBuffer sb  =   new  StringBuffer( "" );
102           try
103          
104              FileReader reader  =   new  FileReader(contentFile); 
105              BufferedReader br  =   new  BufferedReader(reader); 
106              String s1  =   null
107               int  line = 0
108               while ((s1  =  br.readLine())  !=   null
109              
110                   ++ line; 
111                  sb  =  sb.append(s1);
112              }
 
113              br.close(); 
114              reader.close(); 
115          }
catch (IOException e)
116          
117              e.printStackTrace();
118          }
 
119           return  sb.toString();
120      }

121 }

122
123 class  PopupAuthenticator  extends  Authenticator 
124 {
125       private  String username;
126       private  String password;
127       public  PopupAuthenticator(String username,String password)
128       {
129           this .username = username;
130           this .password = password;
131      }

132       public  PasswordAuthentication getPasswordAuthentication() 
133       {
134           return   new  PasswordAuthentication( this .username,  this .password);
135      }

136 }
 
posted on 2007-09-03 14:06 crazy 阅读(594) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: