Example usage for org.apache.commons.net.smtp SMTPClient getReplyStrings

List of usage examples for org.apache.commons.net.smtp SMTPClient getReplyStrings

Introduction

In this page you can find the example usage for org.apache.commons.net.smtp SMTPClient getReplyStrings.

Prototype

public String[] getReplyStrings() 

Source Link

Document

Returns the lines of text from the last SMTP server response as an array of strings, one entry per line.

Usage

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testSimpleMailSendWithEHLO() throws Exception {
    init(smtpConfiguration);/*from   www  .  j  av a  2s  .c  o  m*/

    SMTPClient smtpProtocol = new SMTPClient();
    smtpProtocol.connect("127.0.0.1", smtpListenerPort);

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost());
    String[] capabilityRes = smtpProtocol.getReplyStrings();

    List<String> capabilitieslist = new ArrayList<String>();
    for (int i = 1; i < capabilityRes.length; i++) {
        capabilitieslist.add(capabilityRes[i].substring(4));
    }

    assertEquals("capabilities", 3, capabilitieslist.size());
    assertTrue("capabilities present PIPELINING", capabilitieslist.contains("PIPELINING"));
    assertTrue("capabilities present ENHANCEDSTATUSCODES", capabilitieslist.contains("ENHANCEDSTATUSCODES"));
    assertTrue("capabilities present 8BITMIME", capabilitieslist.contains("8BITMIME"));

    smtpProtocol.setSender("mail@localhost");
    smtpProtocol.addRecipient("mail@localhost");

    smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nBody\r\n\r\n.\r\n");
    smtpProtocol.quit();
    smtpProtocol.disconnect();

    // mail was propagated by SMTPServer
    assertNotNull("mail received by mail server", queue.getLastMail());
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testStartTLSInEHLO() throws Exception {
    smtpConfiguration.setStartTLS();/*w  w  w . j av  a2  s.  com*/
    init(smtpConfiguration);

    SMTPClient smtpProtocol = new SMTPClient();
    smtpProtocol.connect("127.0.0.1", smtpListenerPort);

    // no message there, yet
    assertNull("no mail received by mail server", queue.getLastMail());

    smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost());
    String[] capabilityRes = smtpProtocol.getReplyStrings();

    List<String> capabilitieslist = new ArrayList<String>();
    for (int i = 1; i < capabilityRes.length; i++) {
        capabilitieslist.add(capabilityRes[i].substring(4));
    }

    assertEquals("capabilities", 4, capabilitieslist.size());
    assertTrue("capabilities present PIPELINING", capabilitieslist.contains("PIPELINING"));
    assertTrue("capabilities present ENHANCEDSTATUSCODES", capabilitieslist.contains("ENHANCEDSTATUSCODES"));
    assertTrue("capabilities present 8BITMIME", capabilitieslist.contains("8BITMIME"));
    assertTrue("capabilities present STARTTLS", capabilitieslist.contains("STARTTLS"));

    smtpProtocol.quit();
    smtpProtocol.disconnect();

}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testAuth() throws Exception {
    smtpConfiguration.setAuthorizedAddresses("128.0.0.1/8");
    smtpConfiguration.setAuthorizingAnnounce();
    init(smtpConfiguration);//from   w w w.  ja  v  a2s  .  c  om

    SMTPClient smtpProtocol = new SMTPClient();
    smtpProtocol.connect("127.0.0.1", smtpListenerPort);

    smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString());
    String[] capabilityRes = smtpProtocol.getReplyStrings();

    List<String> capabilitieslist = new ArrayList<String>();
    for (int i = 1; i < capabilityRes.length; i++) {
        capabilitieslist.add(capabilityRes[i].substring(4));
    }

    assertTrue("anouncing auth required", capabilitieslist.contains("AUTH LOGIN PLAIN"));
    // is this required or just for compatibility?
    // assertTrue("anouncing auth required",
    // capabilitieslist.contains("AUTH=LOGIN PLAIN"));

    String userName = "test_user_smtp";
    String noexistUserName = "noexist_test_user_smtp";
    String sender = "test_user_smtp@localhost";
    smtpProtocol.sendCommand("AUTH FOO", null);
    assertEquals("expected error: unrecognized authentication type", 504, smtpProtocol.getReplyCode());

    smtpProtocol.setSender(sender);

    smtpProtocol.addRecipient("mail@sample.com");
    assertEquals("expected 530 error", 530, smtpProtocol.getReplyCode());

    assertFalse("user not existing", usersRepository.contains(noexistUserName));

    smtpProtocol.sendCommand("AUTH PLAIN");
    smtpProtocol.sendCommand(Base64.encodeAsString("\0" + noexistUserName + "\0pwd\0"));
    // smtpProtocol.sendCommand(noexistUserName+"pwd".toCharArray());
    assertEquals("expected error", 535, smtpProtocol.getReplyCode());

    usersRepository.addUser(userName, "pwd");

    smtpProtocol.sendCommand("AUTH PLAIN");
    smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0wrongpwd\0"));
    assertEquals("expected error", 535, smtpProtocol.getReplyCode());

    smtpProtocol.sendCommand("AUTH PLAIN");
    smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0pwd\0"));
    assertEquals("authenticated", 235, smtpProtocol.getReplyCode());

    smtpProtocol.sendCommand("AUTH PLAIN");
    assertEquals("expected error: User has previously authenticated.", 503, smtpProtocol.getReplyCode());

    smtpProtocol.addRecipient("mail@sample.com");
    smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testAuth\r\n");

    smtpProtocol.quit();

    // mail was propagated by SMTPServer
    assertNotNull("mail received by mail server", queue.getLastMail());
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

@Test
public void testDNSRBLNotRejectAuthUser() throws Exception {
    smtpConfiguration.setAuthorizedAddresses("192.168.0.1/32");
    smtpConfiguration.setAuthorizingAnnounce();
    smtpConfiguration.useRBL(true);//from   w  ww . j a  va2s . co  m
    init(smtpConfiguration);

    dnsServer.setLocalhostByName(InetAddress.getByName("127.0.0.1"));

    SMTPClient smtpProtocol = new SMTPClient();
    smtpProtocol.connect("127.0.0.1", smtpListenerPort);

    smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString());
    String[] capabilityRes = smtpProtocol.getReplyStrings();

    List<String> capabilitieslist = new ArrayList<String>();
    for (int i = 1; i < capabilityRes.length; i++) {
        capabilitieslist.add(capabilityRes[i].substring(4));
    }

    assertTrue("anouncing auth required", capabilitieslist.contains("AUTH LOGIN PLAIN"));
    // is this required or just for compatibility? assertTrue("anouncing
    // auth required", capabilitieslist.contains("AUTH=LOGIN PLAIN"));

    String userName = "test_user_smtp";
    String sender = "test_user_smtp@localhost";

    smtpProtocol.setSender(sender);

    usersRepository.addUser(userName, "pwd");

    smtpProtocol.sendCommand("AUTH PLAIN");
    smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0pwd\0"));
    assertEquals("authenticated", 235, smtpProtocol.getReplyCode());

    smtpProtocol.addRecipient("mail@sample.com");
    assertEquals("authenticated.. not reject", 250, smtpProtocol.getReplyCode());

    smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testDNSRBLNotRejectAuthUser\r\n");

    smtpProtocol.quit();

    // mail was propagated by SMTPServer
    assertNotNull("mail received by mail server", queue.getLastMail());
}