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.smtpserver.SMTPServerTest.java

@Test
public void testAuthWithEmptySender() throws Exception {
    smtpConfiguration.setAuthorizedAddresses("128.0.0.1/8");
    smtpConfiguration.setAuthorizingAnnounce();
    init(smtpConfiguration);/*  ww w . ja v a2 s  .  com*/

    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);// w ww. j  a v  a2  s  .co 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());
}

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

@Test
public void testMultipleMailsAndRset() throws Exception {
    init(smtpConfiguration);/*w  w w.  j a v  a  2  s  .  c om*/

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

    smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost());

    smtpProtocol.setSender("mail@sample.com");

    smtpProtocol.reset();

    smtpProtocol.setSender("mail@sample.com");

    smtpProtocol.quit();

    // mail was propagated by SMTPServer
    assertNull("no 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  w  w . ja  v  a 2 s  .  com*/
    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());
}

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

@Test
public void testDNSRBLRejectWorks() throws Exception {
    smtpConfiguration.setAuthorizedAddresses("192.168.0.1/32");
    smtpConfiguration.useRBL(true);/*from   ww  w  .j  a va2  s  .  c om*/
    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 sender = "test_user_smtp@localhost";

    smtpProtocol.setSender(sender);

    smtpProtocol.addRecipient("mail@sample.com");
    assertEquals("reject", 554, smtpProtocol.getReplyCode());

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

    smtpProtocol.quit();

    // mail was rejected by SMTPServer
    assertNull("mail reject by mail server", queue.getLastMail());
}

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

@Test
public void testAddressBracketsEnforcementDisabled() throws Exception {
    smtpConfiguration.setAddressBracketsEnforcement(false);
    init(smtpConfiguration);// www  .j a  v  a2  s. c om
    SMTPClient smtpProtocol = new SMTPClient();
    smtpProtocol.connect("127.0.0.1", smtpListenerPort);

    smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString());

    smtpProtocol.sendCommand("mail from:", "test@localhost");
    assertEquals("accept", 250, smtpProtocol.getReplyCode());

    smtpProtocol.sendCommand("rcpt to:", "mail@sample.com");
    assertEquals("accept", 250, smtpProtocol.getReplyCode());

    smtpProtocol.quit();

    smtpProtocol.connect("127.0.0.1", smtpListenerPort);

    smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString());

    smtpProtocol.sendCommand("mail from:", "<test@localhost>");
    assertEquals("accept", 250, smtpProtocol.getReplyCode());

    smtpProtocol.sendCommand("rcpt to:", "<mail@sample.com>");
    assertEquals("accept", 250, smtpProtocol.getReplyCode());

    smtpProtocol.quit();
}

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

@Test
public void testAddressBracketsEnforcementEnabled() throws Exception {
    init(smtpConfiguration);//  w  w  w.  j  a v a2  s  . c  o m
    SMTPClient smtpProtocol = new SMTPClient();
    smtpProtocol.connect("127.0.0.1", smtpListenerPort);

    smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString());

    smtpProtocol.sendCommand("mail from:", "test@localhost");
    assertEquals("reject", 501, smtpProtocol.getReplyCode());
    smtpProtocol.sendCommand("mail from:", "<test@localhost>");
    assertEquals("accept", 250, smtpProtocol.getReplyCode());

    smtpProtocol.sendCommand("rcpt to:", "mail@sample.com");
    assertEquals("reject", 501, smtpProtocol.getReplyCode());
    smtpProtocol.sendCommand("rcpt to:", "<mail@sample.com>");
    assertEquals("accept", 250, smtpProtocol.getReplyCode());

    smtpProtocol.quit();
}