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

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

Introduction

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

Prototype

public SMTPClient() 

Source Link

Document

Default SMTPClient constructor.

Usage

From source file:org.apache.commons.net.examples.mail.SMTPMail.java

public static void main(String[] args) {
    String sender, recipient, subject, fileName, server, cc;
    List<String> ccList = new ArrayList<String>();
    BufferedReader stdin;//from  w  ww .j  a v  a2 s .co m
    FileReader fileReader = null;
    Writer writer;
    SimpleSMTPHeader header;
    SMTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: SMTPMail <smtpserver>");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        sender = stdin.readLine();

        System.out.print("To: ");
        System.out.flush();

        recipient = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleSMTPHeader(sender, recipient, subject);

        while (true) {
            System.out.print("CC <enter one address per line, hit enter to end>: ");
            System.out.flush();

            cc = stdin.readLine();

            if (cc == null || cc.length() == 0) {
                break;
            }

            header.addCC(cc.trim());
            ccList.add(cc.trim());
        }

        System.out.print("Filename: ");
        System.out.flush();

        fileName = stdin.readLine();

        try {
            fileReader = new FileReader(fileName);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
        }

        client = new SMTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));

        client.connect(server);

        if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("SMTP server refused connection.");
            System.exit(1);
        }

        client.login();

        client.setSender(sender);
        client.addRecipient(recipient);

        for (String recpt : ccList) {
            client.addRecipient(recpt);
        }

        writer = client.sendMessageData();

        if (writer != null) {
            writer.write(header.toString());
            Util.copyReader(fileReader, writer);
            writer.close();
            client.completePendingCommand();
        }

        if (fileReader != null) {
            fileReader.close();
        }

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:org.apache.james.jmap.VacationRelayIntegrationTest.java

@Test
public void forwardingAnEmailShouldWork() throws Exception {
    jmapGuiceProbe.modifyVacation(AccountId.fromString(USER_WITH_DOMAIN),
            VacationPatch.builder().isEnabled(true).textBody(REASON).build());

    String externalMail = "ray@yopmail.com";

    SMTPClient smtpClient = new SMTPClient();
    smtpClient.connect(LOCALHOST_IP, SMTP_PORT);
    smtpClient.helo(DOMAIN);//w  w  w.  j a  va2s. com
    smtpClient.setSender(externalMail);
    smtpClient.rcpt("<" + USER_WITH_DOMAIN + ">");
    smtpClient.sendShortMessageData("content");

    calmlyAwait.atMost(1, TimeUnit.MINUTES).until(() -> {
        try {
            when().get("/api/email").then().statusCode(200).body("[0].from", equalTo(USER_WITH_DOMAIN))
                    .body("[0].to[0]", equalTo(externalMail)).body("[0].text", equalTo(REASON));

            return true;
        } catch (AssertionError e) {
            return false;
        }
    });
}

From source file:org.apache.james.mailets.utils.SMTPMessageSender.java

public static SMTPMessageSender noAuthentication(String ip, int port, String senderDomain) throws IOException {
    SMTPClient smtpClient = new SMTPClient();
    smtpClient.connect(ip, port);/*from  w  ww .  j  av a2 s .  co m*/
    return new SMTPMessageSender(smtpClient, senderDomain);
}

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

protected SMTPClient createClient() {
    return new SMTPClient();
}

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

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

    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);/*  ww w .j a  v a 2  s . 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);//from   ww  w . j a  va 2 s.co 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.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   w w  w. j a  v 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();

}

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

protected SMTPClient newSMTPClient() throws IOException {
    SMTPClient smtp = new SMTPClient();
    smtp.connect("127.0.0.1", smtpListenerPort);
    if (log.isDebugEnabled()) {
        smtp.addProtocolCommandListener(new ProtocolCommandListener() {

            @Override//w  w  w.  j  a  v a2 s.co m
            public void protocolCommandSent(ProtocolCommandEvent event) {
                log.debug("> " + event.getMessage().trim());
            }

            @Override
            public void protocolReplyReceived(ProtocolCommandEvent event) {
                log.debug("< " + event.getMessage().trim());
            }
        });
    }
    return smtp;
}

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

@Test
public void testSimpleMailSendWithHELO() throws Exception {
    init(smtpConfiguration);/*from ww  w. ja v a2 s .c  om*/

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