use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.
the class FileSystemMailContentStoreTest method saveMessageThrowsMailStoreExceptionWhenError.
@Test
public void saveMessageThrowsMailStoreExceptionWhenError() throws Exception {
Environment environment = this.mocker.getInstance(Environment.class);
when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
String batchId = UUID.randomUUID().toString();
String messageId = "ar1vm0Wca42E/dDn3dsH8ogs3/s=";
ExtendedMimeMessage message = mock(ExtendedMimeMessage.class);
when(message.getUniqueMessageId()).thenReturn(messageId);
this.thrown.expect(MailStoreException.class);
this.thrown.expectMessage("Failed to save message (id [" + messageId + "], batch id [" + batchId + "]) into file");
when(message.getContent()).thenReturn("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
doThrow(new IOException()).when(message).writeTo(any(OutputStream.class));
this.mocker.getComponentUnderTest().save(batchId, message);
}
use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.
the class FileSystemMailContentStoreTest method saveMessageWithCustomMessageId.
@Test
public void saveMessageWithCustomMessageId() throws Exception {
String batchId = UUID.randomUUID().toString();
String mimeMessageId = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
ExtendedMimeMessage message = new ExtendedMimeMessage();
message.setMessageId(mimeMessageId);
message.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
this.mocker.getComponentUnderTest().save(batchId, message);
File tempDir = new File(TEMPORARY_DIRECTORY);
File batchDirectory = new File(new File(tempDir, this.mocker.getComponentUnderTest().ROOT_DIRECTORY), URLEncoder.encode(batchId, "UTF-8"));
File messageFile = new File(batchDirectory, URLEncoder.encode(message.getUniqueMessageId(), "UTF-8"));
InputStream in = new FileInputStream(messageFile);
String messageContent = IOUtils.toString(in);
assertTrue(messageContent.contains("Message-ID: " + mimeMessageId));
assertTrue(messageContent.contains("Lorem ipsum dolor sit amet, consectetur adipiscing elit"));
}
use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.
the class MemoryMailStatusResultTest method getAllErrorTest.
@Test
public void getAllErrorTest() throws Exception {
MemoryMailStatusResult statusResult = new MemoryMailStatusResult();
ExtendedMimeMessage message1 = mock(ExtendedMimeMessage.class);
when(message1.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID1);
ExtendedMimeMessage message2 = mock(ExtendedMimeMessage.class);
when(message2.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID2);
ExtendedMimeMessage message3 = mock(ExtendedMimeMessage.class);
when(message3.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID3);
ExtendedMimeMessage message4 = mock(ExtendedMimeMessage.class);
when(message4.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID4);
ExtendedMimeMessage message5 = mock(ExtendedMimeMessage.class);
when(message5.getUniqueMessageId()).thenReturn(UNIQUE_MESSAGE_ID5);
statusResult.setStatus(new MailStatus(BATCH_ID, message1, MailState.PREPARE_SUCCESS));
statusResult.setStatus(new MailStatus(BATCH_ID, message2, MailState.PREPARE_ERROR));
statusResult.setStatus(new MailStatus(BATCH_ID, message3, MailState.SEND_SUCCESS));
statusResult.setStatus(new MailStatus(BATCH_ID, message4, MailState.SEND_ERROR));
statusResult.setStatus(new MailStatus(BATCH_ID, message5, MailState.SEND_FATAL_ERROR));
List<String> allErrorIds = new ArrayList<>();
Iterator<MailStatus> it = statusResult.getAllErrors();
while (it.hasNext()) {
allErrorIds.add(it.next().getMessageId());
}
assertThat(allErrorIds, containsInAnyOrder(UNIQUE_MESSAGE_ID2, UNIQUE_MESSAGE_ID4, UNIQUE_MESSAGE_ID5));
assertThat(allErrorIds.size(), equalTo(3));
}
use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.
the class AbstractTemplateMimeMessageFactory method createMessage.
@Override
public MimeMessage createMessage(Object templateReferenceObject, Map<String, Object> parameters) throws MessagingException {
DocumentReference templateReference = getTypedSource(templateReferenceObject, DocumentReference.class);
// Note: We don't create a Session here ATM since it's not required. The returned MimeMessage will be
// given a valid Session when it's deserialized from the mail content store for sending.
ExtendedMimeMessage message = new ExtendedMimeMessage();
// Handle optional "from" address.
Address from = this.converterManager.convert(Address.class, parameters.get("from"));
if (from != null) {
message.setFrom(from);
}
// Handle optional "to", "cc" and "bcc" addresses.
setRecipient(message, Message.RecipientType.TO, parameters.get("to"));
setRecipient(message, Message.RecipientType.CC, parameters.get("cc"));
setRecipient(message, Message.RecipientType.BCC, parameters.get("bcc"));
// Handle optional "type" parameter to set the mail type
// Set the Message type if passed in parameters
String type = (String) parameters.get("type");
if (type != null) {
message.setType(type);
}
// Handle the subject. Get it from the template
Map<String, Object> velocityVariables = (Map<String, Object>) parameters.get("velocityVariables");
Object localeValue = parameters.get("language");
String subject = getTemplateManager().evaluate(templateReference, "subject", velocityVariables, localeValue);
message.setSubject(subject);
// Add a default body part taken from the template.
Multipart multipart = new MimeMultipart("mixed");
multipart.addBodyPart(getMimeBodyPartFactory().create(templateReference, parameters));
message.setContent(multipart);
return message;
}
use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.
the class AbstractMessageIterator method createMessage.
/**
* @return the MimeMessage as the current element of Iterator.
* @throws MessagingException when an error occurs
*/
public ExtendedMimeMessage createMessage() throws MessagingException {
ExtendedMimeMessage message = createMessageInternal();
// Set the Message type if passed in parameters
String type = (String) parameters.get("type");
if (type != null) {
message.setType(type);
}
return message;
}
Aggregations