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

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

Introduction

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

Prototype

public Writer sendMessageData() throws IOException 

Source Link

Document

Send the SMTP DATA command in preparation to send an email message.

Usage

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

public void testHandleMessageSizeLimitExceeded() throws Exception {
    smtpConfiguration.setMaxMessageSize(1); // set message limit to 1kb
    init(smtpConfiguration);//from  w  w  w .jav  a2s .c  om

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

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

    smtpProtocol.setSender("mail@localhost");
    smtpProtocol.addRecipient("mail@localhost");

    Writer wr = smtpProtocol.sendMessageData();
    // create Body with more than 1kb . 502
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100\r\n");
    // second line
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write("123456781012345678201\r\n"); // 521 + CRLF = 523 + 502 => 1025
    wr.close();

    assertFalse(smtpProtocol.completePendingCommand());

    assertEquals("expected 552 error", 552, smtpProtocol.getReplyCode());

}

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

@Test
public void testHandleMessageSizeLimitRespected() throws Exception {
    smtpConfiguration.setMaxMessageSize(1); // set message limit to 1kb
    init(smtpConfiguration);/*from w  ww .  j  av  a2s . co  m*/

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

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

    smtpProtocol.setSender("mail@localhost");
    smtpProtocol.addRecipient("mail@localhost");

    Writer wr = smtpProtocol.sendMessageData();
    // create Body with less than 1kb
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write(
            "1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
    wr.write("1234567810123456782012\r\n"); // 1022 + CRLF = 1024
    wr.close();

    assertTrue(smtpProtocol.completePendingCommand());

    assertEquals("expected 250 ok", 250, smtpProtocol.getReplyCode());

}