Multipart oDraftParts = getParts();
final int iDraftParts = oDraftParts.getCount();
if (DebugFile.trace) DebugFile.writeln("Multipart.getCount() = " + String.valueOf(iDraftParts));
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
String sSrc, sCid, sText = "";
Parser oPrsr = Parser.createParser(sBody, getEncoding());
// String sCid, sSrc;
HtmlMimeBodyPart oHtmBdy = new HtmlMimeBodyPart(sBody, getEncoding());
try {
// ****************************
// Extract plain text from HTML
if (DebugFile.trace) DebugFile.writeln("new StringBean()");
StringBean oStrBn = new StringBean();
try {
oPrsr.visitAllNodesWith (oStrBn);
} catch (ParserException pe) {
if (DebugFile.trace) {
DebugFile.writeln("org.htmlparser.util.ParserException " + pe.getMessage());
}
throw new MessagingException(pe.getMessage(), pe);
}
sText = oStrBn.getStrings();
oStrBn = null;
// *******************************
// Set plain text alternative part
oMsgPlainText.setDisposition("inline");
oMsgPlainText.setText(sText,Charset.forName(sEncoding).name(),"plain");
if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/alternative).addBodyPart(text/plain)");
oTextHtmlAlt.addBodyPart(oMsgPlainText);
// *****************************************
// Iterate images from HTML and replace CIDs
if (bAttachInlineImages) {
sBody = oHtmBdy.addPreffixToImgSrc("cid:");
} // fi (bAttachInlineImages)
}
catch (ParserException pe) {
if (DebugFile.trace) {
DebugFile.writeln("org.htmlparser.util.ParserException " + pe.getMessage());
}
throw new MessagingException(pe.getMessage(), pe);
}
// End replace image CIDs
// ************************************************************************
// ************************************************************************
// Some defensive programming: ensure that all src="..." attributes point
// either to an absolute http:// URL or to a cid:
oHtmBdy.setHtml (sBody);
ArrayList<String> aLocalUrls = oHtmBdy.extractLocalUrls();
if (aLocalUrls.size()>0) {
if (DebugFile.trace) {
DebugFile.writeln("HTML body part contains local references to external resources");
for (String i : aLocalUrls) {
DebugFile.writeln(i);
}
DebugFile.write(sBody+"\n");
}
throw new MessagingException("HTML body part "+(sId==null ? "" : "of message"+sId)+" contains local references to external resources such as "+aLocalUrls.get(0));
} // aLocalUrls != {}
// ************************************************************************
// Add HTML related images
if (oHtmBdy.getImagesCids().isEmpty()) {
// Set HTML part
MimeBodyPart oMsgHtml = new MimeBodyPart();
oMsgHtml.setDisposition("inline");
// ****************************************************************************
// Replace <!--WEBBEACON SRC="http://..."--> tag by an <IMG SRC="http://..." />
try {
iWebBeaconStart = Gadgets.indexOfIgnoreCase(sBody,"<!--WEBBEACON", 0);
if (iWebBeaconStart>0) {
iSrcTagStart = sBody.indexOf('"', iWebBeaconStart+13);
iSrcTagEnd = sBody.indexOf('"', iSrcTagStart+1);
iWebBeaconEnd = sBody.indexOf("-->", iWebBeaconStart+13);
if (iWebBeaconEnd>0) {
sBody = sBody.substring(0,iWebBeaconStart)+"<IMG SRC=\""+sBody.substring(iSrcTagStart+1,iSrcTagEnd)+"\" WIDTH=\"1\" HEIGHT=\"1\" BORDER=\"0\" ALT=\"\" />"+sBody.substring(iWebBeaconEnd+3);
}
}
} catch (Exception malformedwebbeacon) {
if (DebugFile.trace) DebugFile.writeln("Malformed Web Beacon");
}
oMsgHtml.setText(sBody,Charset.forName(sEncoding).name(),"html");
oTextHtmlAlt.addBodyPart(oMsgHtml);
} else {
// Set HTML text related part
MimeBodyPart oMsgHtmlText = new MimeBodyPart();
oMsgHtmlText.setDisposition("inline");
// ****************************************************************************
// Replace <!--WEBBEACON SRC="http://..."--> tag by an <IMG SRC="http://..." />
try {
iWebBeaconStart = Gadgets.indexOfIgnoreCase(sBody,"<!--WEBBEACON", 0);
if (iWebBeaconStart>0) {
iSrcTagStart = sBody.indexOf('"', iWebBeaconStart+13);
iSrcTagEnd = sBody.indexOf('"', iSrcTagStart+1);
iWebBeaconEnd = sBody.indexOf("-->", iWebBeaconStart+13);
if (iWebBeaconEnd>0) {
sBody = sBody.substring(0,iWebBeaconStart)+"<IMG SRC=\""+sBody.substring(iSrcTagStart+1,iSrcTagEnd)+"\" WIDTH=\"1\" HEIGHT=\"1\" BORDER=\"0\" ALT=\"\" />"+sBody.substring(iWebBeaconEnd+3);
}
}
} catch (Exception malformedwebbeacon) {
if (DebugFile.trace) DebugFile.writeln("Malformed Web Beacon");
}
oMsgHtmlText.setText(sBody,Charset.forName(sEncoding).name(),"html");
if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/related).addBodyPart(text/html)");
oHtmlRelated.addBodyPart(oMsgHtmlText);
// Set HTML text related inline images
Iterator oImgs = oHtmBdy.getImagesCids().keySet().iterator();
while (oImgs.hasNext()) {
BodyPart oImgBodyPart = new MimeBodyPart();
sSrc = (String) oImgs.next();
sCid = (String) oHtmBdy.getImagesCids().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(sSrc)));
}
if (sSrc.endsWith(".png")) oImgBodyPart.setHeader("Content-Type", "image/png;name="+sCid);
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 (0==iDraftParts) {
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 (0==iDraftParts) {
oSentMessage.setText(sBody, Charset.forName(sEncoding).name(), "plain");
} else {
oMsgPlainText.setDisposition("inline");
oMsgPlainText.setText(sBody,Charset.forName(sEncoding).name(),"plain");
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 (iDraftParts>0) {
for (int p=0; p<iDraftParts; p++) {
DBMimePart oPart = (DBMimePart) oDraftParts.getBodyPart(p);
String sDisposition = oPart.getDisposition();
if (sDisposition==null)
sDisposition = "inline";
else if (sDisposition.equals("reference") || sDisposition.equals("pointer"))
sDisposition = "attachment";
int iSize = oPart.getSize();
if (iSize<=0) iSize = 4000;
InputStream oInStrm = oPart.getInputStream();
ByteArrayOutputStream oByStrm = new java.io.ByteArrayOutputStream(iSize);
new StreamPipe().between(oInStrm, oByStrm);
oInStrm.close();
if (DebugFile.trace) DebugFile.writeln("part " + String.valueOf(p) + " size is " + String.valueOf(oByStrm.size()));
ByteArrayDataSource oDataSrc = new ByteArrayDataSource(oByStrm.toByteArray(), oPart.getContentType());
MimeBodyPart oAttachment = new MimeBodyPart();
oAttachment.setDisposition(sDisposition);
if (sDisposition.equals("attachment"))
if (null==oPart.getDescription())
oAttachment.setFileName(oPart.getFileName());
else
oAttachment.setFileName(oPart.getDescription());
oAttachment.setHeader("Content-Transfer-Encoding", "base64");
oAttachment.setDataHandler(new DataHandler(oDataSrc));
oSentMsgParts.addBodyPart(oAttachment);
} // next
oSentMessage.setContent(oSentMsgParts);
} // fi (iDraftParts>0)