}
public TaskExec executeSingle(final NotificationTask task) {
init();
TaskExec execution = new TaskExec();
execution.setTask(task);
execution.setStartDate(new Date());
boolean retryPossible = true;
if (StringUtils.isBlank(smtpHost) || StringUtils.isBlank(task.getSender())
|| StringUtils.isBlank(task.getSubject()) || task.getRecipients().isEmpty()
|| StringUtils.isBlank(task.getHtmlBody()) || StringUtils.isBlank(task.getTextBody())) {
String message = "Could not fetch all required information for sending e-mails:\n"
+ smtpHost + ":" + smtpPort + "\n"
+ task.getRecipients() + "\n"
+ task.getSender() + "\n"
+ task.getSubject() + "\n"
+ task.getHtmlBody() + "\n"
+ task.getTextBody();
LOG.error(message);
execution.setStatus(Status.NOT_SENT.name());
retryPossible = false;
if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) {
execution.setMessage(message);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("About to send e-mails:\n"
+ smtpHost + ":" + smtpPort + "\n"
+ task.getRecipients() + "\n"
+ task.getSender() + "\n"
+ task.getSubject() + "\n"
+ task.getHtmlBody() + "\n"
+ task.getTextBody() + "\n");
}
for (String to : task.getRecipients()) {
try {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(smtpHost);
sender.setPort(smtpPort);
sender.setDefaultEncoding(SyncopeConstants.DEFAULT_ENCODING);
if (StringUtils.isNotBlank(smtpUsername)) {
sender.setUsername(smtpUsername);
}
if (StringUtils.isNotBlank(smtpPassword)) {
sender.setPassword(smtpPassword);
}
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setFrom(task.getSender());
helper.setSubject(task.getSubject());
helper.setText(task.getTextBody(), task.getHtmlBody());
sender.send(message);
execution.setStatus(Status.SENT.name());
StringBuilder report = new StringBuilder();
switch (task.getTraceLevel()) {
case ALL:
report.append("FROM: ").append(task.getSender()).append('\n').
append("TO: ").append(to).append('\n').
append("SUBJECT: ").append(task.getSubject()).append('\n').append('\n').
append(task.getTextBody()).append('\n').append('\n').
append(task.getHtmlBody()).append('\n');
break;
case SUMMARY:
report.append("E-mail sent to ").append(to).append('\n');
break;
case FAILURES:
case NONE:
default:
}
if (report.length() > 0) {
execution.setMessage(report.toString());
}
auditManager.audit(Category.notification, NotificationSubCategory.send, Result.success,
"Successfully sent notification to " + to);
} catch (Exception e) {
LOG.error("Could not send e-mail", e);
execution.setStatus(Status.NOT_SENT.name());
StringWriter exceptionWriter = new StringWriter();
exceptionWriter.write(e.getMessage() + "\n\n");
e.printStackTrace(new PrintWriter(exceptionWriter));
if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) {
execution.setMessage(exceptionWriter.toString());
}
auditManager.audit(Category.notification, NotificationSubCategory.send, Result.failure,
"Could not send notification to " + to, e);
}
execution.setEndDate(new Date());
}
}
if (hasToBeRegistered(execution)) {
execution = notificationManager.storeExec(execution);
if (retryPossible && (Status.valueOf(execution.getStatus()) == Status.NOT_SENT)) {
handleRetries(execution);
}
} else {
notificationManager.setTaskExecuted(execution.getTask().getId(), true);
}
return execution;
}