public void sendMessage()

in geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/transport/nntp/NNTPTransport.java [139:245]


    public void sendMessage(Message message, Address[] addresses) throws MessagingException {
        if (!isConnected()) {
            throw new IllegalStateException("Not connected");
        }

        if (!connection.isPostingAllowed()) {
            throw new MessagingException("Posting disabled for host server");
        }
        // don't bother me w/ null messages or no addreses
        if (message == null) {
            throw new MessagingException("Null message");
        }

        // NNTP only handles instances of MimeMessage, not the more general
        // message case.
        if (!(message instanceof MimeMessage)) {
            throw new MessagingException("NNTP can only send MimeMessages");
        }

        // need to sort the from value out from a variety of sources.
        InternetAddress from = null;

        Address[] fromAddresses = message.getFrom();

        // If the message has a From address set, we just use that. Otherwise,
        // we set a From using
        // the property version, if available.
        if (fromAddresses == null || fromAddresses.length == 0) {
            // the from value can be set explicitly as a property
            String defaultFrom = props.getProperty(NNTP_FROM);
            if (defaultFrom == null) {
                message.setFrom(new InternetAddress(defaultFrom));
            }
        }

        // we must have a message list.
        if (addresses == null || addresses.length == 0) {
            throw new MessagingException("Null or empty address array");
        }

        boolean haveGroup = false;

        // enforce the requirement that all of the targets are NewsAddress
        // instances.
        for (int i = 0; i < addresses.length; i++) {
            if (!(addresses[i] instanceof NewsAddress)) {
                throw new MessagingException("Illegal NewsAddress " + addresses[i]);
            }
        }

        // event notifcation requires we send lists of successes and failures
        // broken down by category.
        // The categories are:
        //
        // 1) addresses successfully processed.
        // 2) addresses deemed valid, but had a processing failure that
        // prevented sending.
        // 3) addressed deemed invalid (basically all other processing
        // failures).
        ArrayList sentAddresses = new ArrayList();
        ArrayList unsentAddresses = new ArrayList();
        ArrayList invalidAddresses = new ArrayList();

        boolean sendFailure = false;

        // now try to post this message to the different news groups.
        for (int i = 0; i < addresses.length; i++) {
            try {
                // select the target news group
                NNTPReply reply = connection.selectGroup(((NewsAddress) addresses[i]).getNewsgroup());

                if (reply.getCode() != NNTPReply.GROUP_SELECTED) {
                    invalidAddresses.add(addresses[i]);
                    sendFailure = true;
                } else {
                    // send data
                    connection.sendPost(message);
                    sentAddresses.add(addresses[i]);
                }
            } catch (MessagingException e) {
                unsentAddresses.add(addresses[i]);
                sendFailure = true;
            }
        }

        // create our lists for notification and exception reporting from this
        // point on.
        Address[] sent = (Address[]) sentAddresses.toArray(new Address[0]);
        Address[] unsent = (Address[]) unsentAddresses.toArray(new Address[0]);
        Address[] invalid = (Address[]) invalidAddresses.toArray(new Address[0]);

        if (sendFailure) {
            // did we deliver anything at all?
            if (sent.length == 0) {
                // notify of the error.
                notifyTransportListeners(TransportEvent.MESSAGE_NOT_DELIVERED, sent, unsent, invalid, message);
            } else {
                // notify that we delivered at least part of this
                notifyTransportListeners(TransportEvent.MESSAGE_PARTIALLY_DELIVERED, sent, unsent, invalid, message);
            }

            throw new MessagingException("Error posting NNTP message");
        }

        // notify our listeners of successful delivery.
        notifyTransportListeners(TransportEvent.MESSAGE_DELIVERED, sent, unsent, invalid, message);
    }