private void checkReply(SMTPClient sc) throws IOException { if (SMTPReply.isNegativeTransient(sc.getReplyCode())) { sc.disconnect(); throw new IOException("Transient SMTP error " + sc.getReplyCode()); } else if (SMTPReply.isNegativePermanent(sc.getReplyCode())) { sc.disconnect(); throw new IOException("Permanent SMTP error " + sc.getReplyCode()); } }
private void ensureIsDisconnected(SMTPClient client) throws IOException, InterruptedException { int initialConnections = smtpServer.getCurrentConnections(); client.quit(); client.disconnect(); assertIsDisconnected(initialConnections); }
@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 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 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(); }
private void send(ProtocolServer server, InetSocketAddress bindedAddress, String msg) throws SocketException, IOException { SMTPClient client = createClient(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.helo("localhost"); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.setSender(SENDER); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT1); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT2); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); assertThat(client.sendShortMessageData(msg)).isTrue(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); }
private void send(ProtocolServer server, InetSocketAddress bindedAddress, String msg) throws SocketException, IOException { SMTPClient client = createClient(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.helo("localhost"); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.setSender(SENDER); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT1); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT2); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); assertThat(client.sendShortMessageData(msg)).isTrue(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); }
@Test public void testStartTlsNotSupported() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.sendCommand("STARTTLS"); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@Test public void testUnknownCommand() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.sendCommand("UNKNOWN"); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@Test public void testNoop() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.noop(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@Test public void testNoop() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.noop(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@Test public void testStartTlsNotSupported() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.sendCommand("STARTTLS"); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@Test public void testEhloNotSupported() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.sendCommand("HELO localhost"); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).describedAs("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@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 testInvalidHelo() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.helo(""); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@Test public void testInvalidHelo() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.helo(""); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
client.disconnect();
@Test public void testReceivedHeader() throws Exception { init(smtpConfiguration); SMTPClient smtp = newSMTPClient(); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull();; smtp.helo(InetAddress.getLocalHost().toString()); smtp.setSender("mail@localhost"); smtp.addRecipient("mail@localhost"); smtp.sendShortMessageData("Subject: test\r\n\r\n"); smtp.quit(); smtp.disconnect(); assertThat(queue.getLastMail().getMessage().getHeader("Received")) .as("spooled mail has Received header") .isNotNull(); }
@Ignore @Test public void testEmptyMessageReceivedHeader() throws Exception { init(smtpConfiguration); SMTPClient smtp = newSMTPClient(); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull();; smtp.helo(InetAddress.getLocalHost().toString()); smtp.setSender("mail@localhost"); smtp.addRecipient("mail@localhost"); smtp.sendShortMessageData(""); smtp.quit(); smtp.disconnect(); assertThat(queue.getLastMail().getMessage().getHeader("Received")) .as("spooled mail has Received header") .isNotNull(); // TODO: test body size }