Search in sources :

Example 1 with MailMessageRepository

use of com.axelor.mail.db.repo.MailMessageRepository 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;
            }
        });
    }
}
Also used : Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) EmailAccount(com.axelor.apps.message.db.EmailAccount) User(com.axelor.auth.db.User) MessagingException(javax.mail.MessagingException) MailSender(com.axelor.mail.MailSender) IOException(java.io.IOException) MailBuilder(com.axelor.mail.MailBuilder) MessagingException(javax.mail.MessagingException) MailException(com.axelor.mail.MailException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) MailMessageRepository(com.axelor.mail.db.repo.MailMessageRepository) MailService(com.axelor.mail.service.MailService) MimeMessage(javax.mail.internet.MimeMessage) Model(com.axelor.db.Model) MailException(com.axelor.mail.MailException) Partner(com.axelor.apps.base.db.Partner) File(java.io.File) MetaAttachment(com.axelor.meta.db.MetaAttachment)

Example 2 with MailMessageRepository

use of com.axelor.mail.db.repo.MailMessageRepository in project axelor-open-suite by axelor.

the class MailServiceBaseImpl method template.

@Override
protected String template(MailMessage message, Model entity) throws IOException {
    if (messageTemplate == null) {
        return super.template(message, entity);
    }
    final String text = message.getBody().trim();
    templatesContext.put("username", userName);
    if (MESSAGE_TYPE_NOTIFICATION.equals(message.getType())) {
        final MailMessageRepository messages = Beans.get(MailMessageRepository.class);
        final Map<String, Object> details = messages.details(message);
        final String jsonBody = details.containsKey("body") ? (String) details.get("body") : text;
        final ObjectMapper mapper = Beans.get(ObjectMapper.class);
        Map<String, Object> data = mapper.readValue(jsonBody, new TypeReference<Map<String, Object>>() {
        });
        templatesContext.putAll(data);
    } else {
        templatesContext.put("comment", text);
    }
    return templates.fromText(messageTemplate.getContent()).make(templatesContext).render();
}
Also used : MailMessageRepository(com.axelor.mail.db.repo.MailMessageRepository) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with MailMessageRepository

use of com.axelor.mail.db.repo.MailMessageRepository 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;
        }
    });
}
Also used : Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) EmailAccount(com.axelor.apps.message.db.EmailAccount) MessagingException(javax.mail.MessagingException) MailSender(com.axelor.mail.MailSender) IOException(java.io.IOException) MailBuilder(com.axelor.mail.MailBuilder) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) IOException(java.io.IOException) MailException(com.axelor.mail.MailException) MailMessageRepository(com.axelor.mail.db.repo.MailMessageRepository) MimeMessage(javax.mail.internet.MimeMessage) Model(com.axelor.db.Model) MailException(com.axelor.mail.MailException) File(java.io.File) MetaAttachment(com.axelor.meta.db.MetaAttachment)

Aggregations

MailMessageRepository (com.axelor.mail.db.repo.MailMessageRepository)3 EmailAccount (com.axelor.apps.message.db.EmailAccount)2 Model (com.axelor.db.Model)2 MailBuilder (com.axelor.mail.MailBuilder)2 MailException (com.axelor.mail.MailException)2 MailSender (com.axelor.mail.MailSender)2 MetaAttachment (com.axelor.meta.db.MetaAttachment)2 File (java.io.File)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 LinkedHashSet (java.util.LinkedHashSet)2 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 Partner (com.axelor.apps.base.db.Partner)1 User (com.axelor.auth.db.User)1 MailService (com.axelor.mail.service.MailService)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1