if (UtilValidate.isNotEmpty(bodyParts)) {
contentType = "multipart/mixed";
}
results.put("contentType", contentType);
Session session;
MimeMessage mail;
try {
Properties props = System.getProperties();
props.put(sendType, sendVia);
if (UtilValidate.isNotEmpty(port)) {
props.put("mail.smtp.port", port);
}
if (UtilValidate.isNotEmpty(socketFactoryPort)) {
props.put("mail.smtp.socketFactory.port", socketFactoryPort);
}
if (UtilValidate.isNotEmpty(socketFactoryClass)) {
props.put("mail.smtp.socketFactory.class", socketFactoryClass);
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
}
if (UtilValidate.isNotEmpty(socketFactoryFallback)) {
props.put("mail.smtp.socketFactory.fallback", socketFactoryFallback);
}
if (useSmtpAuth) {
props.put("mail.smtp.auth", "true");
}
if (sendPartial != null) {
props.put("mail.smtp.sendpartial", sendPartial ? "true" : "false");
}
if (isStartTLSEnabled) {
props.put("mail.smtp.starttls.enable", "true");
}
session = Session.getInstance(props);
boolean debug = UtilProperties.propertyValueEqualsIgnoreCase("general.properties", "mail.debug.on", "Y");
session.setDebug(debug);
mail = new MimeMessage(session);
if (messageId != null) {
mail.setHeader("In-Reply-To", messageId);
mail.setHeader("References", messageId);
}
mail.setFrom(new InternetAddress(sendFrom));
mail.setSubject(subject, "UTF-8");
mail.setHeader("X-Mailer", "Apache OFBiz, The Apache Open For Business Project");
mail.setSentDate(new Date());
mail.addRecipients(Message.RecipientType.TO, sendTo);
if (UtilValidate.isNotEmpty(sendCc)) {
mail.addRecipients(Message.RecipientType.CC, sendCc);
}
if (UtilValidate.isNotEmpty(sendBcc)) {
mail.addRecipients(Message.RecipientType.BCC, sendBcc);
}
if (UtilValidate.isNotEmpty(bodyParts)) {
// check for multipart message (with attachments)
// BodyParts contain a list of Maps items containing content(String) and type(String) of the attachement
MimeMultipart mp = new MimeMultipart();
Debug.logInfo(bodyParts.size() + " multiparts found",module);
for (Map<String, Object> bodyPart: bodyParts) {
Object bodyPartContent = bodyPart.get("content");
MimeBodyPart mbp = new MimeBodyPart();
if (bodyPartContent instanceof String) {
Debug.logInfo("part of type: " + bodyPart.get("type") + " and size: " + bodyPart.get("content").toString().length() , module);
mbp.setText((String) bodyPartContent, "UTF-8", ((String) bodyPart.get("type")).substring(5));
} else if (bodyPartContent instanceof byte[]) {
ByteArrayDataSource bads = new ByteArrayDataSource((byte[]) bodyPartContent, (String) bodyPart.get("type"));
Debug.logInfo("part of type: " + bodyPart.get("type") + " and size: " + ((byte[]) bodyPartContent).length , module);
mbp.setDataHandler(new DataHandler(bads));
} else if (bodyPartContent instanceof DataHandler) {
mbp.setDataHandler((DataHandler) bodyPartContent);
} else {
mbp.setDataHandler(new DataHandler(bodyPartContent, (String) bodyPart.get("type")));
}
String fileName = (String) bodyPart.get("filename");
if (fileName != null) {
mbp.setFileName(fileName);
}
mp.addBodyPart(mbp);
}
mail.setContent(mp);
mail.saveChanges();
} else {
// create the singelpart message
if (contentType.startsWith("text")) {
mail.setText(body, "UTF-8", contentType.substring(5));
} else {
mail.setContent(body, contentType);
}
mail.saveChanges();
}
} catch (MessagingException e) {
Debug.logError(e, "MessagingException when creating message to [" + sendTo + "] from [" + sendFrom + "] cc [" + sendCc + "] bcc [" + sendBcc + "] subject [" + subject + "]", module);
Debug.logError("Email message that could not be created to [" + sendTo + "] had context: " + context, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendMessagingException", UtilMisc.toMap("sendTo", sendTo, "sendFrom", sendFrom, "sendCc", sendCc, "sendBcc", sendBcc, "subject", subject), locale));
} catch (IOException e) {
Debug.logError(e, "IOExcepton when creating message to [" + sendTo + "] from [" + sendFrom + "] cc [" + sendCc + "] bcc [" + sendBcc + "] subject [" + subject + "]", module);
Debug.logError("Email message that could not be created to [" + sendTo + "] had context: " + context, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendIOException", UtilMisc.toMap("sendTo", sendTo, "sendFrom", sendFrom, "sendCc", sendCc, "sendBcc", sendBcc, "subject", subject), locale));
}
// check to see if sending mail is enabled
String mailEnabled = UtilProperties.getPropertyValue("general.properties", "mail.notifications.enabled", "N");
if (!"Y".equalsIgnoreCase(mailEnabled)) {
// no error; just return as if we already processed
Debug.logImportant("Mail notifications disabled in general.properties; mail with subject [" + subject + "] not sent to addressee [" + sendTo + "]", module);
Debug.logVerbose("What would have been sent, the addressee: " + sendTo + " subject: " + subject + " context: " + context, module);
results.put("messageWrapper", new MimeMessageWrapper(session, mail));
return results;
}
Transport trans = null;
try {
trans = session.getTransport("smtp");
if (!useSmtpAuth) {
trans.connect();
} else {
trans.connect(sendVia, authUser, authPass);
}