Search in sources :

Example 6 with SMTPServer

use of org.subethamail.smtp.server.SMTPServer in project fake-smtp-server by gessnerfl.

the class SmtpServerConfiguratorTest method shouldConfigureBasicParameters.

@Test
public void shouldConfigureBasicParameters() {
    final Integer port = 1234;
    final InetAddress bindingAddress = mock(InetAddress.class);
    when(fakeSmtpConfigurationProperties.getPort()).thenReturn(port);
    when(fakeSmtpConfigurationProperties.getBindAddress()).thenReturn(bindingAddress);
    final SMTPServer smtpServer = mock(SMTPServer.class);
    sut.configure(smtpServer);
    verify(smtpServer).setPort(port);
    verify(smtpServer).setBindAddress(bindingAddress);
    verify(smtpServer, never()).setAuthenticationHandlerFactory(any(AuthenticationHandlerFactory.class));
}
Also used : SMTPServer(org.subethamail.smtp.server.SMTPServer) AuthenticationHandlerFactory(org.subethamail.smtp.AuthenticationHandlerFactory) EasyAuthenticationHandlerFactory(org.subethamail.smtp.auth.EasyAuthenticationHandlerFactory) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 7 with SMTPServer

use of org.subethamail.smtp.server.SMTPServer in project motech by motech.

the class EmailChannelBundleIT method testEmailSentOnSendEmailEvent.

@Test
public void testEmailSentOnSendEmailEvent() throws MessagingException, IOException, InterruptedException {
    SMTPServer smtpServer = new SMTPServer(new SimpleMessageListenerAdapter(this));
    new Wait(new ContextPublishedWaitCondition(bundleContext, "org.motechproject.motech-platform-event"), 5000).start();
    new Wait(new ContextPublishedWaitCondition(bundleContext), 5000).start();
    try {
        smtpServer.setPort(8099);
        smtpServer.start();
        String messageText = "test message";
        String from = "testfromaddress";
        String to = "testtoaddress";
        String subject = "test subject";
        Map<String, Object> values = new HashMap<>();
        values.put("fromAddress", from);
        values.put("toAddress", to);
        values.put("message", messageText);
        values.put("subject", subject);
        eventRelay.sendEventMessage(new MotechEvent(SendEmailConstants.SEND_EMAIL_SUBJECT, values));
        new Wait(lock, this, 100, 60000).start();
        assertTrue("Message not received", messageReceived);
        assertNotNull(receivedMessageText);
        assertEquals(messageText, receivedMessageText.trim());
    } finally {
        smtpServer.stop();
    }
}
Also used : ContextPublishedWaitCondition(org.motechproject.testing.osgi.wait.ContextPublishedWaitCondition) HashMap(java.util.HashMap) SMTPServer(org.subethamail.smtp.server.SMTPServer) SimpleMessageListenerAdapter(org.subethamail.smtp.helper.SimpleMessageListenerAdapter) Wait(org.motechproject.testing.osgi.wait.Wait) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 8 with SMTPServer

use of org.subethamail.smtp.server.SMTPServer in project nifi by apache.

the class ListenSMTP method prepareServer.

private SMTPServer prepareServer(final ProcessContext context, final ProcessSessionFactory sessionFactory) {
    final int port = context.getProperty(SMTP_PORT).asInteger();
    final String host = context.getProperty(SMTP_HOSTNAME).getValue();
    final ComponentLog log = getLogger();
    final int maxMessageSize = context.getProperty(SMTP_MAXIMUM_MSG_SIZE).asDataSize(DataUnit.B).intValue();
    // create message handler factory
    final MessageHandlerFactory messageHandlerFactory = (final MessageContext mc) -> {
        return new SmtpConsumer(mc, sessionFactory, port, host, log, maxMessageSize);
    };
    // create smtp server
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final SMTPServer smtpServer = sslContextService == null ? new SMTPServer(messageHandlerFactory) : new SMTPServer(messageHandlerFactory) {

        @Override
        public SSLSocket createSSLSocket(Socket socket) throws IOException {
            InetSocketAddress remoteAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
            String clientAuth = context.getProperty(CLIENT_AUTH).getValue();
            SSLContext sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.valueOf(clientAuth));
            SSLSocketFactory socketFactory = sslContext.getSocketFactory();
            SSLSocket sslSocket = (SSLSocket) (socketFactory.createSocket(socket, remoteAddress.getHostName(), socket.getPort(), true));
            sslSocket.setUseClientMode(false);
            if (SSLContextService.ClientAuth.REQUIRED.toString().equals(clientAuth)) {
                this.setRequireTLS(true);
                sslSocket.setNeedClientAuth(true);
            }
            return sslSocket;
        }
    };
    if (sslContextService != null) {
        smtpServer.setEnableTLS(true);
    } else {
        smtpServer.setHideTLS(true);
    }
    smtpServer.setSoftwareName("Apache NiFi SMTP");
    smtpServer.setPort(port);
    smtpServer.setMaxConnections(context.getProperty(SMTP_MAXIMUM_CONNECTIONS).asInteger());
    smtpServer.setMaxMessageSize(maxMessageSize);
    smtpServer.setConnectionTimeout(context.getProperty(SMTP_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    if (context.getProperty(SMTP_HOSTNAME).isSet()) {
        smtpServer.setHostName(context.getProperty(SMTP_HOSTNAME).getValue());
    }
    return smtpServer;
}
Also used : MessageHandlerFactory(org.subethamail.smtp.MessageHandlerFactory) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) ComponentLog(org.apache.nifi.logging.ComponentLog) SSLContextService(org.apache.nifi.ssl.SSLContextService) RestrictedSSLContextService(org.apache.nifi.ssl.RestrictedSSLContextService) SMTPServer(org.subethamail.smtp.server.SMTPServer) MessageContext(org.subethamail.smtp.MessageContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) SmtpConsumer(org.apache.nifi.processors.email.smtp.SmtpConsumer)

Example 9 with SMTPServer

use of org.subethamail.smtp.server.SMTPServer in project nifi by apache.

the class ListenSMTP method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    if (smtp == null) {
        try {
            final SMTPServer server = prepareServer(context, sessionFactory);
            server.start();
            getLogger().debug("Started SMTP Server on port " + server.getPort());
            smtp = server;
        } catch (final Exception ex) {
            // have to catch exception due to awkward exception handling in subethasmtp
            smtp = null;
            getLogger().error("Unable to start SMTP server due to " + ex.getMessage(), ex);
        }
    }
    // nothing really to do here since threading managed by smtp server sessions
    context.yield();
}
Also used : SMTPServer(org.subethamail.smtp.server.SMTPServer) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException)

Example 10 with SMTPServer

use of org.subethamail.smtp.server.SMTPServer in project vertx-examples by vert-x3.

the class LocalSmtpServer method start.

public static void start(int port) {
    SMTPServer smtpServer = new SMTPServer(new SimpleMessageListenerAdapter(new MyMessageListener()));
    smtpServer.setPort(port);
    smtpServer.start();
}
Also used : SMTPServer(org.subethamail.smtp.server.SMTPServer) SimpleMessageListenerAdapter(org.subethamail.smtp.helper.SimpleMessageListenerAdapter)

Aggregations

SMTPServer (org.subethamail.smtp.server.SMTPServer)13 Test (org.junit.Test)8 AuthenticationHandlerFactory (org.subethamail.smtp.AuthenticationHandlerFactory)6 EasyAuthenticationHandlerFactory (org.subethamail.smtp.auth.EasyAuthenticationHandlerFactory)6 FakeSmtpConfigurationProperties (de.gessnerfl.fakesmtp.config.FakeSmtpConfigurationProperties)5 SimpleMessageListenerAdapter (org.subethamail.smtp.helper.SimpleMessageListenerAdapter)4 IOException (java.io.IOException)2 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 HashMap (java.util.HashMap)1 SSLContext (javax.net.ssl.SSLContext)1 SSLSocket (javax.net.ssl.SSLSocket)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 SmtpConsumer (org.apache.nifi.processors.email.smtp.SmtpConsumer)1 RestrictedSSLContextService (org.apache.nifi.ssl.RestrictedSSLContextService)1 SSLContextService (org.apache.nifi.ssl.SSLContextService)1 MotechEvent (org.motechproject.event.MotechEvent)1