Search in sources :

Example 1 with MailSender

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

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));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) ArrayList(java.util.ArrayList) MailSender(org.xwiki.mail.MailSender) Map(java.util.Map) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 3 with MailSender

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();
}
Also used : MailListener(org.xwiki.mail.MailListener) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailContentStore(org.xwiki.mail.MailContentStore) MailSender(org.xwiki.mail.MailSender) Test(org.junit.Test)

Example 4 with MailSender

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));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailContentStore(org.xwiki.mail.MailContentStore) ArrayList(java.util.ArrayList) MailSender(org.xwiki.mail.MailSender) Map(java.util.Map) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Aggregations

MailListener (org.xwiki.mail.MailListener)4 MailSender (org.xwiki.mail.MailSender)4 Test (org.junit.Test)3 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)3 MailContentStore (org.xwiki.mail.MailContentStore)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 MailStatus (org.xwiki.mail.MailStatus)2 MailStatusStore (org.xwiki.mail.MailStatusStore)2 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