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 testSenderDomainResolvRelayClientDefault() throws Exception {
    smtpConfiguration.setSenderDomainResolv();
    init(smtpConfiguration);//w  w w  .  j  av  a  2s  .  co m

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

    assertTrue("first connection taken", smtpProtocol1.isConnected());

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

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());

    String sender1 = "mail_sender1@xfwrqqfgfe.de";

    // Both mail shold
    smtpProtocol1.setSender(sender1);

    smtpProtocol1.quit();

}

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

@Test
public void testSenderDomainResolvRelayClient() throws Exception {
    smtpConfiguration.setSenderDomainResolv();
    smtpConfiguration.setCheckAuthNetworks(true);
    init(smtpConfiguration);/*from w  ww .j  av  a  2s .  c  om*/

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

    assertTrue("first connection taken", smtpProtocol1.isConnected());

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

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());

    String sender1 = "mail_sender1@xfwrqqfgfe.de";
    String sender2 = "mail_sender2@james.apache.org";

    smtpProtocol1.setSender(sender1);
    assertEquals("expected 501 error", 501, smtpProtocol1.getReplyCode());

    smtpProtocol1.setSender(sender2);

    smtpProtocol1.quit();

}

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

@Test
public void testMaxRcpt() throws Exception {
    smtpConfiguration.setMaxRcpt(1);//from w ww.java  2s. c o  m
    init(smtpConfiguration);

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

    assertTrue("first connection taken", smtpProtocol1.isConnected());

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

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());

    String sender1 = "mail_sender1@james.apache.org";
    String rcpt1 = "test@localhost";
    String rcpt2 = "test2@localhost";

    smtpProtocol1.setSender(sender1);
    smtpProtocol1.addRecipient(rcpt1);

    smtpProtocol1.addRecipient(rcpt2);
    assertEquals("expected 452 error", 452, smtpProtocol1.getReplyCode());

    smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testMaxRcpt1\r\n");

    // After the data is send the rcpt count is set back to 0.. So a new
    // mail with rcpt should be accepted

    smtpProtocol1.setSender(sender1);

    smtpProtocol1.addRecipient(rcpt1);

    smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testMaxRcpt2\r\n");

    smtpProtocol1.quit();

}

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

@Test
public void testMaxRcptDefault() throws Exception {
    init(smtpConfiguration);//from w  ww. ja  v  a 2 s.c  o m

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

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());

    String sender1 = "mail_sender1@james.apache.org";
    String rcpt1 = "test@localhost";

    smtpProtocol1.setSender(sender1);

    smtpProtocol1.addRecipient(rcpt1);

    smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testMaxRcptDefault\r\n");

    smtpProtocol1.quit();
}

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

@Test
public void testEhloResolvDefault() throws Exception {
    init(smtpConfiguration);/*  w  w  w.  j a  v a 2  s.co  m*/

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

    smtpProtocol1.sendCommand("ehlo", "abgsfe3rsf.de");
    // ehlo should not be checked. so this should give a 250 code
    assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode());

    smtpProtocol1.quit();
}

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

@Test
public void testReverseEqualsEhlo() throws Exception {
    smtpConfiguration.setReverseEqualsEhlo();
    smtpConfiguration.setAuthorizedAddresses("192.168.0.1");
    // temporary alter the loopback resolution
    InetAddress jamesDomain = null;
    try {/*from ww w.  ja v  a2 s  .c o m*/
        jamesDomain = dnsServer.getByName("james.apache.org");
    } catch (UnknownHostException e) {
        fail("james.apache.org currently cannot be resolved (check your DNS/internet connection/proxy settings to make test pass)");
    }
    dnsServer.setLocalhostByName(jamesDomain);
    try {
        init(smtpConfiguration);

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

        assertTrue("first connection taken", smtpProtocol1.isConnected());

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

        String ehlo1 = "abgsfe3rsf.de";
        String ehlo2 = "james.apache.org";
        String mail = "sender";
        String rcpt = "recipient";

        smtpProtocol1.sendCommand("ehlo", ehlo1);
        smtpProtocol1.setSender(mail);
        smtpProtocol1.addRecipient(rcpt);

        // this should give a 501 code cause the ehlo not equals reverse of
        // ip
        assertEquals("expected error: ehlo not equals reverse of ip", 501, smtpProtocol1.getReplyCode());

        smtpProtocol1.sendCommand("ehlo", ehlo2);
        smtpProtocol1.setSender(mail);
        smtpProtocol1.addRecipient(rcpt);

        // ehlo is resolvable. so this should give a 250 code
        assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode());

        smtpProtocol1.quit();
    } finally {
        dnsServer.setLocalhostByName(null);
    }
}

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

@Test
public void testHeloEnforcement() throws Exception {
    init(smtpConfiguration);/*w ww. j a va 2 s .  c o  m*/

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

    assertTrue("first connection taken", smtpProtocol1.isConnected());

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

    String sender1 = "mail_sender1@localhost";
    smtpProtocol1.setSender(sender1);
    assertEquals("expected 503 error", 503, smtpProtocol1.getReplyCode());

    smtpProtocol1.helo(InetAddress.getLocalHost().toString());

    smtpProtocol1.setSender(sender1);

    smtpProtocol1.quit();
}

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

@Test
public void testHeloEnforcementDisabled() throws Exception {
    smtpConfiguration.setHeloEhloEnforcement(false);
    init(smtpConfiguration);//from   w  w  w . jav  a  2  s.c o m

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

    assertTrue("first connection taken", smtpProtocol1.isConnected());

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

    String sender1 = "mail_sender1@localhost";

    smtpProtocol1.setSender(sender1);

    smtpProtocol1.quit();
}

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

@Test
public void testAuthCancel() throws Exception {
    smtpConfiguration.setAuthorizedAddresses("127.0.0.1/8");
    smtpConfiguration.setAuthorizingAnnounce();
    init(smtpConfiguration);//from   w ww.ja  va2  s. c o  m

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

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

    smtpProtocol.sendCommand("AUTH PLAIN");

    assertEquals("start auth.", 334, smtpProtocol.getReplyCode());

    smtpProtocol.sendCommand("*");

    assertEquals("cancel auth.", 501, smtpProtocol.getReplyCode());

    smtpProtocol.quit();

}

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 ww  w. j a  v a 2 s .com

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