use of org.xwiki.mail.XWikiAuthenticator 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.XWikiAuthenticator in project xwiki-platform by xwiki.
the class AuthenticatingIntegrationTest method sendTextMail.
@Test
public void sendTextMail() throws Exception {
// Set the EC
Execution execution = this.componentManager.getInstance(Execution.class);
ExecutionContext executionContext = new ExecutionContext();
XWikiContext xContext = new XWikiContext();
xContext.setWikiId("wiki");
executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext);
when(execution.getContext()).thenReturn(executionContext);
Copier<ExecutionContext> executionContextCloner = this.componentManager.getInstance(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
// Just return the same execution context
when(executionContextCloner.copy(executionContext)).thenReturn(executionContext);
// Step 1: Create a JavaMail Session
Properties properties = this.configuration.getAllProperties();
assertEquals("true", properties.getProperty(DefaultMailSenderConfiguration.JAVAMAIL_SMTP_AUTH));
Session session = Session.getInstance(properties, new XWikiAuthenticator(this.configuration));
// Step 2: Create the Message to send
MimeMessage message = new MimeMessage(session);
message.setSubject("subject");
message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));
// Step 3: Add the Message Body
Multipart multipart = new MimeMultipart("mixed");
// Add text in the body
multipart.addBodyPart(this.defaultBodyPartFactory.create("some text here", Collections.<String, Object>singletonMap("mimetype", "text/plain")));
message.setContent(multipart);
// Step 4: Send the mail
this.sender.sendAsynchronously(Arrays.asList(message), session, null);
// Verify that the mail has been received (wait maximum 30 seconds).
this.mail.waitForIncomingEmail(30000L, 1);
MimeMessage[] messages = this.mail.getReceivedMessages();
assertEquals(1, messages.length);
assertEquals("subject", messages[0].getHeader("Subject", null));
assertEquals("john@doe.com", messages[0].getHeader("To", null));
assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());
BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
assertEquals("some text here", textBodyPart.getContent());
}
use of org.xwiki.mail.XWikiAuthenticator in project xwiki-platform by xwiki.
the class DefaultSessionFactory method create.
@Override
public Session create(Map<String, String> additionProperties) {
Session session;
Properties properties = this.configuration.getAllProperties();
for (Map.Entry<String, String> entry : additionProperties.entrySet()) {
properties.setProperty(entry.getKey(), entry.getValue());
}
if (this.configuration.usesAuthentication()) {
session = Session.getInstance(properties, new XWikiAuthenticator(this.configuration));
} else {
session = Session.getInstance(properties);
}
return session;
}
Aggregations