smtpUsername = AppUtil.processHashVariable(smtpUsername, wfAssignment, null, null);
smtpPassword = AppUtil.processHashVariable(smtpPassword, wfAssignment, null, null);
security = AppUtil.processHashVariable(security, wfAssignment, null, null);
// create the email message
final HtmlEmail email = new HtmlEmail();
email.setHostName(smtpHost);
if (smtpPort != null && smtpPort.length() != 0) {
email.setSmtpPort(Integer.parseInt(smtpPort));
}
if (smtpUsername != null && !smtpUsername.isEmpty()) {
if (smtpPassword != null) {
smtpPassword = SecurityUtil.decrypt(smtpPassword);
}
email.setAuthentication(smtpUsername, smtpPassword);
}
if(security!= null){
if(security.equalsIgnoreCase("SSL") ){
email.setSSL(true);
}else if(security.equalsIgnoreCase("TLS")){
email.setTLS(true);
}
}
if (cc != null && cc.length() != 0) {
Collection<String> ccs = AppUtil.getEmailList(null, cc, wfAssignment, appDef);
for (String address : ccs) {
email.addCc(address);
}
}
final String fromStr = WorkflowUtil.processVariable(from, formDataTable, wfAssignment);
email.setFrom(fromStr);
email.setSubject(emailSubject);
email.setCharset("UTF-8");
if ("true".equalsIgnoreCase(isHtml)) {
email.setHtmlMsg(emailMessage);
} else {
email.setMsg(emailMessage);
}
String emailToOutput = "";
if ((toParticipantId != null && toParticipantId.trim().length() != 0) || (toSpecific != null && toSpecific.trim().length() != 0)) {
Collection<String> tss = AppUtil.getEmailList(toParticipantId, toSpecific, wfAssignment, appDef);
for (String address : tss) {
email.addTo(address);
emailToOutput += address + ", ";
}
} else {
throw new PluginException("no email specified");
}
final String to = emailToOutput;
final String profile = DynamicDataSourceManager.getCurrentProfile();
//handle file attachment
String formDefId = (String) properties.get("formDefId");
Object[] fields = null;
if (properties.get("fields") instanceof Object[]){
fields = (Object[]) properties.get("fields");
}
if (formDefId != null && !formDefId.isEmpty() && fields != null && fields.length > 0) {
AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
FormData formData = new FormData();
String primaryKey = appService.getOriginProcessId(wfAssignment.getProcessId());
formData.setPrimaryKeyValue(primaryKey);
Form loadForm = appService.viewDataForm(appDef.getId(), appDef.getVersion().toString(), formDefId, null, null, null, formData, null, null);
for (Object o : fields) {
Map mapping = (HashMap) o;
String fieldId = mapping.get("field").toString();
try {
Element el = FormUtil.findElement(fieldId, loadForm, formData);
String value = FormUtil.getElementPropertyValue(el, formData);
if (value != null && !value.isEmpty()) {
File file = FileUtil.getFile(value, loadForm, primaryKey);
if (file != null) {
FileDataSource fds = new FileDataSource(file);
email.attach(fds, MimeUtility.encodeText(file.getName()), "");
}
}
} catch(Exception e){
LogUtil.info(EmailTool.class.getName(), "Attached file fail from field \"" + fieldId + "\" in form \"" + formDefId + "\"");
}
}
}
Object[] files = null;
if (properties.get("files") instanceof Object[]){
files = (Object[]) properties.get("files");
}
if (files != null && files.length > 0) {
for (Object o : files) {
Map mapping = (HashMap) o;
String path = mapping.get("path").toString();
String fileName = mapping.get("fileName").toString();
String type = mapping.get("type").toString();
try {
if ("system".equals(type)) {
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(path);
attachment.setName(MimeUtility.encodeText(fileName));
email.attach(attachment);
} else {
URL u = new URL(path);
email.attach(u, MimeUtility.encodeText(fileName), "");
}
} catch(Exception e){
LogUtil.info(EmailTool.class.getName(), "Attached file fail from path \"" + path + "\"");
e.printStackTrace();
}
}
}
Thread emailThread = new Thread(new Runnable() {
public void run() {
try {
HostManager.setCurrentProfile(profile);
LogUtil.info(EmailTool.class.getName(), "EmailTool: Sending email from=" + fromStr + ", to=" + to + "cc=" + cc + ", subject=" + email.getSubject());
email.send();
LogUtil.info(EmailTool.class.getName(), "EmailTool: Sending email completed for subject=" + email.getSubject());
} catch (EmailException ex) {
LogUtil.error(EmailTool.class.getName(), ex, "");
}
}
});