use of org.xwiki.mail.MailSender in project xwiki-platform by xwiki.
the class XWiki method sendValidationEmail.
public void sendValidationEmail(String xwikiname, String password, String email, String addfieldname, String addfieldvalue, String contentfield, XWikiContext context) throws XWikiException {
MailSenderConfiguration configuration = Utils.getComponent(MailSenderConfiguration.class);
String sender;
String content;
try {
sender = configuration.getFromAddress();
if (StringUtils.isBlank(sender)) {
String server = context.getRequest().getServerName();
if (server.matches("\\[.*\\]|(\\d{1,3}+\\.){3}+\\d{1,3}+")) {
sender = "noreply@domain.net";
} else {
sender = "noreply@" + server;
}
}
content = getXWikiPreference(contentfield, context);
} catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_EMAIL, XWikiException.ERROR_XWIKI_EMAIL_CANNOT_GET_VALIDATION_CONFIG, "Exception while reading the validation email config", e, null);
}
try {
VelocityContext vcontext = (VelocityContext) context.get("vcontext");
vcontext.put(addfieldname, addfieldvalue);
vcontext.put("email", email);
vcontext.put("password", password);
vcontext.put("sender", sender);
vcontext.put("xwikiname", xwikiname);
content = parseContent(content, context);
} catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_EMAIL, XWikiException.ERROR_XWIKI_EMAIL_CANNOT_PREPARE_VALIDATION_EMAIL, "Exception while preparing the validation email", e, null);
}
// Let's now send the message
try {
Session session = Session.getInstance(configuration.getAllProperties(), new XWikiAuthenticator(configuration));
InputStream is = new ByteArrayInputStream(content.getBytes());
MimeMessage message = new MimeMessage(session, is);
message.setFrom(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO, email);
message.setHeader("X-MailType", "Account Validation");
MailSender mailSender = Utils.getComponent(MailSender.class);
MailListener mailListener = Utils.getComponent(MailListener.class, "database");
mailSender.sendAsynchronously(Arrays.asList(message), session, mailListener);
mailListener.getMailStatusResult().waitTillProcessed(Long.MAX_VALUE);
String errorMessage = MailStatusResultSerializer.serializeErrors(mailListener.getMailStatusResult());
if (errorMessage != null) {
throw new XWikiException(XWikiException.MODULE_XWIKI_EMAIL, XWikiException.ERROR_XWIKI_EMAIL_ERROR_SENDING_EMAIL, String.format("Error while sending the validation email. %s", errorMessage));
}
} catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_EMAIL, XWikiException.ERROR_XWIKI_EMAIL_ERROR_SENDING_EMAIL, "Error while sending the validation email", e);
}
}
use of org.xwiki.mail.MailSender in project xwiki-platform by xwiki.
the class DatabaseMailResenderTest method resendAsynchronouslySeveralMessagesWhenMailContentStoreLoadingFailsForFirstMessage.
@Test
public void resendAsynchronouslySeveralMessagesWhenMailContentStoreLoadingFailsForFirstMessage() throws Exception {
Map filterMap = Collections.singletonMap("state", "prepare_%");
MailStatus status1 = new MailStatus();
status1.setBatchId("batch1");
status1.setMessageId("message1");
MailStatus status2 = new MailStatus();
status2.setBatchId("batch2");
status2.setMessageId("message2");
List<MailStatus> statuses = new ArrayList<>();
statuses.add(status1);
statuses.add(status2);
MailStatusStore statusStore = this.mocker.getInstance(MailStatusStore.class, "database");
when(statusStore.load(filterMap, 0, 0, null, true)).thenReturn(statuses);
MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
when(contentStore.load(any(), eq("batch1"), eq("message1"))).thenThrow(new MailStoreException("error1"));
ExtendedMimeMessage message2 = new ExtendedMimeMessage();
when(contentStore.load(any(), eq("batch2"), eq("message2"))).thenReturn(message2);
MailSender sender = this.mocker.getInstance(MailSender.class);
this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 0, 0);
// The test is here
verify(this.mocker.getMockedLogger()).warn("Failed to load mail content for batchId [{}], messageId [{}]", "batch1", "message1");
verify(sender).sendAsynchronously(eq(Arrays.asList(message2)), any(), any(MailListener.class));
}
use of org.xwiki.mail.MailSender in project xwiki-platform by xwiki.
the class DatabaseMailResenderTest method resendAsynchronouslySingleMesssage.
@Test
public void resendAsynchronouslySingleMesssage() throws Exception {
MailListener listener = this.mocker.registerMockComponent(MailListener.class, "database");
ExtendedMimeMessage message = new ExtendedMimeMessage();
String batchId = UUID.randomUUID().toString();
MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
when(contentStore.load(any(), eq(batchId), eq("messageId"))).thenReturn(message);
MailSender sender = this.mocker.getInstance(MailSender.class);
when(sender.sendAsynchronously(eq(Arrays.asList(message)), any(), any(MailListener.class))).thenReturn(new DefaultMailResult(batchId));
this.mocker.getComponentUnderTest().resendAsynchronously(batchId, "messageId");
// The test is here
verify(sender).sendAsynchronously(eq(Arrays.asList(message)), any(), same(listener));
verify(listener).getMailStatusResult();
}
use of org.xwiki.mail.MailSender in project xwiki-platform by xwiki.
the class DatabaseMailResenderTest method resendAsynchronouslySeveralMessages.
@Test
public void resendAsynchronouslySeveralMessages() throws Exception {
Map filterMap = Collections.singletonMap("state", "prepare_%");
MailStatus status1 = new MailStatus();
status1.setBatchId("batch1");
status1.setMessageId("message1");
MailStatus status2 = new MailStatus();
status2.setBatchId("batch2");
status2.setMessageId("message2");
List<MailStatus> statuses = new ArrayList<>();
statuses.add(status1);
statuses.add(status2);
MailStatusStore statusStore = this.mocker.getInstance(MailStatusStore.class, "database");
when(statusStore.load(filterMap, 0, 0, null, true)).thenReturn(statuses);
MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
ExtendedMimeMessage message1 = new ExtendedMimeMessage();
when(contentStore.load(any(), eq("batch1"), eq("message1"))).thenReturn(message1);
ExtendedMimeMessage message2 = new ExtendedMimeMessage();
when(contentStore.load(any(), eq("batch2"), eq("message2"))).thenReturn(message2);
MailSender sender = this.mocker.getInstance(MailSender.class);
this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 0, 0);
// The test is here
verify(sender).sendAsynchronously(eq(Arrays.asList(message1)), any(), any(MailListener.class));
verify(sender).sendAsynchronously(eq(Arrays.asList(message2)), any(), any(MailListener.class));
}
Aggregations