public SmtpConsumer( final MessageContext context, final ProcessSessionFactory sessionFactory, final int port, final String host, final ComponentLog log, final int maxMessageSize ) { this.context = context; this.sessionFactory = sessionFactory; this.port = port; if (host == null || host.trim().isEmpty()) { this.host = context.getSMTPServer().getHostName(); } else { this.host = host; } this.log = log; this.maxMessageSize = maxMessageSize; }
public static void start(int port) { SMTPServer smtpServer = new SMTPServer(new SimpleMessageListenerAdapter(new MyMessageListener())); smtpServer.setPort(port); smtpServer.start(); }
public static void startWithAuth(int port) { SMTPServer server = new SMTPServer(new SimpleMessageListenerAdapter(new MyMessageListener())); server.setPort(port); UsernamePasswordValidator validator = (s, s1) -> { if (!"username".equalsIgnoreCase(s) || !"password".equalsIgnoreCase(s1)) { throw new LoginFailedException(); } }; server.setAuthenticationHandlerFactory(new PlainAuthenticationHandlerFactory(validator)); server.start(); }
final SMTPServer smtpServer = sslContextService == null ? new SMTPServer(messageHandlerFactory) : new SMTPServer(messageHandlerFactory) { @Override public SSLSocket createSSLSocket(Socket socket) throws IOException { 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());
public void doTest() throws Exception { Wiser wiser = new Wiser(); SMTPServer smtpServer = createServer(new SimpleMessageListenerAdapter(wiser)); smtpServer.setHostName("localhost"); smtpServer.setPort(5000); TestPluginLifeCycle lifeCycle = createLifeCycle(); try { lifeCycle.start(); smtpServer.start(); execute(lifeCycle, wiser); } finally { smtpServer.stop(); lifeCycle.stop(); } } }
/** * Starts the server on the port and address specified in parameters. * * @param port the SMTP port to be opened. * @param bindAddress the address to bind to. null means bind to all. * @throws BindPortException when the port can't be opened. * @throws OutOfRangePortException when port is out of range. * @throws IllegalArgumentException when port is out of range. */ public void startServer(int port, InetAddress bindAddress) throws BindPortException, OutOfRangePortException { LOGGER.debug("Starting server on port {}", port); try { smtpServer.setBindAddress(bindAddress); smtpServer.setPort(port); smtpServer.start(); } catch (RuntimeException exception) { if (exception.getMessage().contains("BindException")) { // Can't open port LOGGER.error("{}. Port {}", exception.getMessage(), port); throw new BindPortException(exception, port); } else if (exception.getMessage().contains("out of range")) { // Port out of range LOGGER.error("Port {} out of range.", port); throw new OutOfRangePortException(exception, port); } else { // Unknown error LOGGER.error("", exception); throw exception; } } }
@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); } } context.yield();//nothing really to do here since threading managed by smtp server sessions }
public void execute(String commandString, Session sess) throws IOException, DropConnectionException { if (!sess.getServer().getRequireTLS() || sess.isTLSStarted()) wrapped.execute(commandString, sess); else sess.sendResponse("530 Must issue a STARTTLS command first"); }
/** * {@inheritDoc} */ public void execute(String commandString, Session sess) throws IOException, DropConnectionException { if (!sess.getServer().getRequireAuth() || sess.isAuthenticated()) wrapped.execute(commandString, sess); else sess.sendResponse("530 5.7.0 Authentication required"); }
protected SMTPServer createServer(MessageHandlerFactory mhf) { return new SMTPServer(mhf); }
@Override protected SMTPServer createServer(MessageHandlerFactory mhf) { SMTPServer smtpServer = super.createServer(mhf); smtpServer.setAuthenticationHandlerFactory(new LoginAuthenticationHandlerFactory(new UsernamePasswordValidator() { public void login(String username, String password) throws LoginFailedException { done.set(true); throw new LoginFailedException(); } })); return smtpServer; } @Override
@Test public void shouldSendTestEmailWithSTARTTLS() { smtpServer.getServer().setEnableTLS(true); smtpServer.getServer().setRequireTLS(true); configure(); when(configuration.getSecureConnection()).thenReturn("STARTTLS"); try { underTest.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); fail("An SSL exception was expected a a proof that STARTTLS is enabled"); } catch (EmailException e) { // We don't have a SSL certificate so we are expecting a SSL error assertThat(e.getCause().getMessage()).isEqualTo("Could not convert socket to TLS"); } }
@Override public void execute(String commandString, ConnectionContext context) throws IOException { context.sendResponse("221 Bye"); context.getSession().quit(); } }
/** * @return the HelpMessage object for the given command name (verb) * @throws CommandException */ public HelpMessage getHelp(String command) throws CommandException { return this.getCommandFromString(command).getHelp(); }
private void configure() { when(configuration.getSmtpHost()).thenReturn("localhost"); when(configuration.getSmtpPort()).thenReturn(smtpServer.getServer().getPort()); when(configuration.getFrom()).thenReturn("server@nowhere"); when(configuration.getFromName()).thenReturn("SonarQube from NoWhere"); when(configuration.getPrefix()).thenReturn("[SONARQUBE]"); when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org"); }
public void close() { LOGGER.debug("Closing the application and saving the configuration"); Configuration.INSTANCE.set("smtp.default.port", panel.getPortText().get().getText()); Configuration.INSTANCE.set("emails.default.dir", panel.getSaveMsgTextField().get().getText()); try { Configuration.INSTANCE.saveToUserProfile(); } catch (IOException ex) { LOGGER.error("Could not save configuration", ex); } // Check for SMTP server running and stop it if (SMTPServerHandler.INSTANCE.getSmtpServer().isRunning()) { SMTPServerHandler.INSTANCE.getSmtpServer().stop(); } mainFrame.dispose(); } }
public void execute(String commandString, Session sess) throws IOException, DropConnectionException { if (!sess.getServer().getRequireTLS() || sess.isTLSStarted()) wrapped.execute(commandString, sess); else sess.sendResponse("530 Must issue a STARTTLS command first"); }
@Override protected SMTPServer createServer(MessageHandlerFactory mhf) { SMTPServer smtpServer = super.createServer(mhf); smtpServer.setAuthenticationHandlerFactory(new LoginAuthenticationHandlerFactory(new UsernamePasswordValidator() { public void login(String username, String password) throws LoginFailedException { usernameRef.set(username); passwordRef.set(password); } })); return smtpServer; } };