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

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

Introduction

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

Prototype

public boolean sendShortMessageData(String message) throws IOException 

Source Link

Document

A convenience method for sending short messages.

Usage

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

@Ignore
@Test//from   ww w. ja  v  a2 s.c  o  m
public void testEmptyMessageReceivedHeader() throws Exception {
    init(smtpConfiguration);

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

    smtp.quit();
    smtp.disconnect();

    assertNotNull("spooled mail has Received header", queue.getLastMail().getMessage().getHeader("Received"));
    // TODO: test body size
}

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

@Test
public void testSimpleMailSendWithHELO() throws Exception {
    init(smtpConfiguration);// www.j av  a2 s.c  o  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.helo(InetAddress.getLocalHost().toString());

    smtpProtocol.setSender("mail@localhost");

    smtpProtocol.addRecipient("mail@localhost");

    smtpProtocol
            .sendShortMessageData("Subject: test mail\r\n\r\nTest body testSimpleMailSendWithHELO\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 testTwoSimultaneousMails() throws Exception {
    init(smtpConfiguration);//from   www.  ja  v a2s .co  m

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

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

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

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

    String sender1 = "mail_sender1@localhost";
    String recipient1 = "mail_recipient1@localhost";
    smtpProtocol1.setSender(sender1);
    smtpProtocol1.addRecipient(recipient1);

    String sender2 = "mail_sender2@localhost";
    String recipient2 = "mail_recipient2@localhost";
    smtpProtocol2.setSender(sender2);
    smtpProtocol2.addRecipient(recipient2);

    smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoSimultaneousMails1\r\n.\r\n");
    verifyLastMail(sender1, recipient1, null);

    smtpProtocol2.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoSimultaneousMails2\r\n.\r\n");
    verifyLastMail(sender2, recipient2, null);

    smtpProtocol1.quit();
    smtpProtocol2.quit();

    smtpProtocol1.disconnect();
    smtpProtocol2.disconnect();
}

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

@Test
public void testTwoMailsInSequence() throws Exception {
    init(smtpConfiguration);//from   ww w  .j  a  va 2s.  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());

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

    String sender1 = "mail_sender1@localhost";
    String recipient1 = "mail_recipient1@localhost";
    smtpProtocol1.setSender(sender1);
    smtpProtocol1.addRecipient(recipient1);

    smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoMailsInSequence1\r\n");
    verifyLastMail(sender1, recipient1, null);

    String sender2 = "mail_sender2@localhost";
    String recipient2 = "mail_recipient2@localhost";
    smtpProtocol1.setSender(sender2);
    smtpProtocol1.addRecipient(recipient2);

    smtpProtocol1.sendShortMessageData("Subject: test2\r\n\r\nTest body2 testTwoMailsInSequence2\r\n");
    verifyLastMail(sender2, recipient2, null);

    smtpProtocol1.quit();
    smtpProtocol1.disconnect();
}

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

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

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

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

@Test
public void testNoRecepientSpecified() throws Exception {
    init(smtpConfiguration);//from w  ww .j ava  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());
}

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);/*  w ww. j  av  a  2  s .  c  o m*/
    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);/*ww w .j  a va 2s.  c  o  m*/
    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());
}