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

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

Introduction

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

Prototype

public int quit() throws IOException 

Source Link

Document

A convenience method to send the SMTP QUIT command to the server, receive the reply, and return the reply code.

Usage

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

@Test
public void testMailHookTemporaryError() throws Exception {
    MailHook hook = new MailHook() {

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

        }

        @Override
        public void destroy() {

        }

        public HookResult doMail(SMTPSession session, MailAddress sender) {
            return new HookResult(HookReturnCode.DENYSOFT);
        }
    };

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

    ProtocolServer server = null;
    try {
        server = createServer(createProtocol(hook), 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.isNegativeTransient(client.getReplyCode()));

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

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

}

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

@Test
public void testRcptHookPermanentError() throws Exception {
    RcptHook hook = new RcptHook() {

        @Override/*from www. ja v  a 2  s.c  om*/
        public void init(Configuration config) throws ConfigurationException {

        }

        @Override
        public void destroy() {

        }

        public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) {
            if (RCPT1.equals(rcpt.toString())) {
                return new HookResult(HookReturnCode.DENY);
            } else {
                return new HookResult(HookReturnCode.DECLINED);
            }
        }

    };

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

    ProtocolServer server = null;
    try {
        server = createServer(createProtocol(hook), 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(RCPT1);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode()));

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

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

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

}

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

@Test
public void testRcptHookTemporaryError() throws Exception {
    RcptHook hook = new RcptHook() {

        @Override//  ww w .  j av  a  2 s.co  m
        public void init(Configuration config) throws ConfigurationException {

        }

        @Override
        public void destroy() {

        }

        public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) {
            if (RCPT1.equals(rcpt.toString())) {
                return new HookResult(HookReturnCode.DENYSOFT);
            } else {
                return new HookResult(HookReturnCode.DECLINED);
            }
        }

    };

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

    ProtocolServer server = null;
    try {
        server = createServer(createProtocol(hook), 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(RCPT1);
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isNegativeTransient(client.getReplyCode()));

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

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

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

}

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

@Test
public void testNullSender() throws Exception {

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

    ProtocolServer server = null;/*  w ww  . j  av  a 2  s. c  om*/
    try {
        server = createServer(createProtocol(new ProtocolHandler[0]), 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("");
        assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

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

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

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

}

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/*from   w ww .j  a va  2  s  .  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   ww w  .ja v  a 2 s. 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.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.smtpserver.SMTPServerTest.java

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

    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);/*  w w w.j  a  va2s  .c  o 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);/*w  w  w  .j a va 2s  . com*/

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

}