String sTextBody, String sHtmlBody,
String sId, String [] aAttachmentsPath,
String sBasePath)
throws IOException,MessagingException,IllegalArgumentException,SecurityException {
PatternCompiler oCompiler = new Perl5Compiler();
PatternMatcher oMatcher = new Perl5Matcher();
String sContentType = (sHtmlBody==null ? "plain" : "html");
if (DebugFile.trace) {
DebugFile.writeln("Begin SessionHandler.composeMessage("+sSubject+","+sEncoding+",...,"+sId+","+sContentType+")");
DebugFile.incIdent();
}
if (sEncoding==null) sEncoding = "ASCII";
String sCharEnc = Charset.forName(sEncoding).name();
SMTPMessage oSentMessage = new SMTPMessage(getSmtpSession());
MimeBodyPart oMsgPlainText = new MimeBodyPart();
MimeMultipart oSentMsgParts = new MimeMultipart("mixed");
if (sContentType.equalsIgnoreCase("html")) {
MimeMultipart oHtmlRelated = new MimeMultipart("related");
MimeMultipart oTextHtmlAlt = new MimeMultipart("alternative");
// ************************************************************************
// Replace image CIDs
HashMap oDocumentImages = new HashMap(23);
StringSubstitution oSrcSubs = new StringSubstitution();
Parser oPrsr = Parser.createParser(sHtmlBody, sEncoding);
String sCid, sSrc;
try {
if (sTextBody==null) {
// ****************************
// Extract plain text from HTML
if (DebugFile.trace) DebugFile.writeln("new StringBean()");
StringBean oStrBn = new StringBean();
try {
oPrsr.visitAllNodesWith (oStrBn);
} catch (ParserException pe) {
throw new MessagingException(pe.getMessage(), pe);
}
sTextBody = oStrBn.getStrings();
oStrBn = null;
} // fi (sTextBody==null)
// *******************************
// Set plain text alternative part
oMsgPlainText.setDisposition("inline");
oMsgPlainText.setText(sTextBody, sCharEnc, "plain");
// oMsgPlainText.setContent(sTextBody, "text/plain; charset="+sCharEnc);
if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/alternative).addBodyPart(text/plain)");
oTextHtmlAlt.addBodyPart(oMsgPlainText);
// *****************************************
// Iterate images from HTML and replace CIDs
NodeList oCollectionList = new NodeList();
TagNameFilter oImgFilter = new TagNameFilter ("IMG");
for (NodeIterator e = oPrsr.elements(); e.hasMoreNodes();)
e.nextNode().collectInto(oCollectionList, oImgFilter);
final int nImgs = oCollectionList.size();
if (DebugFile.trace) DebugFile.writeln("NodeList.size() = " + String.valueOf(nImgs));
for (int i=0; i<nImgs; i++) {
sSrc = ((ImageTag) oCollectionList.elementAt(i)).extractImageLocn();
// Keep a reference to every related image name so that the same image is not included twice in the message
if (!oDocumentImages.containsKey(sSrc)) {
// Find last slash from image url
int iSlash = sSrc.lastIndexOf('/');
// Take image name
if (iSlash>=0) {
while (sSrc.charAt(iSlash)=='/') { if (++iSlash==sSrc.length()) break; }
sCid = sSrc.substring(iSlash);
}
else {
sCid = sSrc;
}
//String sUid = Gadgets.generateUUID();
//sCid = sUid.substring(0,12)+"$"+sUid.substring(12,20)+"$"+sUid.substring(20,28)+"@hipergate.org";
if (DebugFile.trace) DebugFile.writeln("HashMap.put("+sSrc+","+sCid+")");
oDocumentImages.put(sSrc, sCid);
} // fi (!oDocumentImages.containsKey(sSrc))
try {
Pattern oPattern = oCompiler.compile(sSrc, Perl5Compiler.SINGLELINE_MASK);
oSrcSubs.setSubstitution("cid:"+oDocumentImages.get(sSrc));
if (DebugFile.trace) DebugFile.writeln("Util.substitute([PatternMatcher],"+ sSrc + ",cid:"+oDocumentImages.get(sSrc)+",...)");
sHtmlBody = Util.substitute(oMatcher, oPattern, oSrcSubs, sHtmlBody);
} catch (MalformedPatternException neverthrown) { }