if (!addRecipient(recipient)) { return false;
if (addRecipient(recipients[count])) { oneSuccess = true;
@Override public boolean addRecipient(RelayPath path) throws IOException { boolean ok = super.addRecipient(path); if (ok) { rcptCount++; } return ok; }
@Override public boolean addRecipient(String address) throws IOException { boolean ok = super.addRecipient(address); if (ok) { rcptCount++; } return ok; }
/*** * A convenience method for a sending short email without having to * explicitly set the sender and recipient(s). This method * sets the sender and recipient using * {@link #setSender setSender } and * {@link #addRecipient addRecipient }, and then sends the * message using {@link #sendShortMessageData sendShortMessageData }. * <p> * @param sender The email address of the sender. * @param recipient The email address of the recipient. * @param message The short email message to send. * @return True if successfully completed, false if not. * @exception SMTPConnectionClosedException * If the SMTP server prematurely closes the connection as a result * of the client being idle or some other reason causing the server * to send SMTP reply code 421. This exception may be caught either * as an IOException or independently as itself. * @exception IOException If an I/O error occurs while either sending a * command to the server or receiving a reply from the server. ***/ public boolean sendSimpleMessage(String sender, String recipient, String message) throws IOException { if (!setSender(sender)) return false; if (!addRecipient(recipient)) return false; return sendShortMessageData(message); }
@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 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); }
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 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 testSenderDomainResolv() throws Exception { smtpConfiguration.setSenderDomainResolv(); smtpConfiguration.setAuthorizedAddresses("192.168.0.1/32"); 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"; smtpProtocol1.setSender(sender1); assertThat(smtpProtocol1.getReplyCode()) .as("expected 501 error") .isEqualTo(501); smtpProtocol1.addRecipient("test@localhost"); assertThat(smtpProtocol1.getReplyCode()) .as("Recipient not accepted cause no valid sender") .isEqualTo(503); smtpProtocol1.quit(); }
@Test public void testDNSRBLRejectWorks() throws Exception { smtpConfiguration.setAuthorizedAddresses("192.168.0.1/32"); smtpConfiguration.useRBL(true); init(smtpConfiguration); dnsServer.setLocalhostByName(InetAddress.getByName("127.0.0.1")); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); String sender = "test_user_smtp@localhost"; smtpProtocol.setSender(sender); smtpProtocol.addRecipient("mail@sample.com"); assertThat(smtpProtocol.getReplyCode()) .as("reject") .isEqualTo(554); smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testDNSRBLRejectWorks\r\n"); smtpProtocol.quit(); // mail was rejected by SMTPServer assertThat(queue.getLastMail()) .as("mail reject by mail server") .isNull(); }
assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT1); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue();
assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT1); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue();
@Test public void testAuthWithEmptySender() throws Exception { smtpConfiguration.setAuthorizedAddresses("128.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()); String userName = "test_user_smtp"; usersRepository.addUser(userName, "pwd"); smtpProtocol.setSender(""); smtpProtocol.sendCommand("AUTH PLAIN"); smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0pwd\0")); assertThat(smtpProtocol.getReplyCode()) .as("authenticated") .isEqualTo(235); smtpProtocol.addRecipient("mail@sample.com"); assertThat(smtpProtocol.getReplyCode()) .as("expected error") .isEqualTo(503); smtpProtocol.quit(); }
@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(); }
assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT1); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue();
assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT1); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue();
@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 }