return sendShortMessageData(message);
return sendShortMessageData(message);
/*** * 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); }
/*** * 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); }
return false; return sendShortMessageData(message);
@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 messageExceedingMessageSizeShouldBeRespondedAsOverQuota() throws Exception { // Given init(smtpConfiguration); int maxSize = 1024; smtpServer.setMaximalMessageSize(maxSize); //When 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@localhost"); smtpProtocol.addRecipient("mail@localhost"); // Create a 1K+ message StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Subject: test\r\n\r\n"); String repeatedString = "This is the repeated body...\r\n"; int repeatCount = (maxSize / repeatedString.length()) + 1; for (int i = 0; i < repeatCount; i++) { stringBuilder.append(repeatedString); } stringBuilder.append("\r\n\r\n.\r\n"); smtpProtocol.sendShortMessageData(stringBuilder.toString()); // Then assertThat(smtpProtocol.getReplyString()).isEqualTo("552 Quota exceeded\r\n"); // Finally smtpProtocol.quit(); 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(); }
smtpProtocol.sendShortMessageData(stringBuilder.toString());
@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(); }
@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(); }
assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).describedAs("Reply=" + client.getReplyString()).isTrue(); assertThat(client.sendShortMessageData(MSG1)).isTrue();
@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(); }
.isEqualTo(452); smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testMaxRcpt1\r\n"); smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testMaxRcpt2\r\n");
@Test public void testTwoMailsInSequence() throws Exception { 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@localhost"; String recipient1 = "mail_recipient1@localhost"; smtpProtocol1.setSender(sender1); smtpProtocol1.addRecipient(recipient1); smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoMailsInSequence1\r\n"); verifyLastMail(sender1, recipient1, null); String sender2 = "mail_sender2@localhost"; String recipient2 = "mail_recipient2@localhost"; smtpProtocol1.setSender(sender2); smtpProtocol1.addRecipient(recipient2); smtpProtocol1.sendShortMessageData("Subject: test2\r\n\r\nTest body2 testTwoMailsInSequence2\r\n"); verifyLastMail(sender2, recipient2, null); smtpProtocol1.quit(); smtpProtocol1.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(); }
assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); assertThat(client.sendShortMessageData(MSG1)).isFalse(); assertThat(SMTPReply.isNegativePermanent(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 }
@Test public void forwardingAnEmailShouldWork() throws Exception { jmapGuiceProbe.modifyVacation(AccountId.fromString(USER_WITH_DOMAIN), VacationPatch .builder() .isEnabled(true) .textBody(REASON) .build()); String externalMail = "ray@yopmail.com"; SMTPClient smtpClient = new SMTPClient(); smtpClient.connect(LOCALHOST_IP, guiceJamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()); smtpClient.helo(DOMAIN); smtpClient.setSender(externalMail); smtpClient.rcpt("<" + USER_WITH_DOMAIN + ">"); smtpClient.sendShortMessageData("content"); calmlyAwait.atMost(1, TimeUnit.MINUTES) .untilAsserted(() -> fakeSmtp.assertEmailReceived(response -> response .body("[0].from", equalTo(USER_WITH_DOMAIN)) .body("[0].to[0]", equalTo(externalMail)) .body("[0].text", equalTo(REASON)))); } }