Search in sources :

Example 1 with XWikiAuthenticator

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);
    }
}
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 XWikiAuthenticator

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());
}
Also used : BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) XWikiAuthenticator(org.xwiki.mail.XWikiAuthenticator) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Copier(org.xwiki.mail.internal.thread.context.Copier) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Session(javax.mail.Session) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 3 with XWikiAuthenticator

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;
}
Also used : XWikiAuthenticator(org.xwiki.mail.XWikiAuthenticator) Properties(java.util.Properties) Map(java.util.Map) Session(javax.mail.Session)

Aggregations

Session (javax.mail.Session)3 XWikiAuthenticator (org.xwiki.mail.XWikiAuthenticator)3 Properties (java.util.Properties)2 InternetAddress (javax.mail.internet.InternetAddress)2 MimeMessage (javax.mail.internet.MimeMessage)2 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)1 XWikiContext (com.xpn.xwiki.XWikiContext)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 Map (java.util.Map)1 BodyPart (javax.mail.BodyPart)1 Multipart (javax.mail.Multipart)1 MimeMultipart (javax.mail.internet.MimeMultipart)1