/** * Creates an instance of an SMTPClient object * @return an SMTPClient object */ public SMTPClient createInstance() { return new SMTPClient(); } }
protected SMTPClient createClient() { return new SMTPClient(); }
protected SMTPClient createClient() { return new SMTPClient(); }
protected SMTPClient newSMTPClient() throws IOException { SMTPClient smtp = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtp.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); if (LOGGER.isDebugEnabled()) { smtp.addProtocolCommandListener(new ProtocolCommandListener() { @Override public void protocolCommandSent(ProtocolCommandEvent event) { LOGGER.debug("> {}", event.getMessage().trim()); } @Override public void protocolReplyReceived(ProtocolCommandEvent event) { LOGGER.debug("< {}", event.getMessage().trim()); } }); } return smtp; }
@Test public void testSenderDomainResolvDefault() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.helo(InetAddress.getLocalHost().toString()); String sender1 = "mail_sender1@xfwrqqfgfe.de"; smtpProtocol1.setSender(sender1); smtpProtocol1.quit(); }
@Test public void testEhloResolvDefault() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.sendCommand("ehlo", "abgsfe3rsf.de"); // ehlo should not be checked. so this should give a 250 code assertThat(smtpProtocol1.getReplyCode()) .as("ehlo accepted") .isEqualTo(250); smtpProtocol1.quit(); }
@Test public void testHeloResolvDefault() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.helo("abgsfe3rsf.de"); // helo should not be checked. so this should give a 250 code assertThat(smtpProtocol1.getReplyCode()) .as("Helo accepted") .isEqualTo(250); smtpProtocol1.quit(); }
@Test public void testRelayingDenied() throws Exception { smtpConfiguration.setAuthorizedAddresses("128.0.0.1/8"); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); smtpProtocol.setSender("mail@sample.com"); smtpProtocol.addRecipient("maila@sample.com"); assertThat(smtpProtocol.getReplyCode()) .as("expected 550 error") .isEqualTo(550); }
@Test public void testMaxRcptDefault() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.helo(InetAddress.getLocalHost().toString()); String sender1 = "mail_sender1@james.apache.org"; String rcpt1 = "test@localhost"; smtpProtocol1.setSender(sender1); smtpProtocol1.addRecipient(rcpt1); smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testMaxRcptDefault\r\n"); smtpProtocol1.quit(); }
@Test public void testMaxLineLength() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < AbstractChannelPipelineFactory.MAX_LINE_LENGTH; i++) { sb.append("A"); } smtpProtocol.sendCommand("EHLO " + sb.toString()); System.out.println(smtpProtocol.getReplyString()); assertThat(smtpProtocol.getReplyCode()) .as("Line length exceed") .isEqualTo(500); smtpProtocol.sendCommand("EHLO test"); assertThat(smtpProtocol.getReplyCode()) .as("Line length ok") .isEqualTo(250); smtpProtocol.quit(); smtpProtocol.disconnect(); }
@Test public void testHandleAnnouncedMessageSizeLimitExceeded() throws Exception { smtpConfiguration.setMaxMessageSize(1); // set message limit to 1kb init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); smtpProtocol.sendCommand("MAIL FROM:<mail@localhost> SIZE=1025", null); assertThat(smtpProtocol.getReplyCode()) .as("expected error: max msg size exceeded") .isEqualTo(552); smtpProtocol.addRecipient("mail@localhost"); assertThat(smtpProtocol.getReplyCode()) .as("expected error") .isEqualTo(503); }
@Test public void startTlsCommandShouldFailWhenFollowedByInjectedCommand() throws Exception { smtpConfiguration.setStartTLS(); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull();; smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost()); smtpProtocol.sendCommand("STARTTLS\r\nAUTH PLAIN"); assertThat(smtpProtocol.getReplyCode()).isEqualTo(451); smtpProtocol.disconnect(); }
@Test public void testStartTLSInEHLO() throws Exception { smtpConfiguration.setStartTLS(); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull();; smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost()); String[] capabilityRes = smtpProtocol.getReplyStrings(); List<String> capabilitieslist = new ArrayList<>(); for (int i = 1; i < capabilityRes.length; i++) { capabilitieslist.add(capabilityRes[i].substring(4)); } assertThat(capabilitieslist) .as("capabilities") .hasSize(4); assertThat(capabilitieslist) .as("capabilities present PIPELINING ENHANCEDSTATUSCODES 8BITMIME STARTTLS") .containsOnly("PIPELINING", "ENHANCEDSTATUSCODES", "8BITMIME", "STARTTLS"); smtpProtocol.quit(); smtpProtocol.disconnect(); }
@Test public void startTlsCommandShouldWorkWhenAlone() throws Exception { smtpConfiguration.setStartTLS(); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull();; smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost()); smtpProtocol.sendCommand("STARTTLS"); assertThat(smtpProtocol.getReplyCode()).isEqualTo(220); smtpProtocol.disconnect(); }
@Test public void testAuthCancel() throws Exception { smtpConfiguration.setAuthorizedAddresses("127.0.0.1/8"); smtpConfiguration.setAuthorizingAnnounce(); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); smtpProtocol.sendCommand("AUTH PLAIN"); assertThat(smtpProtocol.getReplyCode()) .as("start auth.") .isEqualTo(334); smtpProtocol.sendCommand("*"); assertThat(smtpProtocol.getReplyCode()) .as("cancel auth.") .isEqualTo(501); smtpProtocol.quit(); }
@Test public void testSenderDomainResolvRelayClientDefault() throws Exception { smtpConfiguration.setSenderDomainResolv(); init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") .isTrue(); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull(); smtpProtocol1.helo(InetAddress.getLocalHost().toString()); String sender1 = "mail_sender1@xfwrqqfgfe.de"; // Both mail shold smtpProtocol1.setSender(sender1); smtpProtocol1.quit(); }
@Test public void testHeloEnforcementDisabled() throws Exception { smtpConfiguration.setHeloEhloEnforcement(false); init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") .isTrue(); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull(); String sender1 = "mail_sender1@localhost"; smtpProtocol1.setSender(sender1); smtpProtocol1.quit(); }
@Test public void testMultipleMailsAndRset() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); smtpProtocol.setSender("mail@sample.com"); smtpProtocol.reset(); smtpProtocol.setSender("mail@sample.com"); smtpProtocol.quit(); // mail was propagated by SMTPServer assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull(); }
@Test public void testSimpleMailSendWithHELO() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull(); smtpProtocol.helo(InetAddress.getLocalHost().toString()); smtpProtocol.setSender("mail@localhost"); smtpProtocol.addRecipient("mail@localhost"); smtpProtocol.sendShortMessageData("Subject: test mail\r\n\r\nTest body testSimpleMailSendWithHELO\r\n.\r\n"); smtpProtocol.quit(); smtpProtocol.disconnect(); // mail was propagated by SMTPServer assertThat(queue.getLastMail()) .as("mail received by mail server") .isNotNull(); }
@Test public void testNoRecepientSpecified() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); smtpProtocol.setSender("mail@sample.com"); // left out for test smtpProtocol.rcpt(new Address("mail@localhost")); smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testNoRecepientSpecified\r\n"); assertThat(SMTPReply.isNegativePermanent(smtpProtocol.getReplyCode())) .as("sending succeeded without recepient") .isTrue(); smtpProtocol.quit(); // mail was propagated by SMTPServer assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull(); }