Example usage for org.apache.commons.net.smtp SMTPSClient execTLS

List of usage examples for org.apache.commons.net.smtp SMTPSClient execTLS

Introduction

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

Prototype

public boolean execTLS() throws SSLException, IOException 

Source Link

Document

The TLS command execution.

Usage

From source file:me.schiz.jmeter.protocol.smtp.sampler.SMTPSampler.java

private SampleResult sampleCommand(SampleResult sr) {
    SocketClient soclient = SessionStorage.getInstance().getClient(getSOClient());
    SMTPClient client = null;//from  ww  w .j  a  v  a  2s  .c  om
    int responseCode = 0;
    if (soclient instanceof SMTPClient)
        client = (SMTPClient) soclient;

    String request = "COMMAND\n";
    request += "Client : " + getClient() + "\n";
    request += "Command : " + getCommand() + "\n";
    sr.setRequestHeaders(request);
    if (client == null) {
        sr.setResponseCode("404");
        sr.setResponseData(("Client `" + getClient() + "` not found").getBytes());
        sr.setSuccessful(false);
        return sr;
    } else {
        synchronized (client) {
            sr.sampleStart();
            try {
                responseCode = client.sendCommand(getCommand());
                sr.setResponseCode(String.valueOf(responseCode));
                sr.setSuccessful(SMTPReply.isPositiveIntermediate(responseCode));
                String response = client.getReplyString();
                setSuccessfulByResponseCode(sr, client.getReplyCode());

                if (SessionStorage.getInstance()
                        .getClientType(getSOClient()) == SessionStorage.proto_type.STARTTLS) {
                    String command;
                    if (getCommand().indexOf(' ') != -1)
                        command = getCommand().substring(0, getCommand().indexOf(' '));
                    else
                        command = getCommand();
                    if ((command.equalsIgnoreCase("lhlo") || command.equalsIgnoreCase("ehlo")
                            || command.equalsIgnoreCase("helo")) && getUseSTARTTLS()
                            && client instanceof SMTPSClient) {
                        SMTPSClient sclient = (SMTPSClient) client;
                        if (sclient.execTLS() == false) {
                            sr.setSuccessful(false);
                            sr.setResponseCode("403");
                            ;
                            response += sclient.getReplyString();
                            log.error("client `" + client + "` STARTTLS failed");
                            removeClient();
                        } else {
                            response += "\nSTARTTLS OK";
                        }
                    }
                }
                sr.setResponseData(response.getBytes());
            } catch (IOException e) {
                sr.setSuccessful(false);
                sr.setResponseData(e.toString().getBytes());
                sr.setResponseCode(e.getClass().getName());
                log.error("client `" + client + "` ", e);
                removeClient();
            }
            sr.sampleEnd();
        }
    }
    return sr;
}

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

@Test
public void testStartTLS() throws Exception {
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;//www  .  j a v  a  2s.c om
    try {
        server = createServer(createProtocol(new ProtocolHandler[0]), address,
                Encryption.createStartTls(BogusSslContextFactory.getServerContext()));
        server.bind();

        SMTPSClient client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.sendCommand("EHLO localhost");
        assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode()));

        boolean startTLSAnnounced = false;
        for (String reply : client.getReplyStrings()) {
            if (reply.toUpperCase(Locale.UK).endsWith("STARTTLS")) {
                startTLSAnnounced = true;
                break;
            }
        }
        assertTrue(startTLSAnnounced);

        assertTrue(client.execTLS());

        // TODO: Add back once commons-net 3.1.0 was released.
        // See: NET-421
        //
        //client.quit();
        //assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode()));

        client.disconnect();

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:repl.simple.mathematica.ErrorReporter.java

/**
 * Sends the information to mail server.
 *
 * @param model of bug report//  w  ww .  ja  v a2s.c o  m
 */
private static synchronized void sendBugData(BugReportModel model) {
    String subject;
    Writer writer;
    SimpleSMTPHeader header;
    SMTPSClient client;
    //
    subject = MathREPLBundle.message("reportSubject");
    try {
        header = new SimpleSMTPHeader(model.mailuser, model.to, subject);

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

        client.connect(model.mailserver);
        if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            throw new ConnectException(MathREPLBundle.message("smtpFailure"));
        }
        client.helo("localhost");
        if (client.execTLS()) {

            client.login();
            client.setSender(model.mailuser);
            client.addRecipient(model.to);

            writer = client.sendMessageData();

            if (writer != null) {
                writer.write(header.toString());
                writer.write(model.message);
                writer.close();
                client.completePendingCommand();
            }
        }
        client.logout();
        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
}