www.pudn.com > Javamail.rar > AutoSend.java
package com.digipower.automail.mailsender; /** *Title:
*Description: TestStruts
*Copyright: Copyright (c) 2004
*Company: foxconn
* @author conti * @version 1.0 */ import java.io.*; import java.util.*; import com.digipower.automail.AutoMailConstants; import java.sql.*; import com.digipower.common.basic.*; import java.io.*; public class AutoSend { private Connection conn = null; private ConnectionDB_Oracle db = null; private Statement stmt = null; private ResultSet rs = null; private PreparedStatement pstmt = null; private String strSql = null; private String strCom = ""; private String strCustomer = ""; public void autoSend(String FromAddress, String ReplyAddress, String ReplyToAddress) { try { db = new ConnectionDB_Oracle(); conn = db.getConnection(); conn.setAutoCommit(false); //获得未发送邮件的目录,主题,是否需要回条 GetMailList servers = new GetMailList(); ArrayList mailAddressList=new ArrayList(); mailAddressList=(ArrayList)(servers.getFolderName()) ; ArrayList subjectList = new ArrayList(); subjectList= (ArrayList)(servers.getSubject()) ; ArrayList replyList = new ArrayList(); replyList = (ArrayList)(servers.getReply()); System.out.println("mailAddressList.size():" + mailAddressList.size()); for (int i = 0; i < mailAddressList.size(); i++) { String addrFile = AutoMailConstants.BOOTDIR + mailAddressList.get(i) + AutoMailConstants.MAIL_ADDRESS_FILE; File f = new File(addrFile); if (f.exists( )) { FileReader fr = new FileReader(addrFile); BufferedReader br = new BufferedReader(fr); String strRead = ""; String strSend = ""; while((strRead = br.readLine()) != null) { strSend += strRead; } String mailAddrContent =""; mailAddrContent=strSend; //System.out.println(mailAddrContent); ArrayList addressList= getMailAddr (mailAddrContent); try { //send to manager start String[] MANAGER_ADDRESS = {"conti@power.com", "jack@power.com" }; for(int m=0; m0 && toAddress[k].indexOf(".") > 0 && (toAddress[k].endsWith(".com.cn") || toAddress[k].endsWith(".net") || toAddress[k].endsWith(".com") ||toAddress[k].endsWith(".tw") || toAddress[k].endsWith(".hk") || toAddress[k].endsWith(".org") || toAddress[k].endsWith(".cn"))) { try { SendMail sendmail = new MailSendHtml(AutoMailConstants.HOST_NAME, AutoMailConstants.USER_NAME, AutoMailConstants.PSW);//每发一封信,都要new一个session sendmail.setSubject((subjectList.get(i)).toString().trim()); sendmail.setSendDate(new java.util.Date()); String unSendMailPath = AutoMailConstants.BOOTDIR + mailAddressList.get(i) + AutoMailConstants.UPLOAD_FILE_DIRECTORY; sendmail.setUnSendMailPath(unSendMailPath); //设置待发邮件的路径 String htmlText = ""; htmlText=sendmail.getHtmlFromFile(unSendMailPath + AutoMailConstants.UPLOAD_FILE); //sendmail.setMailContent(plainText); //System.out.println(htmlText); sendmail.setMailContent(htmlText); String [] files; //发送附件的部分 File dir=new File(AutoMailConstants.BOOTDIR + mailAddressList.get(i) + AutoMailConstants.ATTACH_FILES_DIRECTORY); files=dir.list(); for (int j=0;j 0 && strCustomer != null && strCustomer.length() > 0) { pstmt.setString(1, mailAddressList.get(i).toString()); pstmt.setString(2, new String(strCom.getBytes())); pstmt.setString(3, new String(strCustomer.getBytes())); pstmt.setString(4, toAddress[k]); pstmt.executeUpdate(); conn.commit(); } } } } catch(Exception ee) { System.out.println(ee + "000000fail address insert error."); } } } else { //System.out.println("WrongAddress111111:" + toAddress[k] + mailAddressList.get(i)); try { int iNum = 0; //db = new ConnectionDB_Oracle(); //conn = db.getConnection(); //conn.setAutoCommit(false); strSql = "insert into FailureAddressLog values(?, ?, ?, ?)"; pstmt = conn.prepareStatement(strSql); getComCustomer(mailAddressList.get(i).toString(), toAddress[k].toString()); strSql = "select count(*) as num from FailureAddressLog where FolderNameF = '" + mailAddressList.get(i) + "' and company = '" + strCom + "' and customer = '" + strCustomer + "'"; stmt = conn.createStatement(); rs = stmt.executeQuery(strSql); if (rs.next()) { iNum = rs.getInt("num"); if (iNum <= 0) { if (strCom != null && strCom.length() > 0 && strCustomer != null && strCustomer.length() > 0) { pstmt.setString(1, mailAddressList.get(i).toString()); pstmt.setString(2, new String(strCom.getBytes())); pstmt.setString(3, new String(strCustomer.getBytes())); pstmt.setString(4, toAddress[k]); pstmt.executeUpdate(); conn.commit(); } } } } catch(Exception e) { System.out.println(e + "11111fail address insert error."); } } } servers.sendOver((String)(mailAddressList.get(i)));//update the isSend and sendDate of table } catch (Exception ex) { ex.printStackTrace(); //servers.sendOver((String)(mailAddressList.get(i))); servers.disconn(); } } } servers.disconn(); } catch(Exception e) { System.out.print(e); } finally { try { if (rs != null) { rs.close(); } if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); System.out.println("Autosend conn close"); } System.out.println("AutoSend finally"); } catch (Exception e) { e.printStackTrace(); } } } //-------------------- public static ArrayList getMailAddr(String mailAddr) { String str=mailAddr; StringTokenizer tokensA = new StringTokenizer( str,AutoMailConstants.ADDRESS_SEPARATOR); ArrayList list = new ArrayList(); while( tokensA.hasMoreTokens() ) { StringTokenizer tokensB = new StringTokenizer( tokensA.nextToken(),AutoMailConstants.SEPARATOR ); int count = 0; while( tokensB.hasMoreTokens() ) { count ++; if( count == 3 ) { list.add( tokensB.nextElement()); break; } tokensB.nextToken(); } } return list; } //Get the content of company and customer void getComCustomer(String Folder, String Address){ String strRead = ""; String strContent = ""; String strTemp = null; try { String addrFile = AutoMailConstants.BOOTDIR + Folder + AutoMailConstants.MAIL_ADDRESS_FILE; File f = new File(addrFile); if (f.exists()) { // FileReader fr = new FileReader(addrFile); // BufferedReader br = new BufferedReader(fr); FileInputStream fis = new FileInputStream(f); InputStreamReader isr = new InputStreamReader(fis, "UTF8"); BufferedReader br = new BufferedReader(isr); while ( (strRead = br.readLine()) != null) { strContent += strRead; } int iEnd = strContent.indexOf(Address); if(iEnd > 0) { strContent = strContent.substring(0, iEnd - 2); iEnd = strContent.lastIndexOf(AutoMailConstants.SEPARATOR); strCom = strContent.substring(iEnd + 2, strContent.length()); strContent = strContent.substring(0, iEnd); iEnd = strContent.lastIndexOf(";"); strCustomer = strContent.substring(iEnd + 1, strContent.length()); System.out.println("strCustomer:" + strCustomer); System.out.println("strCom:" + strCom); } br.close(); isr.close(); fis.close(); } } catch(Exception e) { e.printStackTrace(); } } }