StringWriter alterTextWriter = new StringWriter();
Velocity.evaluate(context, alterTextWriter, "message body", alterText);
alterTextWriter.flush();
alterTextWriter.close();
alterText = alterTextWriter.toString();
ContentType ct = new ContentType(altPart.getContentType());
String alterTextCharset = ct.getParameter("charset");
//get main part (html or related)
Object mainPart = ((Multipart)body).getBodyPart(1).getContent();
if(mainPart instanceof Multipart)
{
/*
* -mixed
* \-alternative (body)
* \-text
* |-related (mainPart)
* \-html
* |-attachment
* |-attachments
*/
//replace html
String html = (String) ((Multipart)mainPart).getBodyPart(0).getContent();
StringWriter mailBody = new StringWriter();
Velocity.evaluate(context, mailBody, "message body", html);
mailBody.flush();
mailBody.close();
html = mailBody.toString();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, ((Multipart)mainPart).getBodyPart(0).getContentType());
htmlPart.setHeader("Content-Type", ((Multipart)mainPart).getBodyPart(0).getContentType());
htmlPart.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
((Multipart)mainPart).removeBodyPart(0);
((Multipart)mainPart).addBodyPart(htmlPart, 0);
//replace related
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(((Multipart)mainPart));
((Multipart)body).removeBodyPart(1);
((Multipart)body).addBodyPart(bodyPart);
//replace body
((Multipart)content).removeBodyPart(0);
bodyPart = new MimeBodyPart();
bodyPart.setContent(((Multipart)body));
((Multipart)content).addBodyPart(bodyPart, 0);
}
else
{
/*
* -mixed
* \-alternative (body)
* \-text
* |-html
* |-attachments
*/
//replace html
String html = (String) ((Multipart)body).getBodyPart(1).getContent();
StringWriter mailBody = new StringWriter();
Velocity.evaluate(context, mailBody, "message body", html);
mailBody.flush();
mailBody.close();
html = mailBody.toString();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, ((Multipart)body).getBodyPart(1).getContentType());
htmlPart.setHeader("Content-Type", ((Multipart)body).getBodyPart(1).getContentType());
htmlPart.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
//replace html
((Multipart)body).removeBodyPart(1);
((Multipart)body).addBodyPart(htmlPart, 1);
//replace body
((Multipart)content).removeBodyPart(0);
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(((Multipart)body));
((Multipart)content).addBodyPart(bodyPart, 0);
}
if(alterTextCharset != null)
setAlternativeText(alterText, alterTextCharset);
else
setAlternativeText(alterText, charset);
}
else if(body instanceof Multipart
&& ((Multipart)body).getContentType().startsWith("multipart/related"))
{
/*
* -mixed
* \-related (body)
* \-html
* |-attachment
* |-attachments
*/
//replace html
String html = (String) ((Multipart)body).getBodyPart(0).getContent();;
StringWriter mailBody = new StringWriter();
Velocity.evaluate(context, mailBody, "message body", html);
mailBody.flush();
mailBody.close();
html = mailBody.toString();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, ((Multipart)body).getBodyPart(0).getContentType());
htmlPart.setHeader("Content-Type", ((Multipart)body).getBodyPart(0).getContentType());
htmlPart.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
((Multipart)body).removeBodyPart(0);
((Multipart)body).addBodyPart(htmlPart, 0);
//replace body
((Multipart)content).removeBodyPart(0);
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(((Multipart)body));
((Multipart)content).addBodyPart(bodyPart, 0);
}
else
{
/*
* -mixed
* \-html (body)
* |-attachments
*/
//replace html
String html = (String) ((Multipart)content).getBodyPart(0).getContent();
StringWriter mailBody = new StringWriter();
Velocity.evaluate(context, mailBody, "message body", html);
mailBody.flush();
mailBody.close();
html = mailBody.toString();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, ((Multipart)content).getBodyPart(0).getContentType());
htmlPart.setHeader("Content-Type", ((Multipart)content).getBodyPart(0).getContentType());
htmlPart.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
((Multipart)content).removeBodyPart(0);
((Multipart)content).addBodyPart(htmlPart, 0);
}
}
else
{
if(content instanceof Multipart
&& ((Multipart)content).getContentType().startsWith("multipart/alternative"))
{
//get alternative text
BodyPart altPart = ((Multipart)content).getBodyPart(0);
String alterText = (String) altPart.getContent();
StringWriter alterTextWriter = new StringWriter();
Velocity.evaluate(context, alterTextWriter, "message body", alterText);
alterTextWriter.flush();
alterTextWriter.close();
alterText = alterTextWriter.toString();
ContentType ct = new ContentType(altPart.getContentType());
String alterTextCharset = ct.getParameter("charset");
//get and replace html part of message
Object body = ((Multipart)content).getBodyPart(1).getContent();
if(body instanceof Multipart)
{