* Container holding the message to send
*/
public void sendEmailSync(EmailData emailData, String msgIdFileName) throws XmlBlasterException {
if (emailData == null) throw new IllegalArgumentException("SmtpClient.sendEmail(): Missing argument emailData");
try {
MimeMessage message = new MimeMessage(getSession());
message.setFrom(emailData.getFromAddress());
message.setRecipients(Message.RecipientType.TO, emailData.getToAddresses());
if (emailData.getCc().length > 0)
message.setRecipients(Message.RecipientType.CC, emailData.getCc());
if (emailData.getBcc().length > 0)
message.setRecipients(Message.RecipientType.BCC, emailData.getBcc());
message.setSubject(emailData.getSubject(), Constants.UTF8_ENCODING);
AttachmentHolder[] holder = emailData.getAttachments();
if (holder.length == 0 && emailData.getContent() != null && emailData.getContent().length() > 0) {
message.setText(emailData.getContent(), Constants.UTF8_ENCODING);
}
else {
// create the Multipart and add its parts to it
Multipart multi = new MimeMultipart();
if (emailData.getContent() != null && emailData.getContent().length() > 0) {
MimeBodyPart mbp = new MimeBodyPart();
mbp.setFileName("content.txt");
mbp.setText(emailData.getContent(), Constants.UTF8_ENCODING);
mbp.setDisposition(MimeBodyPart.INLINE);
multi.addBodyPart(mbp);
}
for (int i=0; i<holder.length; i++) {
MimeBodyPart mbp = new MimeBodyPart();
// 'AA xmlBlasterMessage.xbf' will be automatically quoted to '"AA xmlBlasterMessage.xbf"' by javamail implementation
// 'xx.xbf' names will be send unquoted
mbp.setFileName(holder[i].getFileName());
byte[] content = holder[i].getContent();
if (this.messageIdForceBase64 && emailData.isMessageIdAttachment(holder[i], msgIdFileName)
|| this.contentForceBase64 && emailData.isMsgUnitAttachment(holder[i])) {
//We don't need to do it, javamail does it for us
//content = Base64.encode(holder[i].getContent()).getBytes(Constants.UTF8_ENCODING);
//Buggy: is not accepted by javamail: (Why? and How?)
mbp.setHeader(Constants.EMAIL_TRANSFER_ENCODING, Constants.ENCODING_BASE64); // "Content-Transfer-Encoding", "base64");
}
else {
// http://www.ietf.org/rfc/rfc2045.txt
// The Quoted-Printable encoding REQUIRES that encoded lines be no more than 76
// characters long. (78 with CRLF), the line uses a trailing '=' as a soft line brake
mbp.setHeader(Constants.EMAIL_TRANSFER_ENCODING, Constants.ENCODING_QUOTED_PRINTABLE); // "Content-Transfer-Encoding", "quoted-printable");
if (holder[i].hasExtensionFromList(this.inlineExtension))
mbp.setDisposition(MimeBodyPart.INLINE);
}
// Encoding violates RFC 2231 but is very common to do so for non-ASCII character sets:
//mbp.setFileName(MimeUtility.encodeText(holder[i].getFileName()));
if (holder[i].getContentType().startsWith("text/")) {
//String tmp = MimeUtility.encodeText(new String(content, Constants.UTF8_ENCODING), Constants.UTF8_ENCODING, Constants.ENCODING_QUOTED_PRINTABLE);
//mbp.setText(tmp, Constants.UTF8_ENCODING);
String contentStr = new String(content, Constants.UTF8_ENCODING);
if (this.breakLongMessageIdLine && emailData.isMessageIdAttachment(holder[i], msgIdFileName)) {
// <messageId><sessionId>unknown</sessionId><requestId>1140597982821000000</requestId><methodName>update</methodName><expires>2006-02-23T08:46:22.821Z</expires></messageId>
contentStr = ReplaceVariable.replaceAll(contentStr, "<requestId>", "\r\n<requestId>");
contentStr = ReplaceVariable.replaceAll(contentStr, "<methodName>", "\r\n<methodName>");
contentStr = ReplaceVariable.replaceAll(contentStr, "<expires>", "\r\n<expires>");
}
mbp.setText(contentStr, Constants.UTF8_ENCODING);
}
else {
// "application/xmlBlaster-xbformat"
DataSource ds = new ByteArrayDataSource(
content,
holder[i].getContentType());
mbp.setDataHandler(new DataHandler(ds));
}
multi.addBodyPart(mbp);
}
// add the Multipart to the message
message.setContent(multi);
} // else multipart
// set the Date: header
Date date = new Date();
message.setSentDate(date);
// Set the xmlBlaster specific expiry header field
// Expires: Thu, 15 Dec 2005 21:45:01 +0100 (CET)
// This could be evaluated by MTA plugins
if (this.addExpiresHeader && emailData.getExpiryTime() != null) {
//message.setHeader(EmailData.EXPIRES_HEADER, emailData.getExpiryTime().toString());
message.setHeader(EmailData.EXPIRES_HEADER_RFC2156, MailUtil.dateTime(emailData.getExpiryTime()));
}
if (log.isLoggable(Level.FINE))
log.fine("Trying send email from=" + emailData.getFrom() + " to="
+ emailData.getRecipientsList());