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(); }
@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; } };
@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
@After public void tearDown() { // clear any authentication handler factory WISER.getServer().setAuthenticationHandlerFactory(null); }
private void setupServerAuth() { WISER.getServer().setAuthenticationHandlerFactory(new EasyAuthenticationHandlerFactory(new UsernamePasswordValidator() { public void login(String username, String password) throws LoginFailedException { if (!username.equals(password)) { throw new LoginFailedException("username=" + username + ", password=" + password); } } })); } }
@Test public void sendHtmlWithBadAuthentication() throws Exception { // Add authentication to Wiser SMTP server wiser.getServer().setAuthenticationHandlerFactory(new TestAuthHandlerFactory()); // Input String testMethodName = Thread.currentThread().getStackTrace()[1].getMethodName(); String toAddress = "mags@bennetstore.com"; String fromAddress = "rgivens@kty.us.gov"; checkBadAuthentication(toAddress, fromAddress, testMethodName, authUsername, "bad password"); checkBadAuthentication(toAddress, fromAddress, testMethodName, "badUserName", authPassword); }
@Test public void sendHtmlWithAuthentication() throws Exception { // Add authentication to Wiser SMTP server wiser.getServer().setAuthenticationHandlerFactory(new TestAuthHandlerFactory()); // Input String testMethodName = Thread.currentThread().getStackTrace()[1].getMethodName(); String toAddress = "rgivens@kty.us.gov"; String fromAddress = "whawkins@kty.us.gov"; // Setup email WorkItemImpl workItem = createEmailWorkItem(toAddress, fromAddress, testMethodName); Connection connection = new Connection(emailHost, emailPort, authUsername, authPassword); sendAndCheckThatMessagesAreSent(workItem, connection); }
@Override public void startup() { serverImpl = new SMTPServer(new HandlerFactory()); // MER - May need to override SMTPServer.createSSLSocket to specify non default keystore. serverImpl.setPort(getPort()); serverImpl.setHostName(getDomain()); serverImpl.setMaxConnections(getMaxConnections()); serverImpl.setHideTLS(isHideTLS()); serverImpl.setEnableTLS(isEnableTLS()); serverImpl.setRequireTLS(isRequireTLS()); if(isAuthenticate()) { AuthenticationHandlerFactory authenticationHandler = new EasyAuthenticationHandlerFactory(new AlfrescoLoginUsernamePasswordValidator()); serverImpl.setAuthenticationHandlerFactory(authenticationHandler); } serverImpl.start(); log.info("Inbound SMTP Email Server has started successfully, on hostName:" + getDomain() + "port:" + getPort()); }
@Test public void useEmailWorkItemHandlerWithAuthentication() throws Exception { // Add authentication to Wiser SMTP server wiser.getServer().setAuthenticationHandlerFactory(new TestAuthHandlerFactory()); // Input String testMethodName = Thread.currentThread().getStackTrace()[1].getMethodName(); String toAddress = "rgivens@yahoo.com"; String fromAddress = "rgivens@kty.us.gov"; EmailWorkItemHandler handler = new EmailWorkItemHandler(); handler.setConnection( emailHost, emailPort, authUsername, authPassword ); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter( "To", toAddress ); workItem.setParameter( "From", fromAddress ); workItem.setParameter( "Reply-To", fromAddress ); workItem.setParameter( "Subject", "Test mail for " + testMethodName ); workItem.setParameter( "Body", "Don't forget to check on Boyd later today." ); WorkItemManager manager = new DefaultWorkItemManager(null); handler.executeWorkItem( workItem, manager ); List<WiserMessage> messages = wiser.getMessages(); assertEquals(1, messages.size()); for (WiserMessage wiserMessage : messages ) { MimeMessage message = wiserMessage.getMimeMessage(); assertEquals(workItem.getParameter("Subject"), message.getSubject()); assertTrue(Arrays.equals(InternetAddress.parse(toAddress), message.getRecipients(RecipientType.TO))); } }
@Override public void startup() { serverImpl = new SMTPServer(new HandlerFactory()); // MER - May need to override SMTPServer.createSSLSocket to specify non default keystore. serverImpl.setPort(getPort()); serverImpl.setHostName(getDomain()); serverImpl.setMaxConnections(getMaxConnections()); serverImpl.setHideTLS(isHideTLS()); serverImpl.setEnableTLS(isEnableTLS()); serverImpl.setRequireTLS(isRequireTLS()); if(isAuthenticate()) { AuthenticationHandlerFactory authenticationHandler = new EasyAuthenticationHandlerFactory(new AlfrescoLoginUsernamePasswordValidator()); serverImpl.setAuthenticationHandlerFactory(authenticationHandler); } serverImpl.start(); log.info("Inbound SMTP Email Server has started successfully, on hostName:" + getDomain() + "port:" + getPort()); }
@Test public void sendHtmlWithAuthenticationAndAttachments() throws Exception { wiser.getServer().setAuthenticationHandlerFactory(new TestAuthHandlerFactory());
protected void startSMTP(String factory) { wiser = new Wiser(); wiser.setPort(1587); wiser.getServer().setAuthenticationHandlerFactory(new AuthenticationHandlerFactory() { /* * AUTH PLAIN handler which returns success on any string */ @Override public List<String> getAuthenticationMechanisms() { return Arrays.asList("PLAIN"); } @Override public AuthenticationHandler create() { return new AuthenticationHandler() { @Override public String auth(final String clientInput) throws RejectException { log.info(clientInput); return null; } @Override public Object getIdentity() { return "username"; } }; } }); Security.setProperty("ssl.SocketFactory.provider", factory); wiser.getServer().setEnableTLS(true); wiser.start(); }