use of com.axelor.mail.MailException in project axelor-open-suite by axelor.
the class MailServiceBaseImpl method send.
@Override
public void send(final MailMessage message) throws MailException {
if (!appBaseService.isApp("base") || !appBaseService.getAppBase().getActivateSendingEmail()) {
return;
}
final EmailAccount emailAccount = mailAccountService.getDefaultSender();
if (emailAccount == null) {
super.send(message);
return;
}
Preconditions.checkNotNull(message, "mail message can't be null");
final Model related = findEntity(message);
final MailSender sender = getMailSender(emailAccount);
final Set<String> recipients = recipients(message, related);
if (recipients.isEmpty()) {
return;
}
final MailMessageRepository messages = Beans.get(MailMessageRepository.class);
for (String recipient : recipients) {
MailBuilder builder = sender.compose().subject(getSubject(message, related));
this.setRecipients(builder, recipient, related);
Model obj = Beans.get(MailService.class).resolve(recipient);
userName = null;
if (obj != null) {
Class<Model> klass = EntityHelper.getEntityClass(obj);
if (klass.equals(User.class)) {
User user = (User) obj;
userName = user.getName();
} else if (klass.equals(Partner.class)) {
Partner partner = (Partner) obj;
userName = partner.getSimpleFullName();
}
}
for (MetaAttachment attachment : messages.findAttachments(message)) {
final Path filePath = MetaFiles.getPath(attachment.getMetaFile());
final File file = filePath.toFile();
builder.attach(file.getName(), file.toString());
}
MimeMessage email;
try {
builder.html(template(message, related));
email = builder.build(message.getMessageId());
final Set<String> references = new LinkedHashSet<>();
if (message.getParent() != null) {
references.add(message.getParent().getMessageId());
}
if (message.getRoot() != null) {
references.add(message.getRoot().getMessageId());
}
if (!references.isEmpty()) {
email.setHeader("References", Joiner.on(" ").skipNulls().join(references));
}
} catch (MessagingException | IOException e) {
throw new MailException(e);
}
// send email using a separate process to void thread blocking
executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
send(sender, email);
return true;
}
});
}
}
use of com.axelor.mail.MailException in project axelor-open-suite by axelor.
the class MailServiceMessageImpl method fetch.
@Override
public void fetch() throws MailException {
final EmailAccount emailAccount = mailAccountService.getDefaultReader();
if (emailAccount == null) {
super.fetch();
} else {
final MailReader reader = getMailReader(emailAccount);
final AuditableRunner runner = Beans.get(AuditableRunner.class);
runner.run(() -> {
try {
fetch(reader);
} catch (Exception e) {
log.error("Unable to fetch messages", e);
}
});
}
}
use of com.axelor.mail.MailException in project axelor-open-suite by axelor.
the class MailServiceMessageImpl method send.
@Override
public void send(final MailMessage message) throws MailException {
final EmailAccount emailAccount = mailAccountService.getDefaultSender();
if (emailAccount == null) {
super.send(message);
return;
}
Preconditions.checkNotNull(message, "mail message can't be null");
final Model related = findEntity(message);
final MailSender sender = getMailSender(emailAccount);
final Set<String> recipients = recipients(message, related);
if (recipients.isEmpty()) {
return;
}
final MailMessageRepository messages = Beans.get(MailMessageRepository.class);
final MailBuilder builder = sender.compose().subject(getSubject(message, related));
for (String recipient : recipients) {
builder.to(recipient);
}
for (MetaAttachment attachment : messages.findAttachments(message)) {
final Path filePath = MetaFiles.getPath(attachment.getMetaFile());
final File file = filePath.toFile();
builder.attach(file.getName(), file.toString());
}
final MimeMessage email;
try {
builder.html(template(message, related));
email = builder.build(message.getMessageId());
final Set<String> references = new LinkedHashSet<>();
if (message.getParent() != null) {
references.add(message.getParent().getMessageId());
}
if (message.getRoot() != null) {
references.add(message.getRoot().getMessageId());
}
if (!references.isEmpty()) {
email.setHeader("References", Joiner.on(" ").skipNulls().join(references));
}
} catch (MessagingException | IOException e) {
throw new MailException(e);
}
// send email using a separate process to void thread blocking
executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
send(sender, email);
return true;
}
});
}
Aggregations