Search in sources :

Example 31 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project nhin-d by DirectProject.

the class WrappedMessage method create.

/**
	 * Wraps a messaging into a new message by creating a message wrapper, copying only desired headers into the wrapper, and pushing the entire
	 * original message (including headers) into the body of the wrapper.  
	 * @param message The message to wrap.
	 * @param headersToCopy The headers that should be copied from the original message into the wrapper.
	 * @return A message object that wraps the entire original entity (including headers) in its body. 
	 * @throws MessagingException
	 */
public static Message create(Message message, String[] headersToCopy) throws MessagingException {
    if (message == null)
        throw new IllegalArgumentException("Message cannot be null");
    InternetHeaders copiedHeaders = copyHeaders(message, headersToCopy);
    copiedHeaders.setHeader(MailStandard.Headers.ContentType, MailStandard.MediaType.WrappedMessage);
    return new Message(copiedHeaders, EntitySerializer.Default.serializeToBytes(message));
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) MimeMessage(javax.mail.internet.MimeMessage)

Example 32 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project nhin-d by DirectProject.

the class Notification method getNotificationFieldsAsHeaders.

/**
	 * Parses the notification part fields of the MimeMultipart body of a MDN message.  The multipart is expected to conform to the MDN specification
	 * as described in RFC3798.
	 * @return The notification part fields as a set of Internet headers. 
	 */
public static InternetHeaders getNotificationFieldsAsHeaders(MimeMultipart mm) {
    InternetHeaders retVal = null;
    if (mm == null)
        throw new IllegalArgumentException("Multipart can not be null");
    try {
        if (mm.getCount() < 2)
            throw new IllegalArgumentException("Multipart can not be null");
        // the second part should be the notification
        BodyPart part = mm.getBodyPart(1);
        try {
            Object contecntObj = part.getContent();
            if (dsnClass != null && dsnClass.getCanonicalName().equals(contecntObj.getClass().getCanonicalName())) {
                retVal = (InternetHeaders) getHeaders.invoke(contecntObj);
                return retVal;
            }
        } catch (Exception e) {
        /* no-op */
        }
        if (!part.getContentType().equalsIgnoreCase(MDNStandard.MediaType.DispositionNotification))
            throw new IllegalArgumentException("Notification part content type is not " + MDNStandard.MediaType.DispositionNotification);
        // parse fields
        retVal = new InternetHeaders();
        String[] fields = getPartContentBodyAsString(part).split("\r\n");
        for (String field : fields) {
            int idx = field.indexOf(":");
            if (idx > -1) {
                String name = field.substring(0, idx);
                String value = field.substring(idx + 1).trim();
                retVal.setHeader(name, value);
            }
        }
    } catch (MessagingException e) {
        throw new NHINDException("Failed to parse notification fields.", e);
    }
    return retVal;
}
Also used : BodyPart(javax.mail.BodyPart) InternetHeaders(javax.mail.internet.InternetHeaders) MessagingException(javax.mail.MessagingException) NHINDException(org.nhindirect.stagent.NHINDException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 33 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project ats-framework by Axway.

the class MimePackage method parseContent.

private void parseContent(MimePart part, boolean doNotParseBrokenParts) throws PackageException {
    if (exceedsMaxNestedLevel()) {
        if (log.isInfoEnabled() && !skippedParsingMsgIsAlreadyLogged) {
            log.info("Skipping parsing of nested message parts from current MimePackage because max nested level is reached." + " Current max nesting level is " + ActionLibraryConfigurator.getInstance().getMimePackageMaxNestedLevel());
            skippedParsingMsgIsAlreadyLogged = true;
        }
        return;
    }
    try {
        Object content = part.getContent();
        if (content instanceof Multipart) {
            // if multipart recurse through all child parts
            MimeMultipart mimeMultipart = (MimeMultipart) content;
            int partCount = mimeMultipart.getCount();
            for (int i = 0; i < partCount; i++) {
                try {
                    parseContent((MimeBodyPart) mimeMultipart.getBodyPart(i));
                } catch (PackageException pe) {
                    if (doNotParseBrokenParts) {
                        log.warn("Could not parse part: " + mimeMultipart.getBodyPart(i).getContentType());
                    } else {
                        log.error("Could not parse part: " + mimeMultipart.getBodyPart(i).getContentType());
                        throw new PackageException(pe);
                    }
                }
            }
        } else if (content instanceof MimeMessage) {
            MimeMessage mimeMessage = (MimeMessage) content;
            nestedMimePackages.add(new MimePackage(this.nestedPath, nestedMimePackages.size(), mimeMessage, partOfImapFolder));
            // to treat it as such - it will not be decomposed
            if (isPartAttachment(part)) {
                parts.add(part);
                attachmentPartIndices.add(parts.size() - 1);
            } else {
                try {
                    parseContent(mimeMessage);
                } catch (PackageException pe) {
                    if (doNotParseBrokenParts) {
                        log.warn("Could not parse part: " + mimeMessage.getContentID());
                    } else {
                        log.error("Could not parse part: " + mimeMessage.getContentID());
                        throw pe;
                    }
                }
            }
        } else {
            InternetHeaders internetHeaders = null;
            if (part.getContentType().toLowerCase().startsWith(CONTENT_TYPE_RFC822_HEADERS)) {
                try {
                    // check for "text/rfc822-headers"
                    internetHeaders = getInternetHeaders(content);
                } catch (PackageException e) {
                    throw new PackageException("Content type " + CONTENT_TYPE_RFC822_HEADERS + " is found but headers are not parsed successfully.", e);
                }
                if (internetHeaders == null) {
                    // javax.mail implementation is not very strict
                    log.error("Mail part with content type " + CONTENT_TYPE_RFC822_HEADERS + " is found but could not be parsed. Contents: " + content);
                }
            }
            if (internetHeaders != null) {
                // the "Content-Type" is "text/rfc822-headers" and javamail returns it as InternetHeaders.
                // this is a message with headers only, we will keep it as a
                // nested MimePackage
                MimePackage nestedMimePackage = new MimePackage();
                nestedMimePackage.setNestedPath(this.nestedPath, nestedMimePackages.size());
                Enumeration<?> enumerator = internetHeaders.getAllHeaders();
                while (enumerator.hasMoreElements()) {
                    Header inetHeader = (Header) enumerator.nextElement();
                    nestedMimePackage.addHeader(inetHeader.getName(), inetHeader.getValue());
                }
                nestedMimePackages.add(nestedMimePackage);
            } else {
                // add the body
                parts.add(part);
                if (isPartAttachment(part)) {
                    attachmentPartIndices.add(parts.size() - 1);
                } else {
                    regularPartIndices.add(parts.size() - 1);
                }
            }
        }
    } catch (MessagingException me) {
        throw new PackageException("Could not parse MIME part", me);
    } catch (IOException ioe) {
        throw new PackageException("Could not parse MIME message", ioe);
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Enumeration(java.util.Enumeration) InternetHeaders(javax.mail.internet.InternetHeaders) Header(javax.mail.Header) PackageHeader(com.axway.ats.action.objects.model.PackageHeader) MimeMultipart(javax.mail.internet.MimeMultipart) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) IOException(java.io.IOException)

Example 34 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project zm-mailbox by Zimbra.

the class MessageRFC822Handler method getContentImpl.

/**
     * Returns the subject of the attached message.
     */
@Override
protected String getContentImpl() throws MimeHandlerException {
    DataSource ds = getDataSource();
    if (ds == null) {
        return null;
    }
    InputStream is = null;
    String content = null;
    try {
        is = ds.getInputStream();
        if (is == null) {
            return null;
        }
        InternetHeaders headers = new InternetHeaders(is);
        String[] subject = headers.getHeader("Subject");
        if (subject == null || subject.length == 0 || subject[0] == null) {
            return null;
        }
        int maxLength = MimeHandlerManager.getIndexedTextLimit();
        if (subject[0].length() > maxLength) {
            content = subject[0].substring(0, maxLength);
        } else {
            content = subject[0];
        }
    } catch (Exception e) {
        throw new MimeHandlerException(e);
    } finally {
        ByteUtil.closeStream(is);
    }
    return content;
}
Also used : MimeHandlerException(com.zimbra.cs.mime.MimeHandlerException) InternetHeaders(javax.mail.internet.InternetHeaders) InputStream(java.io.InputStream) MimeHandlerException(com.zimbra.cs.mime.MimeHandlerException) DataSource(javax.activation.DataSource)

Example 35 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project logging-log4j2 by apache.

the class SmtpManager method sendEvents.

/**
     * Send the contents of the cyclic buffer as an e-mail message.
     * @param layout The layout for formatting the events.
     * @param appendEvent The event that triggered the send.
     */
public void sendEvents(final Layout<?> layout, final LogEvent appendEvent) {
    if (message == null) {
        connect(appendEvent);
    }
    try {
        final LogEvent[] priorEvents = buffer.removeAll();
        // LOG4J-310: log appendEvent even if priorEvents is empty
        final byte[] rawBytes = formatContentToBytes(priorEvents, appendEvent, layout);
        final String contentType = layout.getContentType();
        final String encoding = getEncoding(rawBytes, contentType);
        final byte[] encodedBytes = encodeContentToBytes(rawBytes, encoding);
        final InternetHeaders headers = getHeaders(contentType, encoding);
        final MimeMultipart mp = getMimeMultipart(encodedBytes, headers);
        sendMultipartMessage(message, mp);
    } catch (final MessagingException | IOException | RuntimeException e) {
        logError("Caught exception while sending e-mail notification.", e);
        throw new LoggingException("Error occurred while sending email", e);
    }
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) LoggingException(org.apache.logging.log4j.LoggingException) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Aggregations

InternetHeaders (javax.mail.internet.InternetHeaders)51 MimeMultipart (javax.mail.internet.MimeMultipart)18 MessagingException (javax.mail.MessagingException)16 BodyPart (javax.mail.BodyPart)14 IOException (java.io.IOException)10 DispositionNotification (com.sun.mail.dsn.DispositionNotification)9 MimeMessage (javax.mail.internet.MimeMessage)8 MimeMultipartReport (org.apache.mailet.base.mail.MimeMultipartReport)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Header (javax.mail.Header)5 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)5 InputStream (java.io.InputStream)4 MimeBodyPart (javax.mail.internet.MimeBodyPart)4 NHINDException (org.nhindirect.stagent.NHINDException)4 MimeException (org.nhindirect.stagent.mail.MimeException)4 X509Certificate (java.security.cert.X509Certificate)3 ContentType (javax.mail.internet.ContentType)3 ParseException (javax.mail.internet.ParseException)3 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)3