public Response starttls(String server) {
        greet(server);
        if (!hasExtension("starttls"))
            throw new SMTPException("STARTTLS not supported by server.");
        Response response = doCommand("STARTTLS");
        if (response.code != 220)
            return response;
        try {
            socket = ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(
                    socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
            ((SSLSocket) socket).setUseClientMode(true);
            ((SSLSocket) socket).startHandshake();
        } catch (IOException e) {
            throw new SMTPException(e);
        }
        read = null;
        write = null;
        heloResponse = null;
        ehloResponse = null;
        esmtpFeatures = null;
        esmtp = false;
        return response;
    }