Search in sources :

Example 1 with EmailAccount

use of com.axelor.apps.message.db.EmailAccount 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 EmailAccount

use of com.axelor.apps.message.db.EmailAccount in project axelor-open-suite by axelor.

the class TemplateMessageServiceImpl method generateMessage.

protected Message generateMessage(String model, Long objectId, Template template, Templates templates, Map<String, Object> templatesContext, Boolean isForTemporaryEmail) {
    String content = "";
    String subject = "";
    String replyToRecipients = "";
    String toRecipients = "";
    String ccRecipients = "";
    String bccRecipients = "";
    String addressBlock = "";
    int mediaTypeSelect;
    String signature = "";
    if (!Strings.isNullOrEmpty(template.getContent())) {
        content = templates.fromText(template.getContent()).make(templatesContext).render();
    }
    if (!Strings.isNullOrEmpty(template.getAddressBlock())) {
        addressBlock = templates.fromText(template.getAddressBlock()).make(templatesContext).render();
    }
    if (!Strings.isNullOrEmpty(template.getSubject())) {
        subject = templates.fromText(template.getSubject()).make(templatesContext).render();
        log.debug("Subject ::: {}", subject);
    }
    if (!Strings.isNullOrEmpty(template.getReplyToRecipients())) {
        replyToRecipients = templates.fromText(template.getReplyToRecipients()).make(templatesContext).render();
        log.debug("Reply to ::: {}", replyToRecipients);
    }
    if (template.getToRecipients() != null) {
        toRecipients = templates.fromText(template.getToRecipients()).make(templatesContext).render();
        log.debug("To ::: {}", toRecipients);
    }
    if (template.getCcRecipients() != null) {
        ccRecipients = templates.fromText(template.getCcRecipients()).make(templatesContext).render();
        log.debug("CC ::: {}", ccRecipients);
    }
    if (template.getBccRecipients() != null) {
        bccRecipients = templates.fromText(template.getBccRecipients()).make(templatesContext).render();
        log.debug("BCC ::: {}", bccRecipients);
    }
    mediaTypeSelect = this.getMediaTypeSelect(template);
    log.debug("Media ::: {}", mediaTypeSelect);
    if (template.getSignature() != null) {
        signature = templates.fromText(template.getSignature()).make(templatesContext).render();
        log.debug("Signature ::: {}", signature);
    }
    EmailAccount mailAccount = getMailAccount();
    EmailAddress fromAddress = null;
    if (mailAccount == null) {
        TraceBackService.trace(new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MAIL_ACCOUNT_6)));
    } else {
        fromAddress = getEmailAddress(mailAccount.getFromAddress());
    }
    Message message = messageService.createMessage(model, Math.toIntExact(objectId), subject, content, fromAddress, getEmailAddresses(replyToRecipients), getEmailAddresses(toRecipients), getEmailAddresses(ccRecipients), getEmailAddresses(bccRecipients), null, addressBlock, mediaTypeSelect, mailAccount, signature, isForTemporaryEmail);
    return message;
}
Also used : AxelorException(com.axelor.exception.AxelorException) EmailAccount(com.axelor.apps.message.db.EmailAccount) Message(com.axelor.apps.message.db.Message) IExceptionMessage(com.axelor.apps.message.exception.IExceptionMessage) EmailAddress(com.axelor.apps.message.db.EmailAddress)

Example 3 with EmailAccount

use of com.axelor.apps.message.db.EmailAccount in project axelor-open-suite by axelor.

the class MailAccountController method fetchEmails.

public void fetchEmails(ActionRequest request, ActionResponse response) throws MessagingException, IOException {
    EmailAccount account = request.getContext().asType(EmailAccount.class);
    account = Beans.get(EmailAccountRepository.class).find(account.getId());
    int totalFetched = Beans.get(MailAccountService.class).fetchEmails(account, true);
    response.setFlash(I18n.get(String.format("Total email fetched: %s", totalFetched)));
}
Also used : EmailAccount(com.axelor.apps.message.db.EmailAccount) MailAccountService(com.axelor.apps.message.service.MailAccountService)

Example 4 with EmailAccount

use of com.axelor.apps.message.db.EmailAccount 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);
            }
        });
    }
}
Also used : MailReader(com.axelor.mail.MailReader) EmailAccount(com.axelor.apps.message.db.EmailAccount) AuditableRunner(com.axelor.auth.AuditableRunner) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) IOException(java.io.IOException) MailException(com.axelor.mail.MailException)

Example 5 with EmailAccount

use of com.axelor.apps.message.db.EmailAccount in project axelor-open-suite by axelor.

the class DMSFileServiceImpl method sendEmail.

@Override
public void sendEmail(DMSFile dmsFile) throws AxelorException, MessagingException {
    final EmailAccount emailAccount = mailAccountService.getDefaultSender();
    User currentUser = AuthUtils.getUser();
    User lockedBy = dmsFile.getLockedBy();
    EmailAddress emailAddress = this.getEmailAddress(lockedBy);
    String content = null;
    String language = currentUser.getLanguage();
    if (language != null && language.equals("fr")) {
        content = String.format(CONTENT_EN, lockedBy.getFullName(), currentUser.getFullName());
    } else {
        content = String.format(CONTENT_EN, lockedBy.getFullName(), currentUser.getFullName());
    }
    List<EmailAddress> toEmailAddressList = new ArrayList<>();
    toEmailAddressList.add(emailAddress);
    Message message = messageService.createMessage(null, 0, I18n.get(SUBJECT), content, null, null, toEmailAddressList, null, null, null, null, MessageRepository.MEDIA_TYPE_EMAIL, emailAccount, null);
    messageService.sendByEmail(message);
}
Also used : EmailAccount(com.axelor.apps.message.db.EmailAccount) User(com.axelor.auth.db.User) IExceptionMessage(com.axelor.apps.projectdms.exception.IExceptionMessage) Message(com.axelor.apps.message.db.Message) ArrayList(java.util.ArrayList) EmailAddress(com.axelor.apps.message.db.EmailAddress)

Aggregations

EmailAccount (com.axelor.apps.message.db.EmailAccount)9 IOException (java.io.IOException)6 MessagingException (javax.mail.MessagingException)5 AxelorException (com.axelor.exception.AxelorException)3 MailBuilder (com.axelor.mail.MailBuilder)3 MailException (com.axelor.mail.MailException)3 MailSender (com.axelor.mail.MailSender)3 MetaAttachment (com.axelor.meta.db.MetaAttachment)3 EmailAddress (com.axelor.apps.message.db.EmailAddress)2 Message (com.axelor.apps.message.db.Message)2 MailAccountService (com.axelor.apps.message.service.MailAccountService)2 User (com.axelor.auth.db.User)2 Model (com.axelor.db.Model)2 MailMessageRepository (com.axelor.mail.db.repo.MailMessageRepository)2 File (java.io.File)2 Path (java.nio.file.Path)2 LinkedHashSet (java.util.LinkedHashSet)2 AddressException (javax.mail.internet.AddressException)2 MimeMessage (javax.mail.internet.MimeMessage)2 Partner (com.axelor.apps.base.db.Partner)1