// ************************************************************************
// 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
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);
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();
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;
}
oDocumentImages.put(sSrc, sCid);
} // fi (!oDocumentImages.containsKey(sSrc))
try {
Pattern oPattern = oCompiler.compile(sSrc, Perl5Compiler.SINGLELINE_MASK);
oSrcSubs.setSubstitution("cid:"+oDocumentImages.get(sSrc));
sHtmlBody = Util.substitute(oMatcher, oPattern, oSrcSubs, sHtmlBody);
} catch (MalformedPatternException neverthrown) { }
} // next
}