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

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

Introduction

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

Prototype

@Override
public void disconnect() throws IOException 

Source Link

Document

Closes the connection to the SMTP server and sets to null some internal data so that the memory may be reclaimed by the garbage collector.

Usage

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

@Test
public void testMessageHookPermanentError() throws Exception {
    TestMessageHook testHook = new TestMessageHook();

    MessageHook hook = new MessageHook() {

        @Override/*  ww  w.  ja  v a  2s  .  c  o m*/
        public void init(Configuration config) throws ConfigurationException {

        }

        @Override
        public void destroy() {

        }

        public HookResult onMessage(SMTPSession session, MailEnvelope mail) {
            return new HookResult(HookReturnCode.DENY);
        }

    };

    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;
    try {
        server = createServer(createProtocol(hook, testHook), address);
        server.bind();

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

        client.helo("localhost");
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.setSender(SENDER);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.addRecipient(RCPT2);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        assertFalse(client.sendShortMessageData(MSG1));
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode()));

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

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

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

}

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

@Test
public void testMessageHookTemporaryError() throws Exception {
    TestMessageHook testHook = new TestMessageHook();

    MessageHook hook = new MessageHook() {

        @Override//from   w  w w  . j  av a 2 s  .  c  om
        public void init(Configuration config) throws ConfigurationException {

        }

        @Override
        public void destroy() {

        }

        public HookResult onMessage(SMTPSession session, MailEnvelope mail) {
            return new HookResult(HookReturnCode.DENYSOFT);
        }

    };

    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;
    try {
        server = createServer(createProtocol(hook, testHook), address);
        server.bind();

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

        client.helo("localhost");
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.setSender(SENDER);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.addRecipient(RCPT2);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        assertFalse(client.sendShortMessageData(MSG1));
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativeTransient(client.getReplyCode()));

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

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

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

}

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

@Test
public void testConnectHandlerPermananet() throws Exception {
    ConnectHandler<SMTPSession> connectHandler = new ConnectHandler<SMTPSession>() {

        @Override/*from   w ww  .j  a va  2s  .  c om*/
        public void init(Configuration config) throws ConfigurationException {

        }

        @Override
        public void destroy() {

        }

        public Response onConnect(SMTPSession session) {
            return new SMTPResponse("554", "Bye Bye");
        }
    };

    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;
    try {

        server = createServer(createProtocol(connectHandler), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode()));

        client.disconnect();

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

}

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

@Test
public void testConnectHandlerTemporary() throws Exception {
    ConnectHandler<SMTPSession> connectHandler = new ConnectHandler<SMTPSession>() {

        @Override//from w w w  .ja  va2s.  com
        public void init(Configuration config) throws ConfigurationException {

        }

        @Override
        public void destroy() {

        }

        public Response onConnect(SMTPSession session) {
            return new SMTPResponse("451", "Bye Bye");
        }
    };

    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;
    try {
        server = createServer(createProtocol(connectHandler), address);
        server.bind();

        SMTPClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativeTransient(client.getReplyCode()));

        client.disconnect();

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

}

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

@Test
public void testDisconnectHandler() throws Exception {

    final AtomicBoolean called = new AtomicBoolean(false);
    DisconnectHandler<SMTPSession> handler = new DisconnectHandler<SMTPSession>() {

        @Override//from   ww  w .j  a  va 2s . c o m
        public void init(Configuration config) throws ConfigurationException {

        }

        @Override
        public void destroy() {

        }

        public void onDisconnect(SMTPSession session) {
            called.set(true);
        }
    };

    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;
    try {
        server = createServer(createProtocol(handler), address);
        server.bind();

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

        client.disconnect();

        Thread.sleep(1000);
        assertTrue(called.get());

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

}

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

@Test
public void testMaxLineLength() throws Exception {
    init(smtpConfiguration);/*from   www .jav  a 2s. c  o  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 testConnectionLimit() throws Exception {
    smtpConfiguration.setConnectionLimit(2);
    init(smtpConfiguration);//from  w  w w . ja va  2s  .co m

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

    SMTPClient smtpProtocol3 = new SMTPClient();

    try {
        smtpProtocol3.connect("127.0.0.1", smtpListenerPort);
        Thread.sleep(3000);
        fail("Shold disconnect connection 3");
    } catch (Exception e) {
    }

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

    smtpProtocol3.connect("127.0.0.1", smtpListenerPort);
    Thread.sleep(3000);

}

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

@Test
public void testSimpleMailSendWithEHLO() throws Exception {
    init(smtpConfiguration);//from  ww  w. j av  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 w  ww  . j a v a2  s .  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 testReceivedHeader() throws Exception {
    init(smtpConfiguration);/*from  ww w.  j av  a 2 s.  co m*/

    SMTPClient smtp = newSMTPClient();

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

    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();

    assertNotNull("spooled mail has Received header", queue.getLastMail().getMessage().getHeader("Received"));
}