/** * 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; } } }
private void configureServer(int port, String host) { mailServer.setPort(port); try { mailServer.getServer().setBindAddress(InetAddress.getByName(host)); } catch (UnknownHostException e) { //if not a valid binding address then fall back to host name as listen address LOGGER.info("Invalid binding address, using it as hostname instead", e); mailServer.setHostname(host); } } }
/** * Start the SMTP Embedded Server; called by the container during deployment * * @throws Exception */ @PostConstruct public void startup() throws Exception { server = new SMTPServer(new SimpleMessageListenerAdapter(listener)); server.setBindAddress(InetAddress.getLoopbackAddress()); server.setPort(BIND_PORT); server.start(); }