Package org.apache.mailet

Examples of org.apache.mailet.MailAddress


            MimeMessage message = new MimeMessage(mail.getMessage());
            //We need to remove this header from the copy we're sending around
            message.removeHeader(RFC2822Headers.RETURN_PATH);

            //Figure out the listserv address.
            MailAddress listservAddr = getListservAddress();
            if (listservAddr == null) {
                //Use the recipient
                listservAddr = (MailAddress)mail.getRecipients().iterator().next();
            }

            //Check if the X-been-there header is set to the listserv's name
            //  (the address).  If it has, this means it's a message from this
            //  listserv that's getting bounced back, so we need to swallow it
            if (listservAddr.equals(message.getHeader("X-been-there"))) {
                mail.setState(Mail.GHOST);
                return;
            }

            //Set the subject if set
            String prefix = getSubjectPrefix();
            if (prefix != null) {
                if (isPrefixAutoBracketed()) {
                    StringBuffer prefixBuffer =
                        new StringBuffer(64)
                            .append("[")
                            .append(prefix)
                            .append("]");
                    prefix = prefixBuffer.toString();
                }
                String subj = message.getSubject();
                if (subj == null) {
                    subj = "";
                }
                subj = normalizeSubject(subj, prefix);
                message.setSubject(subj);
            }

            //If replies should go to this list, we need to set the header
            if (isReplyToList()) {
                message.setHeader(RFC2822Headers.REPLY_TO, listservAddr.toString());
            }
            //We're going to set this special header to avoid bounces
            //  getting sent back out to the list
            message.setHeader("X-been-there", listservAddr.toString());

            //Send the message to the list members
            //We set the postmaster as the sender for now so bounces go to him/her
            getMailetContext().sendMail(getMailetContext().getPostmaster(), members, message);
View Full Code Here


                rs = stmt.executeQuery();
                Collection tmpMembers = new Vector();
                while (rs.next()) {
                    String address = rs.getString(1);
                    try {
                        MailAddress mailAddress = new MailAddress(address);
                        tmpMembers.add(mailAddress);
                    } catch (ParseException pe) {
                        //don't stop... just log and continue
                        StringBuffer exceptionBuffer =
                            new StringBuffer(64)
                                    .append("error parsing address '")
                                    .append(address)
                                    .append("' in listserv '")
                                    .append(listservID)
                                    .append("'");
                        log(exceptionBuffer.toString());
                    }
                }
                members = tmpMembers;
            } finally {
                ResultSet localRS = rs;
                // Clear reference to result set
                rs = null;
                theJDBCUtil.closeJDBCResultSet(localRS);
                Statement localStmt = stmt;
                // Clear reference to statement
                stmt = null;
                theJDBCUtil.closeJDBCStatement(localStmt);
            }

            stmt = conn.prepareStatement(listservQuery);
            stmt.setString(1, listservID);
            rs = stmt.executeQuery();
            if (!rs.next()) {
                StringBuffer exceptionBuffer =
                    new StringBuffer(64)
                            .append("Could not find listserv record for '")
                            .append(listservID)
                            .append("'");
                throw new MailetException(exceptionBuffer.toString());
            }
            membersOnly = rs.getBoolean("members_only");
            attachmentsAllowed = rs.getBoolean("attachments_allowed");
            replyToList = rs.getBoolean("reply_to_list");
            subjectPrefix = rs.getString("subject_prefix");
            String address = rs.getString("list_address");
            if (address == null) {
                listservAddress = null;
            } else {
                try {
                    listservAddress = new MailAddress(address);
                } catch (ParseException pe) {
                    //log and ignore
                    StringBuffer logBuffer =
                        new StringBuffer(128)
                                .append("invalid listserv address '")
View Full Code Here

                    addressString = st.nextToken();
                    iAddress = new InternetAddress(addressString);
                    if (isDebug) {
                        log("Address = " + iAddress.toString());
                    }
                    addresses.add(new MailAddress(iAddress));
                }
            }
        }
        return addresses;
    }
View Full Code Here

                            .append(": did not start and end with < >");
                    getLogger().error(errorBuffer.toString());
                }
                return;
            }
            MailAddress senderAddress = null;
            //Remove < and >
            sender = sender.substring(1, sender.length() - 1);
            if (sender.length() == 0) {
                //This is the <> case.  Let senderAddress == null
            } else {
                if (sender.indexOf("@") < 0) {
                    sender = sender + "@localhost";
                }
                try {
                    senderAddress = new MailAddress(sender);
                } catch (Exception pe) {
                    responseString = "501 Syntax error in sender address";
                    writeLoggedFlushedResponse(responseString);
                    if (getLogger().isErrorEnabled()) {
                        StringBuffer errorBuffer =
View Full Code Here

                                .append(": did not start and end with < >");
                    getLogger().error(errorBuffer.toString());
                }
                return;
            }
            MailAddress recipientAddress = null;
            //Remove < and >
            recipient = recipient.substring(1, recipient.length() - 1);
            if (recipient.indexOf("@") < 0) {
                recipient = recipient + "@localhost";
            }
            try {
                recipientAddress = new MailAddress(recipient);
            } catch (Exception pe) {
                responseString = "501 Syntax error in recipient address";
                writeLoggedFlushedResponse(responseString);

                if (getLogger().isErrorEnabled()) {
                    StringBuffer errorBuffer =
                        new StringBuffer(192)
                                .append("Error parsing recipient address: ")
                                .append(recipient)
                                .append(": ")
                                .append(pe.getMessage());
                    getLogger().error(errorBuffer.toString());
                }
                return;
            }
            if (theConfigData.isAuthRequired()) {
                // Make sure the mail is being sent locally if not
                // authenticated else reject.
                if (getUser() == null) {
                    String toDomain = recipientAddress.getHost();
                    if (!theConfigData.getMailServer().isLocalServer(toDomain)) {
                        responseString = "530 Authentication Required";
                        writeLoggedFlushedResponse(responseString);
                        getLogger().error("Rejected message - authentication is required for mail request");
                        return;
                    }
                } else {
                    // Identity verification checking
                    if (theConfigData.isVerifyIdentity()) {
                        String authUser = (getUser()).toLowerCase(Locale.US);
                        MailAddress senderAddress = (MailAddress) state.get(SENDER);
                        boolean domainExists = false;

                        if ((!authUser.equals(senderAddress.getUser())) ||
                            (!theConfigData.getMailServer().isLocalServer(senderAddress.getHost()))) {
                            responseString = "503 Incorrect Authentication for Specified Email Address";
                            writeLoggedFlushedResponse(responseString);
                            if (getLogger().isErrorEnabled()) {
                                StringBuffer errorBuffer =
                                    new StringBuffer(128)
View Full Code Here

            mc.setErrorMessage(rsMessage.getString(2));
            String sender = rsMessage.getString(3);
            if (sender == null) {
                mc.setSender(null);
            } else {
                mc.setSender(new MailAddress(sender));
            }
            StringTokenizer st = new StringTokenizer(rsMessage.getString(4), "\r\n", false);
            Set recipients = new HashSet();
            while (st.hasMoreTokens()) {
                recipients.add(new MailAddress(st.nextToken()));
            }
            mc.setRecipients(recipients);
            mc.setRemoteHost(rsMessage.getString(5));
            mc.setRemoteAddr(rsMessage.getString(6));
            mc.setLastUpdated(rsMessage.getTimestamp(7));
View Full Code Here

        response.setText(textBuffer.toString());

        Set recipients = new HashSet();
        Address addresses[] = response.getAllRecipients();
        for (int i = 0; i < addresses.length; i++) {
            recipients.add(new MailAddress((InternetAddress)addresses[0]));
        }

        MailAddress sender = new MailAddress((InternetAddress)response.getFrom()[0]);
        getMailetContext().sendMail(sender, recipients, response);
    }
View Full Code Here

            //Create an array of the recipients as InternetAddress objects
            Collection recipients = mail.getRecipients();
            InternetAddress addr[] = new InternetAddress[recipients.size()];
            int j = 0;
            for (Iterator i = recipients.iterator(); i.hasNext(); j++) {
                MailAddress rcpt = (MailAddress)i.next();
                addr[j] = rcpt.toInternetAddress();
            }

            //Figure out which servers to try to send to.  This collection
            //  will hold all the possible target servers
            Collection targetServers = null;
            if (gatewayServer == null) {
                MailAddress rcpt = (MailAddress) recipients.iterator().next();
                String host = rcpt.getHost();

                //Lookup the possible targets
                targetServers = getMailetContext().getMailServers(host);
                if (targetServers.size() == 0) {
                    log("No mail server found for: " + host);
                    StringBuffer exceptionBuffer =
                        new StringBuffer(128)
                            .append("There are no DNS entries for the hostname ")
                            .append(host)
                            .append(".  I cannot determine where to send this message.");
                    return failMessage(mail, new MessagingException(exceptionBuffer.toString()), false);
                }
            } else {
                targetServers = new Vector();
                targetServers.add(gatewayServer);
            }

            MessagingException lastError = null;

            if (addr.length > 0) {
                Iterator i = targetServers.iterator();
                while ( i.hasNext()) {
                    try {
                        String outgoingMailServer = i.next().toString ();
                        StringBuffer logMessageBuffer =
                            new StringBuffer(256)
                                    .append("Attempting delivery of ")
                                    .append(mail.getName())
                                    .append(" to host ")
                                    .append(outgoingMailServer)
                                    .append(" to addresses ")
                                    .append(Arrays.asList(addr));
                        log(logMessageBuffer.toString());
                        URLName urlname = new URLName("smtp://" + outgoingMailServer);

                        Properties props = session.getProperties();
                        //This was an older version of JavaMail
                        if (mail.getSender() == null) {
                            props.put("mail.smtp.user", "<>");
                            props.put("mail.smtp.from", "<>");
                        } else {
                            String sender = mail.getSender().toString();
                            props.put("mail.smtp.user", sender);
                            props.put("mail.smtp.from", sender);
                        }

                        //Many of these properties are only in later JavaMail versions
                        //"mail.smtp.ehlo"  //default true
                        //"mail.smtp.auth"  //default false
                        //"mail.smtp.dsn.ret"  //default to nothing... appended as RET= after MAIL FROM line.
                        //"mail.smtp.dsn.notify" //default to nothing...appended as NOTIFY= after RCPT TO line.

                        Transport transport = null;
                        try {
                            transport = session.getTransport(urlname);
                            try {
                                transport.connect();
                            } catch (MessagingException me) {
                                // Any error on connect should cause the mailet to attempt
                                // to connect to the next SMTP server associated with this MX record,
                                // assuming the number of retries hasn't been exceeded.
                                if (failMessage(mail, me, false)) {
                                    return true;
                                } else {
                                    continue;
                                }
                            }
                            transport.sendMessage(message, addr);
                        } finally {
                            if (transport != null) {
                                transport.close();
                                transport = null;
                            }
                        }
                        logMessageBuffer =
                            new StringBuffer(256)
                                    .append("Mail (")
                                    .append(mail.getName())
                                    .append(") sent successfully to ")
                                    .append(outgoingMailServer);
                        log(logMessageBuffer.toString());
                        return true;
                    } catch (MessagingException me) {
                        //MessagingException are horribly difficult to figure out what actually happened.
                        StringBuffer exceptionBuffer =
                            new StringBuffer(256)
                                    .append("Exception delivering message (")
                                    .append(mail.getName())
                                    .append(") - ")
                                    .append(me.getMessage());
                        log(exceptionBuffer.toString());
                        //Assume it is a permanent exception, or prove ourselves otherwise
                        boolean permanent = true;
                        if ((me.getNextException() != null) &&
                            (me.getNextException() instanceof java.io.IOException)) {
                            //This is more than likely a temporary failure

                            // If it's an IO exception with no nested exception, it's probably
                            // some socket or weird I/O related problem.
                            lastError = me;
                            continue;
                        }
                        // This was not a connection or I/O error particular to one
                        // SMTP server of an MX set.  Instead, it is almost certainly
                        // a protocol level error.  In this case we assume that this
                        // is an error we'd encounter with any of the SMTP servers
                        // associated with this MX record, and we pass the exception
                        // to the code in the outer block that determines its severity.
                        throw me;
                    }
                } // end while
                //If we encountered an exception while looping through, send the last exception we got
                if (lastError != null) {
                    throw lastError;
                }
            } else {
                log("No recipients specified... not sure how this could have happened.");
            }
        } catch (SendFailedException sfe) {
            //Would like to log all the types of email addresses
            if (sfe.getValidSentAddresses() != null) {
                Address[] validSent = sfe.getValidSentAddresses();
                Collection recipients = mail.getRecipients();
                //Remove these addresses for the recipients
                for (int i = 0; i < validSent.length; i++) {
                    try {
                        MailAddress addr = new MailAddress(validSent[i].toString());
                        recipients.remove(addr);
                    } catch (ParseException pe) {
                        //ignore once debugging done
                        pe.printStackTrace();
                    }
View Full Code Here

        if (gatewayServer == null) {
            // Must first organize the recipients into distinct servers (name made case insensitive)
            Hashtable targets = new Hashtable();
            for (Iterator i = recipients.iterator(); i.hasNext();) {
                MailAddress target = (MailAddress)i.next();
                String targetServer = target.getHost().toLowerCase(Locale.US);
                Collection temp = (Collection)targets.get(targetServer);
                if (temp == null) {
                    temp = new Vector();
                    targets.put(targetServer, temp);
                }
View Full Code Here

            public Collection match(Mail mail) throws MessagingException {
                return null;
            }
        });
        FakeMail mail = new FakeMail();
        mail.setRecipients(Arrays.asList(new MailAddress[] { new MailAddress("user", "domain") }));

        assertNotNull("Should match all recipients", inverter.match(mail));
    }
View Full Code Here

TOP

Related Classes of org.apache.mailet.MailAddress

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.