Search in sources :

Example 1 with SendingMessage

use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.

the class Emailer method returnToQueue.

protected void returnToQueue(SendingMessage sendingMessage) {
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        SendingMessage msg = em.merge(sendingMessage);
        msg.setAttemptsMade(msg.getAttemptsMade() + 1);
        msg.setStatus(SendingStatus.QUEUE);
        tx.commit();
    } catch (Exception e) {
        log.error("Error returning message to '{}' to the queue", sendingMessage.getAddress(), e);
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) AddressException(javax.mail.internet.AddressException) MailSendException(org.springframework.mail.MailSendException)

Example 2 with SendingMessage

use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.

the class Emailer method markAsSent.

protected void markAsSent(SendingMessage sendingMessage) {
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        SendingMessage msg = em.merge(sendingMessage);
        msg.setStatus(SendingStatus.SENT);
        msg.setAttemptsMade(msg.getAttemptsMade() + 1);
        msg.setDateSent(timeSource.currentTimestamp());
        tx.commit();
    } catch (Exception e) {
        log.error("Error marking message to '{}' as sent", sendingMessage.getAddress(), e);
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) AddressException(javax.mail.internet.AddressException) MailSendException(org.springframework.mail.MailSendException)

Example 3 with SendingMessage

use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.

the class Emailer method loadContentText.

@Override
public String loadContentText(SendingMessage sendingMessage) {
    SendingMessage msg;
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        msg = em.reload(sendingMessage, "sendingMessage.loadContentText");
        tx.commit();
    }
    Objects.requireNonNull(msg, "Sending message not found: " + sendingMessage.getId());
    if (msg.getContentTextFile() != null) {
        byte[] bodyContent;
        try {
            bodyContent = fileStorage.loadFile(msg.getContentTextFile());
        } catch (FileStorageException e) {
            throw new RuntimeException(e);
        }
        // noinspection UnnecessaryLocalVariable
        String res = bodyTextFromByteArray(bodyContent);
        return res;
    } else {
        return msg.getContentText();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Example 4 with SendingMessage

use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.

the class Emailer method persistAndSendEmail.

protected void persistAndSendEmail(EmailInfo emailInfo) throws EmailException {
    Objects.requireNonNull(emailInfo.getAddresses(), "addresses are null");
    Objects.requireNonNull(emailInfo.getCaption(), "caption is null");
    Objects.requireNonNull(emailInfo.getBody(), "body is null");
    Objects.requireNonNull(emailInfo.getFrom(), "from is null");
    List<SendingMessage> messages = splitEmail(emailInfo, null, null);
    List<String> failedAddresses = new ArrayList<>();
    List<String> errorMessages = new ArrayList<>();
    for (SendingMessage sendingMessage : messages) {
        SendingMessage persistedMessage = persistMessageIfPossible(sendingMessage);
        try {
            emailSender.sendEmail(sendingMessage);
            if (persistedMessage != null) {
                markAsSent(persistedMessage);
            }
        } catch (Exception e) {
            log.warn("Unable to send email to '" + sendingMessage.getAddress() + "'", e);
            failedAddresses.add(sendingMessage.getAddress());
            errorMessages.add(e.getMessage());
            if (persistedMessage != null) {
                markAsNonSent(persistedMessage);
            }
        }
    }
    if (!failedAddresses.isEmpty()) {
        throw new EmailException(failedAddresses, errorMessages);
    }
}
Also used : SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) AddressException(javax.mail.internet.AddressException) MailSendException(org.springframework.mail.MailSendException)

Example 5 with SendingMessage

use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.

the class Emailer method persistMessages.

protected void persistMessages(List<SendingMessage> sendingMessageList, SendingStatus status) {
    MessagePersistingContext context = new MessagePersistingContext();
    try {
        try (Transaction tx = persistence.createTransaction()) {
            EntityManager em = persistence.getEntityManager();
            for (SendingMessage message : sendingMessageList) {
                message.setStatus(status);
                try {
                    persistSendingMessage(em, message, context);
                } catch (FileStorageException e) {
                    throw new RuntimeException("Failed to store message " + message.getCaption(), e);
                }
            }
            tx.commit();
        }
        context.finished();
    } finally {
        removeOrphanFiles(context);
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Aggregations

SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)19 EntityManager (com.haulmont.cuba.core.EntityManager)8 Transaction (com.haulmont.cuba.core.Transaction)8 SMTPAddressFailedException (com.sun.mail.smtp.SMTPAddressFailedException)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)5 AddressException (javax.mail.internet.AddressException)5 MailSendException (org.springframework.mail.MailSendException)5 SendingAttachment (com.haulmont.cuba.core.entity.SendingAttachment)3 MimeMessage (javax.mail.internet.MimeMessage)2 Test (org.junit.Test)2 Nullable (javax.annotation.Nullable)1 Address (javax.mail.Address)1