PreparedStatement oStm = null;
ResultSet oRst = null;
MimeSender oSnd = null;
SessionHandler oHnl = null;
DBStore oSto = null;
DBFolder oFld = null;
DBSubset oLists = new DBSubset(DB.k_lists,DB.gu_list,DB.gu_list+"=? OR "+DB.de_list+"=?",1);
DBSubset oMaccs = new DBSubset(DB.k_user_mail,DB.gu_account,DB.gu_account+"=? OR "+DB.tl_account+"=?",1);
if (args==null) {
System.out.println("Mail batch descriptor is required");
} else if (args.length==0) {
System.out.println("Mail batch descriptor is required");
} else {
try {
System.out.println("Connecting to database...");
oDbb = new DBBind(args[0]);
System.out.println("Database connection was successfull");
if (oDbb.getProperty("storage")==null) {
System.out.println("storage property is required but not found at "+args[0]+".cnf file");
nErrs++;
} else {
if (oDbb.getProperty("mail.list")==null) {
System.out.println("mail.list property is required but not found at "+args[0]+".cnf file");
nErrs++;
} else {
String sMailingDir = Gadgets.chomp(oDbb.getProperty("storage"), File.separator)+"mailing"+File.separator+oDbb.getProperty("mail.list");
String sBody = null;
String sType = null;
String sBodyFile = sMailingDir+File.separator+"body.htm";
if (new File(sMailingDir+File.separator+"body.htm").exists()) {
System.out.println("Trying to read file "+sMailingDir+File.separator+"body.htm");
sBody = oFs.readfilestr("file://"+sMailingDir+File.separator+"body.htm","UTF-8");
sType = "html";
} else if (new File("file://"+sMailingDir+File.separator+"body.txt").exists()) {
System.out.println("Trying to read file "+sMailingDir+File.separator+"body.txt");
sBody = oFs.readfilestr("file://"+sMailingDir+File.separator+"body.txt","UTF-8");
sType = "plain";
}
if (null==sBody) {
System.out.println("Could not find body.htm nor body.txt files at directory "+sMailingDir);
nErrs++;
} else {
oCon = oDbb.getConnection("MimeSender.main()");
oCon.setAutoCommit(false);
int nLists = oLists.load(oCon, new Object[]{oDbb.getProperty("mail.list").trim(),oDbb.getProperty("mail.list").trim()});
if (nLists==0) {
System.out.println("Mailing list "+oDbb.getProperty("mail.list")+" not found at k_lists table");
nErrs++;
} else if (nLists>1) {
System.out.println("Ambiguous name for mailing list "+oDbb.getProperty("mail.list"));
nErrs++;
} else {
Date dtNow = new Date();
if (oDbb.getProperty("mail.account")==null) {
System.out.println("mail.account property is required but not found at "+args[0]+".cnf file");
nErrs++;
} else {
int nMaccs = oMaccs.load(oCon, new Object[]{oDbb.getProperty("mail.account").trim(),oDbb.getProperty("mail.account").trim()});
if (nMaccs==0) {
System.out.println("Mailing account "+oDbb.getProperty("mail.account")+" not found at k_user_mail table");
nErrs++;
} else if (nMaccs>1) {
System.out.println("Ambiguous name for mailing account "+oDbb.getProperty("mail.account"));
nErrs++;
} else {
System.out.println("Composing message...");
DistributionList oLst = new DistributionList(oCon, oLists.getString(0,0));
oLists = null;
System.out.println("Getting mail account...");
MailAccount oAcc = new MailAccount(oCon, oMaccs.getString(0,0));
ACLUser oUsr = new ACLUser(oCon, oAcc.getString(DB.gu_user));
System.out.println("Got mail");
String sMBoxDir = DBStore.MBoxDirectory(oDbb.getProfileName(),
oUsr.getInt(DB.id_domain),
oUsr.getString(DB.gu_workarea));
System.out.println("mbox directory is "+sMBoxDir);
System.out.println("Opening mail session... ");
oHnl = new SessionHandler(oAcc);
oSto = DBStore.open (oHnl.getSession(), oDbb.getProfileName(), sMBoxDir,
oUsr.getString(DB.gu_user), oUsr.getString(DB.tx_pwd));
oFld = oSto.openDBFolder("outbox", DBFolder.READ_WRITE);
System.out.println("Creating message template...");
DBMimeMessage oMsg = DraftsHelper.draftMessage(oFld, oDbb.getProperty("mail.host","127.0.0.1"),
oUsr.getString(DB.gu_workarea),
oUsr.getString(DB.gu_user), sType);
DraftsHelper.draftUpdate (oCon, oUsr.getInt(DB.id_domain),
oUsr.getString(DB.gu_workarea),
oMsg.getMessageGuid(),
DBCommand.queryStr(oCon, "SELECT "+DB.id_message+" FROM "+DB.k_mime_msgs+
" WHERE "+DB.gu_mimemsg+"='"+oMsg.getMessageGuid()+"'"),
oLst.getString(DB.tx_from),
oLst.getStringNull(DB.tx_reply,oLst.getString(DB.tx_from)),
oLst.getStringNull(DB.tx_sender,oLst.getString(DB.tx_from)),
oLst.getStringNull(DB.tx_subject,""),
"text/"+sType+"; charset=utf-8",
sBody, null, null, null);
System.out.println("Message template successfully composed");
String sGuJob = null;
if (null!=oDbb.getProperty("mail.job.title")) {
oStm = oCon.prepareStatement("SELECT "+DB.gu_job+" FROM "+DB.k_jobs+" WHERE "+
DB.tl_job+"=? AND "+DB.gu_workarea+"=?");
oStm.setString(1, oDbb.getProperty("mail.job.title"));
oStm.setString(2, oLst.getString(DB.gu_workarea));
oRst = oStm.executeQuery();
if (oRst.next()) sGuJob = oRst.getString(1);
oRst.close();
oRst=null;
oStm.close();
oStm=null;
} // fi
if (null==sGuJob) {
oSnd = newInstance(oCon, oLst.getString(DB.gu_list),
oLst.getString(DB.gu_workarea),
oUsr.getString(DB.gu_user), dtNow,
Job.STATUS_RUNNING,
oDbb.getProperty("mail.job.title",oLst.getStringNull(DB.de_list,"")+" "+dtNow.toString()),
"profile:"+oDbb.getProfileName()+
",id:"+oMsg.getMessageID()+
",message:"+oMsg.getMessageGuid()+
",account:"+oAcc.getString(DB.gu_account)+
",gu_list:"+oLst.getString(DB.gu_list)+
",personalized:"+String.valueOf(true)+
",bo_attachimages:1");
} else {
oSnd = new MimeSender();
oSnd.load(oCon, sGuJob);
oSnd.setStatus(oCon, Job.STATUS_RUNNING);
}
oSnd.setDataBaseBind(oDbb);
oFld.close(false);
oFld=null;
oSto.close();
oSto=null;
oHnl.close();
oHnl=null;
System.out.println("Loading queue...");
if (null==sGuJob) {
AtomFeeder oAfd = new AtomFeeder();
oAfd.loadAtoms (oCon, oSnd.getString(DB.gu_job), Atom.STATUS_RUNNING);
}
oCon.commit();
oSnd.setPending(DBCommand.queryInt(oCon,"SELECT COUNT(*) FROM "+DB.k_job_atoms+" WHERE "+DB.gu_job+"='"+oSnd.getString(DB.gu_job)+"' AND "+DB.id_status+"="+String.valueOf(Atom.STATUS_RUNNING)));
System.out.println("Queue loaded with "+String.valueOf(oSnd.pending())+" mails to be sent");
oStm = oCon.prepareStatement("SELECT a.*, j." + DB.tx_parameters + " FROM " + DB.k_job_atoms + " a, " + DB.k_jobs + " j WHERE a." + DB.id_status + "=" + String.valueOf(Atom.STATUS_RUNNING) + " AND j." + DB.gu_job + "=a." + DB.gu_job + " AND j." + DB.gu_job + "=? ORDER BY "+DB.pg_atom,
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
oStm.setString(1, oSnd.getString(DB.gu_job));
oRst = oStm.executeQuery();
ResultSetMetaData oMdt = oRst.getMetaData();
boolean bHasNext = oRst.next();
while (bHasNext) {
Atom oAtm = new Atom(oRst, oMdt);
oAtm.replace(DB.id_format,sType);
try {
oSnd.process(oAtm);
oAtm.archive(oCon);
oCon.commit();
nSent++;
System.out.println("Mail number "+String.valueOf(oAtm.getInt(DB.pg_atom))+" "+oAtm.getString(DB.tx_email)+" sent OK");
} catch (SQLException sqle) {
nErrs++;
System.out.println("Mail number "+String.valueOf(oAtm.getInt(DB.pg_atom))+" "+oAtm.getString(DB.tx_email)+" failed with SQLException "+sqle.getMessage());
if (DebugFile.trace) DebugFile.writeln("SQLException at atom "+String.valueOf(oAtm.getInt(DB.pg_atom))+" "+sqle.getMessage()+" "+StackTraceUtil.getStackTrace(sqle));
try { oCon.rollback(); oAtm.setStatus(oCon, Atom.STATUS_INTERRUPTED, "SQLException "+sqle.getMessage()); oCon.commit(); } catch (SQLException ignore) { }
oRst.close(); oRst=null;
oStm.close(); oStm=null;
break;
} catch (NullPointerException npe) {
nErrs++;
System.out.println("Mail number "+String.valueOf(oAtm.getInt(DB.pg_atom))+" "+oAtm.getString(DB.tx_email)+" failed with NullPointerException "+npe.getMessage());
if (DebugFile.trace) DebugFile.writeln("NullPointerException at atom "+String.valueOf(oAtm.getInt(DB.pg_atom))+" "+npe.getMessage()+" "+StackTraceUtil.getStackTrace(npe));
try { oCon.rollback(); oAtm.setStatus(oCon, Atom.STATUS_INTERRUPTED, "NullPointerException "+npe.getMessage()); oCon.commit(); } catch (SQLException ignore) { }
oRst.close(); oRst=null;
oStm.close(); oStm=null;
break;
} catch (MessagingException msge) {
nErrs++;
System.out.println("Mail number "+String.valueOf(oAtm.getInt(DB.pg_atom))+" "+oAtm.getString(DB.tx_email)+" failed with MessagingException "+msge.getMessage());
if (DebugFile.trace) DebugFile.writeln(msge.getClass().getName()+" at atom "+String.valueOf(oAtm.getInt(DB.pg_atom))+" "+msge.getMessage()+" "+StackTraceUtil.getStackTrace(msge));
try { oCon.rollback(); oAtm.setStatus(oCon, Atom.STATUS_INTERRUPTED, "MessagingException "+msge.getMessage()); oCon.commit(); } catch (SQLException ignore) { }
} finally {
bHasNext = oRst.next();
}
} //wend
if (null!=oRst) oRst.close();
oRst=null;
if (null!=oStm) oStm.close();
oStm=null;
if (oSnd.getStatus()==Job.STATUS_RUNNING) {
System.out.println("Finishing job...");
oSnd.setStatus(oCon, Job.STATUS_FINISHED);
} else {
System.out.println("Job finished abnormaly.");
}
oSnd=null;
} // fi
}
} // fi
oCon.close("MimeSender.main()");
oCon=null;
}
} // fi
}
System.out.println("Disconnecting from database...");
oDbb.close();
oDbb=null;
if (nErrs==0)
System.out.println("Bulk mailing sucessfully finished, "+String.valueOf(nSent)+" messages sent");
else
System.out.println("Bulk mailing finished with errors, "+String.valueOf(nSent)+" message"+(nSent==1 ? "s" : "")+" sent and "+String.valueOf(nErrs)+" error"+(nErrs==1 ? "s" : ""));
} catch (Exception xcpt) {
System.out.println(xcpt.getClass().getName()+" "+xcpt.getMessage());
System.out.println(StackTraceUtil.getStackTrace(xcpt));
if (oFld!=null) try { oFld.close(false); } catch (MessagingException ignore) { }
if (oSto!=null) try { oSto.close(); } catch (MessagingException ignore) { }
if (oHnl!=null) try { oHnl.close(); } catch (MessagingException ignore) { }
if (oSnd!=null) {
try {
if (null!=oCon) if (!oCon.isClosed()) oSnd.setStatus(oCon, Job.STATUS_INTERRUPTED);