Example usage for javax.mail.internet MimePart getHeader

List of usage examples for javax.mail.internet MimePart getHeader

Introduction

In this page you can find the example usage for javax.mail.internet MimePart getHeader.

Prototype

public String[] getHeader(String header_name) throws MessagingException;

Source Link

Document

Get all the headers for this header name.

Usage

From source file:com.zimbra.cs.mime.Mime.java

/**
 * Returns {@code true} if the {@code Auto-Submitted} header is set
 * to a value other than {@code no}.//from  ww w . j a va 2s . com
 */
public static boolean isAutoSubmitted(MimePart part) throws MessagingException {
    String[] autoSubmitted = part.getHeader("Auto-Submitted");
    if (autoSubmitted != null) {
        for (int i = 0; i < autoSubmitted.length; i++) {
            if (!autoSubmitted[i].equalsIgnoreCase("no")) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.zimbra.cs.mime.Mime.java

/**
 * Returns the decoded and unfolded values for the given header name,
 * or an empty array if no headers with the given name exist.
 *///w ww.ja v a  2s. co  m
public static String[] getHeaders(MimePart part, String headerName) {
    try {
        String[] values = part.getHeader(headerName);
        if (values == null || values.length == 0)
            return NO_HEADERS;

        for (int i = 0; i < values.length; i++) {
            try {
                values[i] = MimeUtility.decodeText(values[i]);
            } catch (UnsupportedEncodingException e) {
                // values[i] would contain the undecoded value, fine
            }
            values[i] = MimeUtility.unfold(values[i]);
        }

        return values;
    } catch (MessagingException e) {
        sLog.debug("Unable to get headers named '%s'", headerName, e);
        return NO_HEADERS;
    }
}

From source file:com.icegreen.greenmail.store.SimpleMessageAttributes.java

/**
 * Parses key data items from a MimeMessage for seperate storage.
 * TODO this is a mess, and should be completely revamped.
 *//*from   ww w .  j  ava2s.  c om*/
void parseMimePart(MimePart part) throws MessagingException {
    size = GreenMailUtil.getBody(part).length();

    // Section 1 - Message Headers
    if (part instanceof MimeMessage) {
        try {
            subject = ((MimeMessage) part).getSubject();
        } catch (MessagingException me) {
            //                if (DEBUG) getLogger().debug("Messaging Exception for getSubject: " + me);
        }
    }
    try {
        from = part.getHeader("From");
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getHeader(From): " + me);
    }
    try {
        sender = part.getHeader("Sender");
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getHeader(Sender): " + me);
    }
    try {
        replyTo = part.getHeader("Reply To");
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getHeader(Reply To): " + me);
    }
    try {
        to = part.getHeader("To");
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getHeader(To): " + me);
    }
    try {
        cc = part.getHeader("Cc");
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getHeader(To): " + me);
    }
    try {
        bcc = part.getHeader("Bcc");
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getHeader(To): " + me);
    }
    try {
        inReplyTo = part.getHeader("In Reply To");
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getHeader(In Reply To): " + me);
    }
    try {
        date = part.getHeader("Date");
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getHeader(Date): " + me);
    }
    try {
        messageID = part.getHeader("Message-ID");
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getHeader(messageID): " + me);
    }
    String contentTypeLine = null;
    try {
        contentTypeLine = part.getContentType();
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getContentType(): " + me);
    }
    if (contentTypeLine != null) {
        decodeContentType(contentTypeLine);
    }
    try {
        contentID = part.getContentID();
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getContentUD(): " + me);
    }
    try {
        contentDesc = part.getDescription();
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getDescription(): " + me);
    }
    try {
        contentEncoding = part.getEncoding();
        // default value.
        if (contentEncoding == null) {
            contentEncoding = "7BIT";
        }
    } catch (MessagingException me) {
        //            if (DEBUG) getLogger().debug("Messaging Exception for getEncoding(): " + me);
    }

    try {
        //            contentDisposition = part.getDisposition();
        contentDisposition = Header.create(part.getHeader("Content-Disposition"));
    } catch (MessagingException me) {
        if (log.isDebugEnabled()) {
            log.debug("Can not create content disposition for part " + part, me);
        }
    }

    try {
        // TODO this doesn't work
        lineCount = getLineCount(part);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("Can not get line count for part " + part, e);
        }
    }

    // Recurse through any embedded parts
    if (primaryType.equalsIgnoreCase(MULTIPART)) {
        MimeMultipart container;
        try {
            container = (MimeMultipart) part.getContent();
            int count = container.getCount();
            parts = new SimpleMessageAttributes[count];
            for (int i = 0; i < count; i++) {
                BodyPart nextPart = container.getBodyPart(i);

                if (nextPart instanceof MimePart) {
                    SimpleMessageAttributes partAttrs = new SimpleMessageAttributes(null, receivedDate);
                    partAttrs.parseMimePart((MimePart) nextPart);
                    parts[i] = partAttrs;

                }
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Can not recurse through multipart content", e);
            }
        }
    } else if (primaryType.equalsIgnoreCase("message")) {
        if (secondaryType.equalsIgnoreCase("RFC822")) {
            //try {

            /*
            MimeMessageWrapper message = new MimeMessageWrapper(part.getInputStream());
            SimpleMessageAttributes msgAttrs = new SimpleMessageAttributes();
            msgAttrs.setAttributesFor(message);
                    
            if (part instanceof MimeMessage) {
            Comments out because I don't know what it should do here
            MimeMessage msg1 = (MimeMessage) part;
            MimeMessageWrapper message2 = new MimeMessageWrapper(msg1);
            SimpleMessageAttributes msgAttrs2 = new SimpleMessageAttributes();
            msgAttrs.setAttributesFor(message2);
            }
                    
            parts = new SimpleMessageAttributes[1];
            parts[0] = msgAttrs;
            */
            //} catch (Exception e) {
            //getLogger().error("Error interpreting a message/rfc822: " + e);
            //e.printStackTrace();
            //}

            // TODO: Warn till fixed!
            log.warn("Unknown/unhandled subtype " + secondaryType + " of message encountered.");
        } else {
            log.warn("Unknown/unhandled subtype " + secondaryType + " of message encountered.");
        }
    }
}