Examples of MailAddress


Examples of org.apache.mailet.MailAddress

        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

Examples of org.apache.mailet.MailAddress

            //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

Examples of org.apache.mailet.MailAddress

        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

Examples of org.apache.mailet.MailAddress

            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

Examples of org.apache.mailet.MailAddress

        assertNotNull("Should match all recipients", inverter.match(mail));
    }
   
    public void testNonMatch() throws MessagingException {
        final MailAddress address1 = new MailAddress("user", "domain");
        final MailAddress address2 = new MailAddress("user", "domain2");
       
        MatcherInverter inverter = new MatcherInverter(new GenericMatcher() {

            public Collection match(Mail mail) throws MessagingException {
                return mail.getRecipients();
View Full Code Here

Examples of org.apache.mailet.MailAddress

        assertNull("Should match all recipients", inverter.match(mail));
    }
   
    public void testOneMatch() throws MessagingException {
        final MailAddress address1 = new MailAddress("user", "domain");
        final MailAddress address2 = new MailAddress("user", "domain2");
       
        MatcherInverter inverter = new MatcherInverter(new GenericMatcher() {

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

        assertEquals("Should match one recipient", address2.toString(), inverter.match(mail).iterator().next().toString());
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.