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) { }
} // next
}
catch (ParserException pe) {
if (DebugFile.trace) {
DebugFile.writeln("org.htmlparser.util.ParserException " + pe.getMessage());
}
}
// End replace image CIDs
// ************************************************************************
// ************************************************************************
// Add HTML related images
if (oDocumentImages.isEmpty()) {
// Set HTML part
MimeBodyPart oMsgHtml = new MimeBodyPart();
oMsgHtml.setDisposition("inline");
oMsgHtml.setText(sHtmlBody, sCharEnc, "html");
// oMsgHtml.setContent(sHtmlBody, "text/html; charset="+sCharEnc);
oTextHtmlAlt.addBodyPart(oMsgHtml);
} else {
// Set HTML text related part
MimeBodyPart oMsgHtmlText = new MimeBodyPart();
oMsgHtmlText.setDisposition("inline");
oMsgHtmlText.setText(sHtmlBody, sCharEnc, "html");
// oMsgHtmlText.setContent(sHtmlBody, "text/html; charset="+sCharEnc);
if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/related).addBodyPart(text/html)");
oHtmlRelated.addBodyPart(oMsgHtmlText);
// Set HTML text related inline images
Iterator oImgs = oDocumentImages.keySet().iterator();
while (oImgs.hasNext()) {
BodyPart oImgBodyPart = new MimeBodyPart();
sSrc = (String) oImgs.next();
sCid = (String) oDocumentImages.get(sSrc);
if (sSrc.startsWith("www."))
sSrc = "http://" + sSrc;
if (sSrc.startsWith("http://") || sSrc.startsWith("https://")) {
oImgBodyPart.setDataHandler(new DataHandler(new URL(Hosts.resolve(sSrc))));
}
else {
oImgBodyPart.setDataHandler(new DataHandler(new FileDataSource((sBasePath==null ? "" : sBasePath)+sSrc)));
}
oImgBodyPart.setDisposition("inline");
oImgBodyPart.setHeader("Content-ID", sCid);
oImgBodyPart.setFileName(sCid);
// Add image to multi-part
if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/related).addBodyPart("+sCid+")");
oHtmlRelated.addBodyPart(oImgBodyPart);
} // wend
// Set html text alternative part (html text + inline images)
MimeBodyPart oTextHtmlRelated = new MimeBodyPart();
oTextHtmlRelated.setContent(oHtmlRelated);
if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/alternative).addBodyPart(multipart/related)");
oTextHtmlAlt.addBodyPart(oTextHtmlRelated);
}
// ************************************************************************
// Create message to be sent and add main text body to it
if (aAttachmentsPath==null) {
oSentMessage.setContent(oTextHtmlAlt);
} else {
MimeBodyPart oMixedPart = new MimeBodyPart();
oMixedPart.setContent(oTextHtmlAlt);
oSentMsgParts.addBodyPart(oMixedPart);
}
} else { // (sContentType=="plain")
// *************************************************
// If this is a plain text message just add the text
if (aAttachmentsPath==null) {
oSentMessage.setText(sTextBody, sCharEnc);
} else {
oMsgPlainText.setDisposition("inline");
oMsgPlainText.setText(sTextBody, sCharEnc, "plain");
//oMsgPlainText.setContent(sTextBody, "text/plain; charset="+sCharEnc);
if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/mixed).addBodyPart(text/plain)");
oSentMsgParts.addBodyPart(oMsgPlainText);
}
}
// fi (sContentType=="html")
// ************************************************************************
// Add attachments to message to be sent
if (aAttachmentsPath!=null) {
final int nAttachments = aAttachmentsPath.length;
FileSystem oFS = new FileSystem();
for (int p=0; p<nAttachments; p++) {
String sFilePath = aAttachmentsPath[p];
if (sBasePath!=null) {
if (!sFilePath.startsWith(sBasePath))
sFilePath = sBasePath + sFilePath;
}
File oFile = new File(sFilePath);
MimeBodyPart oAttachment = new MimeBodyPart();
oAttachment.setDisposition("attachment");
oAttachment.setFileName(oFile.getName());
oAttachment.setHeader("Content-Transfer-Encoding", "base64");
ByteArrayDataSource oDataSrc;
try {
oDataSrc = new ByteArrayDataSource(oFS.readfilebin(sFilePath), "application/octet-stream");
} catch (com.enterprisedt.net.ftp.FTPException ftpe) {
throw new IOException(ftpe.getMessage());
}
oAttachment.setDataHandler(new DataHandler(oDataSrc));
oSentMsgParts.addBodyPart(oAttachment);
} // next
oSentMessage.setContent(oSentMsgParts);
} // fi (iDraftParts>0)