Search in sources :

Example 6 with EmailAccount

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

Example 7 with EmailAccount

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

the class MessageServiceImpl method sendByEmail.

@Override
@Transactional(rollbackOn = { Exception.class })
public Message sendByEmail(Message message, Boolean isTemporaryEmail) throws MessagingException, AxelorException {
    EmailAccount mailAccount = message.getMailAccount();
    if (mailAccount == null) {
        return message;
    }
    log.debug("Sending email...");
    MailAccountService mailAccountService = Beans.get(MailAccountService.class);
    com.axelor.mail.MailAccount account = new SmtpAccount(mailAccount.getHost(), mailAccount.getPort().toString(), mailAccount.getLogin(), mailAccountService.getDecryptPassword(mailAccount.getPassword()), mailAccountService.getSecurity(mailAccount));
    List<String> replytoRecipients = this.getEmailAddresses(message.getReplyToEmailAddressSet());
    List<String> toRecipients = this.getEmailAddresses(message.getToEmailAddressSet());
    List<String> ccRecipients = this.getEmailAddresses(message.getCcEmailAddressSet());
    List<String> bccRecipients = this.getEmailAddresses(message.getBccEmailAddressSet());
    if (toRecipients.isEmpty() && ccRecipients.isEmpty() && bccRecipients.isEmpty()) {
        throw new AxelorException(message, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MESSAGE_6));
    }
    MailSender sender = new MailSender(account);
    MailBuilder mailBuilder = sender.compose();
    mailBuilder.subject(message.getSubject());
    if (!Strings.isNullOrEmpty(mailAccount.getFromAddress())) {
        String fromAddress = mailAccount.getFromAddress();
        if (!Strings.isNullOrEmpty(mailAccount.getFromName())) {
            fromAddress = String.format("%s <%s>", mailAccount.getFromName(), mailAccount.getFromAddress());
        } else if (message.getFromEmailAddress() != null) {
            if (!Strings.isNullOrEmpty(message.getFromEmailAddress().getAddress())) {
                log.debug("Override from :::  {}", this.getFullEmailAddress(message.getFromEmailAddress()));
                mailBuilder.from(this.getFullEmailAddress(message.getFromEmailAddress()));
            } else {
                throw new AxelorException(message, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, IExceptionMessage.MESSAGE_5);
            }
        }
        mailBuilder.from(fromAddress);
    }
    if (replytoRecipients != null && !replytoRecipients.isEmpty()) {
        mailBuilder.replyTo(Joiner.on(",").join(replytoRecipients));
    }
    if (!toRecipients.isEmpty()) {
        mailBuilder.to(Joiner.on(",").join(toRecipients));
    }
    if (ccRecipients != null && !ccRecipients.isEmpty()) {
        mailBuilder.cc(Joiner.on(",").join(ccRecipients));
    }
    if (bccRecipients != null && !bccRecipients.isEmpty()) {
        mailBuilder.bcc(Joiner.on(",").join(bccRecipients));
    }
    if (!Strings.isNullOrEmpty(message.getContent())) {
        mailBuilder.html(message.getContent());
    }
    if (!isTemporaryEmail) {
        for (MetaAttachment metaAttachment : getMetaAttachments(message)) {
            MetaFile metaFile = metaAttachment.getMetaFile();
            mailBuilder.attach(metaFile.getFileName(), MetaFiles.getPath(metaFile).toString());
        }
        getEntityManager().flush();
        getEntityManager().lock(message, LockModeType.PESSIMISTIC_WRITE);
        // send email using a separate process to avoid thread blocking
        sendMailQueueService.submitMailJob(mailBuilder, message);
    } else {
        // No separate thread or JPA persistence lock required
        try {
            mailBuilder.send();
        } catch (IOException e) {
            log.debug("Exception when sending email", e);
            TraceBackService.trace(e);
        }
        log.debug("Email sent.");
    }
    return message;
}
Also used : AxelorException(com.axelor.exception.AxelorException) EmailAccount(com.axelor.apps.message.db.EmailAccount) SmtpAccount(com.axelor.mail.SmtpAccount) MailSender(com.axelor.mail.MailSender) IOException(java.io.IOException) MailBuilder(com.axelor.mail.MailBuilder) MetaFile(com.axelor.meta.db.MetaFile) MetaAttachment(com.axelor.meta.db.MetaAttachment) Transactional(com.google.inject.persist.Transactional)

Example 8 with EmailAccount

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

the class MailAccountController method validateSmtpAccount.

public void validateSmtpAccount(ActionRequest request, ActionResponse response) {
    EmailAccount account = request.getContext().asType(EmailAccount.class);
    try {
        Beans.get(MailAccountService.class).checkMailAccountConfiguration(account);
        response.setValue("isValid", Boolean.TRUE);
        response.setValue("change", Boolean.FALSE);
        response.setValue("newPassword", null);
        response.setFlash(I18n.get(IExceptionMessage.MAIL_ACCOUNT_3));
    } catch (Exception e) {
        TraceBackService.trace(response, e);
        response.setValue("isValid", Boolean.FALSE);
    }
}
Also used : EmailAccount(com.axelor.apps.message.db.EmailAccount) MailAccountService(com.axelor.apps.message.service.MailAccountService) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) AxelorException(com.axelor.exception.AxelorException)

Example 9 with EmailAccount

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

the class FetchEmailJob method execute.

@Override
public void execute(JobExecutionContext context) {
    List<EmailAccount> mailAccounts = mailAccountRepo.all().filter("self.isValid = true and self.serverTypeSelect > 1").fetch();
    log.debug("Total email fetching accounts : {}", mailAccounts.size());
    for (EmailAccount account : mailAccounts) {
        try {
            Integer total = mailAccountService.fetchEmails(account, true);
            log.debug("Email fetched for account: {}, total: {} ", account.getName(), total);
        } catch (MessagingException | IOException e) {
            TraceBackService.trace(e);
        }
    }
}
Also used : EmailAccount(com.axelor.apps.message.db.EmailAccount) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

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