Example usage for javax.mail.internet MimeMessage getMatchingHeaders

List of usage examples for javax.mail.internet MimeMessage getMatchingHeaders

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage getMatchingHeaders.

Prototype

@Override
public Enumeration<Header> getMatchingHeaders(String[] names) throws MessagingException 

Source Link

Document

Return matching headers from this Message as an Enumeration of Header objects.

Usage

From source file:org.apache.jsieve.mailet.SieveMailboxMailet.java

/**
 * Delivers a mail to a local mailbox./*from w ww. ja  va2  s.c o m*/
 * 
 * @param mail
 *            the mail being processed
 * 
 * @throws MessagingException
 *             if an error occurs while storing the mail
 */
@SuppressWarnings("unchecked")
@Override
public void service(Mail mail) throws MessagingException {
    Collection<MailAddress> recipients = mail.getRecipients();
    Collection<MailAddress> errors = new Vector<MailAddress>();

    MimeMessage message = null;
    if (deliveryHeader != null || resetReturnPath) {
        message = mail.getMessage();
    }

    if (resetReturnPath) {
        // Set Return-Path and remove all other Return-Path headers from the
        // message
        // This only works because there is a placeholder inserted by
        // MimeMessageWrapper
        message.setHeader(RFC2822Headers.RETURN_PATH,
                (mail.getSender() == null ? "<>" : "<" + mail.getSender() + ">"));
    }

    Enumeration headers;
    InternetHeaders deliveredTo = new InternetHeaders();
    if (deliveryHeader != null) {
        // Copy any Delivered-To headers from the message
        headers = message.getMatchingHeaders(new String[] { deliveryHeader });
        while (headers.hasMoreElements()) {
            Header header = (Header) headers.nextElement();
            deliveredTo.addHeader(header.getName(), header.getValue());
        }
    }

    for (Iterator<MailAddress> i = recipients.iterator(); i.hasNext();) {
        MailAddress recipient = i.next();
        try {
            if (deliveryHeader != null) {
                // Add qmail's de facto standard Delivered-To header
                message.addHeader(deliveryHeader, recipient.toString());
            }

            storeMail(mail.getSender(), recipient, mail);

            if (deliveryHeader != null) {
                if (i.hasNext()) {
                    // Remove headers but leave all placeholders
                    message.removeHeader(deliveryHeader);
                    headers = deliveredTo.getAllHeaders();
                    // And restore any original Delivered-To headers
                    while (headers.hasMoreElements()) {
                        Header header = (Header) headers.nextElement();
                        message.addHeader(header.getName(), header.getValue());
                    }
                }
            }
        } catch (Exception ex) {
            log("Error while storing mail.", ex);
            errors.add(recipient);
        }
    }

    if (!errors.isEmpty()) {
        // If there were errors, we redirect the email to the ERROR
        // processor.
        // In order for this server to meet the requirements of the SMTP
        // specification, mails on the ERROR processor must be returned to
        // the sender. Note that this email doesn't include any details
        // regarding the details of the failure(s).
        // In the future we may wish to address this.
        getMailetContext().sendMail(mail.getSender(), errors, mail.getMessage(), Mail.ERROR);
    }
    if (consume) {
        // Consume this message
        mail.setState(Mail.GHOST);
    }
}