Search in sources :

Example 1 with MailSenderConfiguration

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);
    }
}
Also used : MailListener(org.xwiki.mail.MailListener) InternetAddress(javax.mail.internet.InternetAddress) XWikiAuthenticator(org.xwiki.mail.XWikiAuthenticator) VelocityContext(org.apache.velocity.VelocityContext) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MailSender(org.xwiki.mail.MailSender) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) MailSenderConfiguration(org.xwiki.mail.MailSenderConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(javax.mail.internet.MimeMessage) Session(javax.mail.Session)

Example 2 with MailSenderConfiguration

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);
}
Also used : ModelContext(org.xwiki.model.ModelContext) ExecutionContext(org.xwiki.context.ExecutionContext) EnvironmentConfiguration(org.xwiki.environment.internal.EnvironmentConfiguration) Copier(org.xwiki.mail.internal.thread.context.Copier) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) WikiReference(org.xwiki.model.reference.WikiReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ScriptServicePermissionChecker(org.xwiki.mail.script.ScriptServicePermissionChecker) MailSenderConfiguration(org.xwiki.mail.MailSenderConfiguration) BeforeComponent(org.xwiki.test.annotation.BeforeComponent)

Example 3 with MailSenderConfiguration

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();
}
Also used : Expectations(org.jmock.Expectations) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) XWiki(com.xpn.xwiki.XWiki) Properties(java.util.Properties) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) MailSenderConfiguration(org.xwiki.mail.MailSenderConfiguration)

Aggregations

MailSenderConfiguration (org.xwiki.mail.MailSenderConfiguration)3 Properties (java.util.Properties)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 Session (javax.mail.Session)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 NamingException (javax.naming.NamingException)1 URIException (org.apache.commons.httpclient.URIException)1 VelocityContext (org.apache.velocity.VelocityContext)1