Search in sources :

Example 1 with Message

use of org.exoplatform.services.mail.Message in project kernel by exoplatform.

the class TestMailService method testSendMessage.

public void testSendMessage() throws Exception {
    Message message = new Message();
    message.setFrom(generateRandomEmailSender());
    message.setTo(generateRandomEmailRecipient());
    message.setCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
    message.setBCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
    message.setSubject(MAIL_SUBJECT);
    message.setBody(MAIL_CONTENTS);
    message.setMimeType(TEXT_HTML);
    Attachment attachment = new Attachment();
    attachment.setInputStream(new ByteArrayInputStream(ATTACHMENT.getBytes()));
    attachment.setMimeType(TEXT_PLAIN);
    message.addAttachment(attachment);
    assertEquals("SMTP server should be now empty", 0, mailServer.getMessages().size());
    assertFalse(isEmailMessageSent(MAIL_SUBJECT));
    service.sendMessage(message);
    Thread.sleep(ONE_SECOND);
    assertEquals("SMTP server should have one message", 5, mailServer.getMessages().size());
    assertTrue(isEmailMessageSent(MAIL_SUBJECT));
}
Also used : Message(org.exoplatform.services.mail.Message) WiserMessage(org.subethamail.wiser.WiserMessage) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Attachment(org.exoplatform.services.mail.Attachment)

Example 2 with Message

use of org.exoplatform.services.mail.Message in project kernel by exoplatform.

the class TestMailService method testSendMessageAsynchExceptionCause.

/**
 * Here we test asynchronous email sending of {@link Message}.
 * We check if we can get real cause of exception, if that occurs during
 * message sending process.
 */
public void testSendMessageAsynchExceptionCause() throws Exception {
    Attachment attachment = new Attachment();
    attachment.setInputStream(new ByteArrayInputStream(ATTACHMENT.getBytes()));
    attachment.setMimeType(TEXT_PLAIN);
    Message message = new Message();
    message.setFrom("!@#$%^&*()");
    message.setTo(generateRandomEmailRecipient());
    message.setCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
    message.setBCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
    message.setSubject(MAIL_SUBJECT);
    message.setBody(MAIL_CONTENTS);
    message.setMimeType(TEXT_HTML);
    message.addAttachment(attachment);
    Future<Boolean> future = service.sendMessageInFuture(message);
    try {
        future.get();
        fail();
    } catch (ExecutionException ee) {
        assertEquals("We tried to send mail with malformed sender field," + " so we expect an AdressException to be the real cause of ExecutionException", ee.getCause().getClass(), AddressException.class);
    }
}
Also used : Message(org.exoplatform.services.mail.Message) WiserMessage(org.subethamail.wiser.WiserMessage) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) AddressException(javax.mail.internet.AddressException) Attachment(org.exoplatform.services.mail.Attachment) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with Message

use of org.exoplatform.services.mail.Message in project kernel by exoplatform.

the class MailServiceImpl method sendMessage.

/**
 * {@inheritDoc}
 */
public void sendMessage(String from, String to, String subject, String body) throws Exception {
    Message message = new Message();
    message.setFrom(from);
    message.setTo(to);
    message.setSubject(subject);
    message.setBody(body);
    sendMessage(message);
}
Also used : Message(org.exoplatform.services.mail.Message) MimeMessage(javax.mail.internet.MimeMessage)

Example 4 with Message

use of org.exoplatform.services.mail.Message in project kernel by exoplatform.

the class TestMailService method testSendMessageInFuture.

/**
 * Here we test asynchronous email sending of {@link Message}.
 * We check concurrent execution of {@link FutureTask}
 */
public void testSendMessageInFuture() throws Exception {
    Message message;
    Attachment attachment = new Attachment();
    attachment.setInputStream(new ByteArrayInputStream(ATTACHMENT.getBytes()));
    attachment.setMimeType(TEXT_PLAIN);
    @SuppressWarnings("unchecked") Future<Boolean>[] futures = new Future[THREAD_NUMBER];
    assertEquals("SMTP server should be now empty", 0, mailServer.getMessages().size());
    for (int i = 0; i < THREAD_NUMBER; i++) {
        assertFalse(isEmailMessageSent(MAIL_SUBJECT + i));
        message = new Message();
        message.setFrom(generateRandomEmailSender());
        message.setTo(generateRandomEmailRecipient());
        message.setCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
        message.setBCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
        message.setSubject(MAIL_SUBJECT + i);
        message.setBody(MAIL_CONTENTS + i);
        message.setMimeType(TEXT_HTML);
        message.addAttachment(attachment);
        futures[i] = service.sendMessageInFuture(message);
    }
    for (int i = 0; i < THREAD_NUMBER; i++) {
        assertTrue(futures[i].get());
        assertTrue(isEmailMessageSent(MAIL_SUBJECT + i));
    }
    // we assume that one thread sends one email
    assertEquals("SMTP server should have" + (5 * THREAD_NUMBER) + " message (asynchronously sent)", 5 * THREAD_NUMBER, mailServer.getMessages().size());
}
Also used : Message(org.exoplatform.services.mail.Message) WiserMessage(org.subethamail.wiser.WiserMessage) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Future(java.util.concurrent.Future) Attachment(org.exoplatform.services.mail.Attachment)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)4 Message (org.exoplatform.services.mail.Message)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Attachment (org.exoplatform.services.mail.Attachment)3 WiserMessage (org.subethamail.wiser.WiserMessage)3 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 AddressException (javax.mail.internet.AddressException)1