Search in sources :

Example 6 with SendingMessage

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

the class Emailer method splitEmail.

protected List<SendingMessage> splitEmail(EmailInfo info, @Nullable Integer attemptsCount, @Nullable Date deadline) {
    List<SendingMessage> sendingMessageList = new ArrayList<>();
    String[] splitAddresses = info.getAddresses().split("[,;]");
    for (String address : splitAddresses) {
        address = address.trim();
        if (StringUtils.isNotBlank(address)) {
            SendingMessage sendingMessage = convertToSendingMessage(address, info.getFrom(), info.getCaption(), info.getBody(), info.getHeaders(), info.getAttachments(), attemptsCount, deadline);
            sendingMessageList.add(sendingMessage);
        }
    }
    return sendingMessageList;
}
Also used : SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Example 7 with SendingMessage

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

the class Emailer method migrateEmailsToFileStorage.

@Override
public void migrateEmailsToFileStorage(List<SendingMessage> messages) {
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        for (SendingMessage msg : messages) {
            migrateMessage(em, msg);
        }
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Example 8 with SendingMessage

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

the class EmailerTest method testMigration.

@Test
public void testMigration() throws Exception {
    emailerConfig.setFileStorageUsed(false);
    byte[] expectedBytes = new byte[] { 1, 2, 3, 4, 6 };
    EmailAttachment fileAttachment = new EmailAttachment(expectedBytes, "invoice.pdf");
    String body = "Hi! This is test email. Bye.";
    EmailInfo emailInfo = new EmailInfo("test@example.com", "Test", body);
    emailInfo.setAttachments(new EmailAttachment[] { fileAttachment });
    List<SendingMessage> messages = emailer.sendEmailAsync(emailInfo);
    SendingMessage msg;
    SendingAttachment attachment;
    // check DB storage
    msg = reload(messages.get(0), "sendingMessage.loadFromQueue");
    attachment = msg.getAttachments().get(0);
    assertNotNull(msg.getContentText());
    assertNull(msg.getContentTextFile());
    assertNotNull(attachment.getContent());
    assertNull(attachment.getContentFile());
    emailer.migrateEmailsToFileStorage(Lists.newArrayList(msg));
    emailer.migrateAttachmentsToFileStorage(Lists.newArrayList(attachment));
    // check file storage
    msg = reload(msg, "sendingMessage.loadFromQueue");
    attachment = msg.getAttachments().get(0);
    assertNull(msg.getContentText());
    assertNotNull(msg.getContentTextFile());
    assertEquals(body, emailer.loadContentText(msg));
    assertNull(attachment.getContent());
    assertNotNull(attachment.getContentFile());
    FileStorageAPI fileStorage = AppBeans.get(FileStorageAPI.NAME);
    byte[] actualBytes = fileStorage.loadFile(attachment.getContentFile());
    assertByteArrayEquals(expectedBytes, actualBytes);
}
Also used : SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) Test(org.junit.Test)

Example 9 with SendingMessage

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

the class EmailerTest method doTestSeveralRecipients.

private void doTestSeveralRecipients(boolean useFs) throws MessagingException {
    emailerConfig.setFileStorageUsed(useFs);
    testMailSender.clearBuffer();
    String body = "Test Email Body";
    // 3 recipients
    String recipients = "misha@example.com,kolya@example.com;tanya@example.com;";
    EmailInfo myInfo = new EmailInfo(recipients, "Test", body);
    List<SendingMessage> messages = emailer.sendEmailAsync(myInfo);
    assertEquals(3, messages.size());
    assertTrue(testMailSender.isEmpty());
    emailer.processQueuedEmails();
    Set<String> recipientSet = new HashSet<>();
    // check
    assertEquals(3, testMailSender.getBufferSize());
    for (int i = 0; i < 3; i++) {
        MimeMessage msg = testMailSender.fetchSentEmail();
        Address[] msgRecipients = msg.getAllRecipients();
        assertEquals(1, msgRecipients.length);
        recipientSet.add(msgRecipients[0].toString());
    }
    assertTrue(recipientSet.contains("misha@example.com"));
    assertTrue(recipientSet.contains("kolya@example.com"));
    assertTrue(recipientSet.contains("tanya@example.com"));
}
Also used : Address(javax.mail.Address) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) MimeMessage(javax.mail.internet.MimeMessage)

Example 10 with SendingMessage

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

the class EmailerTest method doTestSentFromSecondAttempt.

private void doTestSentFromSecondAttempt(boolean useFs) {
    emailerConfig.setFileStorageUsed(useFs);
    testMailSender.clearBuffer();
    String body = "Test Email Body";
    EmailInfo myInfo = new EmailInfo("recipient@example.com", "Test", body);
    List<SendingMessage> messages = emailer.sendEmailAsync(myInfo, 2, getDeadlineWhichDoesntMatter());
    assertEquals(1, messages.size());
    // not sent yet
    assertTrue(testMailSender.isEmpty());
    SendingMessage sendingMsg = reload(messages.get(0));
    assertEquals(SendingStatus.QUEUE, sendingMsg.getStatus());
    // will fail
    testMailSender.failPlease();
    try {
        // try once
        emailer.processQueuedEmails();
        sendingMsg = reload(sendingMsg);
        assertEquals(SendingStatus.QUEUE, sendingMsg.getStatus());
    } finally {
        testMailSender.workNormallyPlease();
    }
    // success now
    emailer.processQueuedEmails();
    sendingMsg = reload(sendingMsg);
    assertEquals(SendingStatus.SENT, sendingMsg.getStatus());
    assertEquals(2, sendingMsg.getAttemptsCount().intValue());
}
Also used : 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