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));
}
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);
}
}
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);
}
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());
}
Aggregations