if (bClickThrough && oSessionProps.getProperty("webserver")==null) {
if (DebugFile.trace) DebugFile.decIdent();
throw new NullPointerException("SendMail.send() If clickthrough property is true then webserver property is required");
}
DBBind oDbb;
ArrayList<String> aWarnings = new ArrayList<String>();
// *******************************************
// Setup default values for missing parameters
if (null==oSessionProps) oSessionProps=oMacc.getProperties();
if (null==sEncoding) sEncoding = "UTF-8";
if (null==sReplyAddr) sReplyAddr = sFromAddr;
if (null==sRecipientType) sRecipientType = "to";
if (null==sId) sId = Gadgets.generateUUID();
if (null==sEnvCnfFileName) sEnvCnfFileName = "hipergate";
if (null==sJobTl) sJobTl = "";
final int nRecipients = aRecipients.length;
if (DebugFile.trace) DebugFile.writeln("recipient count is "+String.valueOf(nRecipients));
// Remove blank spaces, tabs and carriage return characters from e-mail address
// end make a limited attempt to extract a sanitized email address
// prefer text in <brackets>, ignore anything in (parentheses)
for (int r=0; r<nRecipients; r++) {
aRecipients[r] = MailMessage.sanitizeAddress(Gadgets.removeChars(aRecipients[r], " \t\r"));
if (!Gadgets.checkEMail(aRecipients[r])) {
if (DebugFile.trace) DebugFile.writeln("ERROR "+aRecipients[r]+" at line "+String.valueOf(r+1)+" is not a valid e-mail address");
aWarnings.add(aRecipients[r]+" at line "+String.valueOf(r+1)+" is not a valid e-mail address");
}
}
// Get mail from address
if (!Gadgets.checkEMail(sFromAddr)) {
aWarnings.add(sFromAddr+" is not a valid from e-mail address");
}
// Get mail reply-to address
if (!Gadgets.checkEMail(sReplyAddr)) {
aWarnings.add(sReplyAddr+" is not a valid reply-to e-mail address");
}
RecipientType oRecType;
if (sRecipientType.equalsIgnoreCase("cc"))
oRecType = RecipientType.CC;
else if (sRecipientType.equalsIgnoreCase("bcc"))
oRecType = RecipientType.BCC;
else if (sRecipientType.equalsIgnoreCase("to"))
oRecType = RecipientType.TO;
else
throw new MessagingException(sRecipientType+" is not a valid recipient type");
if (sJobTl.length()>0) {
if (null==oGlobalDbb)
oDbb = new DBBind(sEnvCnfFileName);
else
oDbb = oGlobalDbb;
JDCConnection oCon = null;
try {
Job oSnd;
DBPersist oJob = new DBPersist(DB.k_jobs,"Job");
oCon = oDbb.getConnection("SendMail",false);
oCon.setAutoCommit(false);
ACLUser oUsr = new ACLUser(oCon, oMacc.getString(DB.gu_user));
String sJobId = Job.getIdFromTitle(oCon, sJobTl, oUsr.getString(DB.gu_workarea));
if (null==sJobId) {
if (DebugFile.trace) DebugFile.writeln("Job "+sJobTl+" not found, creating a new one...");
if (oMacc.isNull(DB.gu_account)) {
if (DebugFile.trace) DebugFile.decIdent();
throw new SQLException("No MailAccount found for user "+oUsr.getString(DB.gu_user),"01S06");
}
String sMBoxDir = DBStore.MBoxDirectory(oDbb.getProfileName(),oUsr.getInt(DB.id_domain),oUsr.getString(DB.gu_workarea));
if (DebugFile.trace) DebugFile.writeln("mbox directory is "+sMBoxDir);
SessionHandler oHndl = new SessionHandler(oMacc,sMBoxDir);
DBStore oRDBMS = DBStore.open(oHndl.getSession(), oDbb.getProfileName(), sMBoxDir, oUsr.getString(DB.gu_user), oUsr.getString(DB.tx_pwd));
DBFolder oOutbox = oRDBMS.openDBFolder("outbox",DBFolder.READ_WRITE);
DBMimeMessage oMsg = DraftsHelper.draftMessage(oOutbox, oMacc.getString(DB.outgoing_server),
oUsr.getString(DB.gu_workarea),
oUsr.getString(DB.gu_user),
sTextHtml==null ? "plain" : "html");
String sMsgId = oMsg.getContentID();
if (null==sMsgId) throw new NullPointerException("MIME message identifier could not be set for message with GUID "+oMsg.getMessageGuid());
DraftsHelper.draftUpdate(oCon, oUsr.getInt(DB.id_domain),
oUsr.getString(DB.gu_workarea),
oMsg.getMessageGuid(), sMsgId,
sFromAddr,sReplyAddr,sFromPersonal,
sSubject, "text/"+(sTextHtml==null ? "plain" : "html")+";charset="+sEncoding,
(sTextHtml==null ? sTextPlain : sTextHtml), null, null, null);
if (aAttachments!=null) {
Integer oPart = DBCommand.queryMaxInt(oCon, DB.id_part, DB.k_mime_parts, DB.gu_mimemsg+"='"+oMsg.getMessageGuid()+"'");
if (oPart==null) oPart = new Integer(1);
PreparedStatement oStm = oCon.prepareStatement("INSERT INTO " + DB.k_mime_parts + "("+DB.gu_mimemsg+","+DB.id_message+","+DB.id_part+","+DB.id_disposition+","+DB.id_content+","+DB.id_type+","+DB.len_part+","+DB.de_part+","+DB.file_name+") VALUES ('"+oMsg.getMessageGuid()+"',?,?,'reference',?,?,?,?,?)");
for (int p=0; p<aAttachments.length; p++) {
String sFilePath = Gadgets.chomp(sUserDir,File.separator)+aAttachments[p];
File oAttach = new File(sFilePath);
if (oAttach.exists()) {
oStm.setString(1, sMsgId);
oStm.setInt(2, oPart.intValue()+p);
oStm.setString(3, DBMimePart.getMimeType(oCon,aAttachments[p]));
oStm.setString(4, DBMimePart.getMimeType(oCon,aAttachments[p]));
oStm.setInt(5, (int) oAttach.length());
oStm.setString(6, aAttachments[p]);
oStm.setString(7, sFilePath);
oStm.executeUpdate();
} // fi
} // next
oStm.close();
} // fi
sJobId = (sId.length()==32 ? sId : Gadgets.generateUUID());
oJob.put(DB.gu_job, sJobId);
oJob.put(DB.gu_workarea, oUsr.getString(DB.gu_workarea));
oJob.put(DB.gu_writer, oUsr.getString(DB.gu_user));
oJob.put(DB.id_command, Job.COMMAND_SEND);
oJob.put(DB.id_status, bAutoRunJob ? Job.STATUS_SUSPENDED : Job.STATUS_PENDING);
oJob.put(DB.tl_job, sJobTl);
oJob.put(DB.tx_parameters, (bTestMode ? "testmode:true," : "")+
"message:"+oMsg.getMessageGuid()+","+
"id:"+sMsgId+","+
"profile:"+oDbb.getProfileName()+","+
"account:"+oMacc.getString(DB.gu_account)+","+
"personalized:true"+","+
"attachimages:"+(bAttachImages ? "true" : "false")+","+
"clickthrough:"+(bClickThrough ? "true" : "false")+","+
"webbeacon:"+(bWebBeacon ? "true" : "false")+","+
"webserver:"+oSessionProps.getProperty("webserver")+","+
"encoding:"+sEncoding);
if (dtExecution!=null) oJob.put(DB.dt_execution, dtExecution);
oJob.store(oCon);
DBCommand.executeUpdate(oCon, "UPDATE "+DB.k_mime_msgs+" SET "+DB.gu_job+"='"+sJobId+"' WHERE "+DB.gu_mimemsg+"='"+oMsg.getMessageGuid()+"'");
oSnd = Job.instantiate(oCon, sJobId, oDbb.getProperties());
oSnd.insertRecipients(oCon, aRecipients, sRecipientType,
sTextHtml==null ? "text" : "html",
Job.STATUS_PENDING);
} else {
if (DebugFile.trace) DebugFile.writeln("Job "+sJobTl+" found with GUID "+sJobId);
oSnd = Job.instantiate(oCon, sJobId, oDbb.getProperties());
oSnd.insertRecipients(oCon, aRecipients, sRecipientType,
sTextHtml==null ? "text" : "html",
Job.STATUS_PENDING);
}
oCon.commit();
oCon.close("SendMail");
oCon = null;
if (bAutoRunJob) {
SingleThreadExecutor oSte = new SingleThreadExecutor(oDbb, sJobId);
oSte.registerCallback(SendMail.DEBUGLN);
oSte.registerCallback(SendMail.PRINTLN);
oSte.run();
} // bAutoRunJob
} catch (SQLException sqle) {
if (DebugFile.trace) DebugFile.writeln("SQLException "+sqle.getMessage());
aWarnings.add("SQLException "+sqle.getMessage());
if (null!=oCon) {
if (!oCon.isClosed()) {
if (!oCon.getAutoCommit()) oCon.rollback();
oCon.close("SendMail");
}
oCon = null;
}
if (null==oGlobalDbb && null!=oDbb) oDbb.close();
oDbb = null;
}
} else {
SessionHandler oSssnHndlr = new SessionHandler(oSessionProps);
ByteArrayOutputStream oByteOutStrm = new ByteArrayOutputStream();