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

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

Introduction

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

Prototype

public int sendCommand(String command) throws IOException 

Source Link

Document

Sends an SMTP command with no arguments to the server, waits for a reply and returns the numerical response code.

Usage

From source file:me.schiz.jmeter.protocol.smtp.sampler.SMTPSampler.java

private SampleResult sampleCommand(SampleResult sr) {
    SocketClient soclient = SessionStorage.getInstance().getClient(getSOClient());
    SMTPClient client = null;
    int responseCode = 0;
    if (soclient instanceof SMTPClient)
        client = (SMTPClient) soclient;//from   ww w.j  a v a  2s  .c  o m

    String request = "COMMAND\n";
    request += "Client : " + getClient() + "\n";
    request += "Command : " + getCommand() + "\n";
    sr.setRequestHeaders(request);
    if (client == null) {
        sr.setResponseCode("404");
        sr.setResponseData(("Client `" + getClient() + "` not found").getBytes());
        sr.setSuccessful(false);
        return sr;
    } else {
        synchronized (client) {
            sr.sampleStart();
            try {
                responseCode = client.sendCommand(getCommand());
                sr.setResponseCode(String.valueOf(responseCode));
                sr.setSuccessful(SMTPReply.isPositiveIntermediate(responseCode));
                String response = client.getReplyString();
                setSuccessfulByResponseCode(sr, client.getReplyCode());

                if (SessionStorage.getInstance()
                        .getClientType(getSOClient()) == SessionStorage.proto_type.STARTTLS) {
                    String command;
                    if (getCommand().indexOf(' ') != -1)
                        command = getCommand().substring(0, getCommand().indexOf(' '));
                    else
                        command = getCommand();
                    if ((command.equalsIgnoreCase("lhlo") || command.equalsIgnoreCase("ehlo")
                            || command.equalsIgnoreCase("helo")) && getUseSTARTTLS()
                            && client instanceof SMTPSClient) {
                        SMTPSClient sclient = (SMTPSClient) client;
                        if (sclient.execTLS() == false) {
                            sr.setSuccessful(false);
                            sr.setResponseCode("403");
                            ;
                            response += sclient.getReplyString();
                            log.error("client `" + client + "` STARTTLS failed");
                            removeClient();
                        } else {
                            response += "\nSTARTTLS OK";
                        }
                    }
                }
                sr.setResponseData(response.getBytes());
            } catch (IOException e) {
                sr.setSuccessful(false);
                sr.setResponseData(e.toString().getBytes());
                sr.setResponseCode(e.getClass().getName());
                log.error("client `" + client + "` ", e);
                removeClient();
            }
            sr.sampleEnd();
        }
    }
    return sr;
}

From source file:ProtocolRunner.java

private static void handleSMTP(
    SMTPClient client, 
    ProtocolRunner runner, /*from w w  w .j a va 2s .  c o m*/
    String commandString) 
    throws IOException {
        
    if(commandString == null) { // means just connected 
        // check if the server response was valid
        if(!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
            runner.handleDisconnect();
            return;
        }
    } else { // need to handle a command
        client.sendCommand(commandString);
    }
        
    runner.getTCPServerResponse().append(client.getReplyString());
}

From source file:org.apache.james.protocols.lmtp.AbstractLMTPServerTest.java

@Test
public void testEhloNotSupported() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;/*from   w w w  .  j a  v  a  2s.  com*/
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.sendCommand("HELO localhost");
        assertTrue(SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();

        Iterator<MailEnvelope> queued = hook.getQueued().iterator();
        assertFalse(queued.hasNext());

    } finally {
        if (server != null) {
            server.unbind();
        }
    }
}

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testStartTlsNotSupported() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;/*  ww w. j  a  v  a  2 s.c  om*/
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.sendCommand("STARTTLS");
        assertTrue(SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();
        Iterator<MailEnvelope> queued = hook.getQueued().iterator();
        assertFalse(queued.hasNext());

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:org.apache.james.protocols.smtp.AbstractSMTPServerTest.java

@Test
public void testUnknownCommand() throws Exception {
    TestMessageHook hook = new TestMessageHook();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;/*from w  w w  .j ava  2s .  c om*/
    try {
        server = createServer(createProtocol(hook), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.sendCommand("UNKNOWN");
        assertTrue(SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.quit();
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));
        client.disconnect();
        Iterator<MailEnvelope> queued = hook.getQueued().iterator();
        assertFalse(queued.hasNext());

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

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

@Test
public void testMaxLineLength() throws Exception {
    init(smtpConfiguration);//from w w  w. j  a va 2s  .co m

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

    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());
    assertEquals("Line length exceed", 500, smtpProtocol.getReplyCode());

    smtpProtocol.sendCommand("EHLO test");
    assertEquals("Line length ok", 250, smtpProtocol.getReplyCode());

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

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

@Test
public void testSimpleMailSendWithEHLO() throws Exception {
    init(smtpConfiguration);//w  w  w  .jav  a  2  s  .co  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();/*from  ww w  .  j a  v  a  2s  .  c o m*/
    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 testAuthWithEmptySender() throws Exception {
    smtpConfiguration.setAuthorizedAddresses("128.0.0.1/8");
    smtpConfiguration.setAuthorizingAnnounce();
    init(smtpConfiguration);//  w  w  w  .  j ava 2 s.  c  o  m

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

    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"));
    assertEquals("authenticated", 235, smtpProtocol.getReplyCode());

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

    smtpProtocol.quit();
}

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

@Test
public void testNoRecepientSpecified() throws Exception {
    init(smtpConfiguration);/*from  w  w  w . j a  v  a  2  s.  c o  m*/

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

    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");
    assertTrue("sending succeeded without recepient",
            SMTPReply.isNegativePermanent(smtpProtocol.getReplyCode()));

    smtpProtocol.quit();

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