use of org.xwiki.mail.MailSenderConfiguration 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.MailSenderConfiguration in project xwiki-platform by xwiki.
the class ScriptingIntegrationTest method registerConfiguration.
@BeforeComponent
public void registerConfiguration() throws Exception {
MailSenderConfiguration configuration = new TestMailSenderConfiguration(this.mail.getSmtp().getPort(), null, null, new Properties());
this.componentManager.registerComponent(MailSenderConfiguration.class, configuration);
// Register a test Permission Checker that allows sending mails
ScriptServicePermissionChecker checker = mock(ScriptServicePermissionChecker.class);
this.componentManager.registerComponent(ScriptServicePermissionChecker.class, "test", checker);
// Set the current wiki in the Context
ModelContext modelContext = this.componentManager.registerMockComponent(ModelContext.class);
Mockito.when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki"));
Provider<XWikiContext> xwikiContextProvider = this.componentManager.registerMockComponent(XWikiContext.TYPE_PROVIDER);
when(xwikiContextProvider.get()).thenReturn(Mockito.mock(XWikiContext.class));
this.componentManager.registerMockComponent(ExecutionContextManager.class);
this.componentManager.registerMockComponent(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
EnvironmentConfiguration environmentConfiguration = this.componentManager.registerMockComponent(EnvironmentConfiguration.class);
when(environmentConfiguration.getPermanentDirectoryPath()).thenReturn(System.getProperty("java.io.tmpdir"));
this.componentManager.registerMockComponent(ConverterManager.class);
}
use of org.xwiki.mail.MailSenderConfiguration in project xwiki-platform by xwiki.
the class MailSenderApiTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
this.mockXWiki = mock(XWiki.class);
this.xwiki = (XWiki) this.mockXWiki.proxy();
getContext().setWiki(this.xwiki);
// The plugin init creates a XWiki.Mail document if it doesn't exist and ensure it has the correct
// class properties.
this.mockXWiki.stubs().method("getDocument").with(eq("XWiki.Mail"), ANYTHING).will(returnValue(new XWikiDocument()));
this.mockXWiki.stubs().method("saveDocument");
// Register a mock Mail Sender Configuration component since it's used by MailConfiguration
Mockery mockery = new JUnit4Mockery();
final MailSenderConfiguration mockConfiguration = getComponentManager().registerMockComponent(mockery, MailSenderConfiguration.class);
mockery.checking(new Expectations() {
{
allowing(mockConfiguration).getHost();
will(returnValue("myserver"));
allowing(mockConfiguration).getPort();
will(returnValue(25));
allowing(mockConfiguration).getFromAddress();
will(returnValue(null));
allowing(mockConfiguration).getUsername();
will(returnValue(null));
allowing(mockConfiguration).getPassword();
will(returnValue(null));
allowing(mockConfiguration).getAdditionalProperties();
will(returnValue(new Properties()));
}
});
MailSenderPlugin plugin = new MailSenderPlugin("dummy", "dummy", getContext());
this.api = new MailSenderPluginApi(plugin, getContext());
// Ensure that there are no messages in inbox
Mailbox.clearAll();
}
Aggregations