Example usage for javax.mail.internet MimeUtility unfold

List of usage examples for javax.mail.internet MimeUtility unfold

Introduction

In this page you can find the example usage for javax.mail.internet MimeUtility unfold.

Prototype

public static String unfold(String s) 

Source Link

Document

Unfold a folded header.

Usage

From source file:Main.java

public static String getReplytoInetAddressForBroken(MimeMessage msg) {
    String rawReplyto = getRawReplyTo(msg);
    String inetAddress = null;/*  www  .j  av  a2  s.co m*/
    try {
        inetAddress = MimeUtility.decodeText(MimeUtility.unfold(rawReplyto));
    } catch (UnsupportedEncodingException ignore) {
    }
    return null != inetAddress ? inetAddress : "";
}

From source file:com.cubusmail.mail.MessageHandler.java

/**
 * @return/*from   w  w  w.  j  a va  2s  . co m*/
 * @throws MessagingException
 */
public String getSubject() throws MessagingException {

    // return this.message.getSubject();
    String sub = this.message.getHeader("Subject", null);
    if (sub != null) {
        try {
            sub = MimeUtility.unfold(sub);
            sub = MimeUtility.decodeText(sub);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage());
        }
        return sub;
    }
    return sub;
}

From source file:de.mendelson.comm.as2.message.AS2MessageCreation.java

/**Enwrapps the data into a signed MIME message structure and returns it*/
private void enwrappInMessageAndSign(AS2Message message, Part contentPart, Partner sender, Partner receiver)
        throws Exception {
    AS2MessageInfo info = (AS2MessageInfo) message.getAS2Info();
    MimeMessage messagePart = new MimeMessage(Session.getInstance(System.getProperties(), null));
    //sign message if this is requested
    if (info.getSignType() != AS2Message.SIGNATURE_NONE) {
        MimeMultipart signedPart = this.signContentPart(contentPart, sender, receiver);
        if (this.logger != null) {
            String signAlias = this.signatureCertManager.getAliasByFingerprint(sender.getSignFingerprintSHA1());
            this.logger.log(Level.INFO,
                    this.rb.getResourceString("message.signed",
                            new Object[] { info.getMessageId(), signAlias,
                                    this.rbMessage.getResourceString("signature." + receiver.getSignType()) }),
                    info);/*w  w w.  j  a  va2 s.c o m*/
        }
        messagePart.setContent(signedPart);
        messagePart.saveChanges();
    } else {
        //unsigned message
        if (contentPart instanceof MimeBodyPart) {
            MimeMultipart unsignedPart = new MimeMultipart();
            unsignedPart.addBodyPart((MimeBodyPart) contentPart);
            if (this.logger != null) {
                this.logger.log(Level.INFO,
                        this.rb.getResourceString("message.notsigned", new Object[] { info.getMessageId() }),
                        info);
            }
            messagePart.setContent(unsignedPart);
        } else if (contentPart instanceof MimeMultipart) {
            messagePart.setContent((MimeMultipart) contentPart);
        } else if (contentPart instanceof MimeMessage) {
            messagePart = (MimeMessage) contentPart;
        } else {
            throw new IllegalArgumentException("enwrappInMessageAndSign: Unable to set the content of a "
                    + contentPart.getClass().getName());
        }
        messagePart.saveChanges();
    }
    //store signed or unsigned data
    ByteArrayOutputStream signedOut = new ByteArrayOutputStream();
    //normally the content type header is folded (which is correct but some products are not able to parse this properly)
    //Now take the content-type, unfold it and write it
    Enumeration headerLines = messagePart.getMatchingHeaderLines(new String[] { "Content-Type" });
    LineOutputStream los = new LineOutputStream(signedOut);
    while (headerLines.hasMoreElements()) {
        //requires java mail API >= 1.4
        String nextHeaderLine = MimeUtility.unfold((String) headerLines.nextElement());
        //write the line only if the as2 message is encrypted. If the as2 message is unencrypted this header is added later
        //in the class MessageHttpUploader
        if (info.getEncryptionType() != AS2Message.ENCRYPTION_NONE) {
            los.writeln(nextHeaderLine);
        }
        //store the content line in the as2 message object, this value is required later in MessageHttpUploader
        message.setContentType(nextHeaderLine.substring(nextHeaderLine.indexOf(':') + 1));
    }
    messagePart.writeTo(signedOut, new String[] { "Message-ID", "Mime-Version", "Content-Type" });
    signedOut.flush();
    signedOut.close();
    message.setDecryptedRawData(signedOut.toByteArray());
}

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

/**
 * Returns the decoded and unfolded value for the given header name.  If
 * multiple headers with the same name exist, returns the first one.
 * If the header does not exist, returns <tt>null</tt>.
 *//*ww w . j a v  a2  s. com*/
public static String getHeader(MimePart part, String headerName) {
    try {
        String value = part.getHeader(headerName, null);
        if (value == null || value.isEmpty()) {
            return null;
        }

        try {
            value = MimeUtility.decodeText(value);
        } catch (UnsupportedEncodingException e) {
        }
        value = MimeUtility.unfold(value);
        return value;
    } catch (MessagingException e) {
        sLog.debug("Unable to get header '%s'", headerName, e);
        return null;
    }
}

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.
 *//*  www  . ja  va  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:davmail.imap.ImapConnection.java

protected void appendEnvelopeHeader(StringBuilder buffer, String[] value) throws UnsupportedEncodingException {
    if (buffer.charAt(buffer.length() - 1) != '(') {
        buffer.append(' ');
    }/*w ww .j  a v a  2  s .  c  o m*/
    if (value != null && value.length > 0) {
        appendEnvelopeHeaderValue(buffer, MimeUtility.unfold(value[0]));
    } else {
        buffer.append("NIL");
    }
}

From source file:davmail.imap.ImapConnection.java

protected void appendMailEnvelopeHeader(StringBuilder buffer, String[] value) {
    buffer.append(' ');
    if (value != null && value.length > 0) {
        try {/*from   www  .jav  a 2  s  .  c o  m*/
            String unfoldedValue = MimeUtility.unfold(value[0]);
            InternetAddress[] addresses = InternetAddress.parseHeader(unfoldedValue, false);
            if (addresses != null && addresses.length > 0) {
                buffer.append('(');
                for (InternetAddress address : addresses) {
                    buffer.append('(');
                    String personal = address.getPersonal();
                    if (personal != null) {
                        appendEnvelopeHeaderValue(buffer, personal);
                    } else {
                        buffer.append("NIL");
                    }
                    buffer.append(" NIL ");
                    String mail = address.getAddress();
                    int atIndex = mail.indexOf('@');
                    if (atIndex >= 0) {
                        buffer.append('"').append(mail.substring(0, atIndex)).append('"');
                        buffer.append(' ');
                        buffer.append('"').append(mail.substring(atIndex + 1)).append('"');
                    } else {
                        buffer.append("NIL NIL");
                    }
                    buffer.append(')');
                }
                buffer.append(')');
            } else {
                buffer.append("NIL");
            }
        } catch (AddressException e) {
            DavGatewayTray.warn(e);
            buffer.append("NIL");
        } catch (UnsupportedEncodingException e) {
            DavGatewayTray.warn(e);
            buffer.append("NIL");
        }
    } else {
        buffer.append("NIL");
    }
}

From source file:davmail.imap.ImapConnection.java

protected void appendBodyStructure(StringBuilder buffer, MimePart bodyPart)
        throws IOException, MessagingException {
    String contentType = MimeUtility.unfold(bodyPart.getContentType());
    int slashIndex = contentType.indexOf('/');
    if (slashIndex < 0) {
        throw new DavMailException("EXCEPTION_INVALID_CONTENT_TYPE", contentType);
    }//from   w  w  w . j  a va2  s . c  om
    String type = contentType.substring(0, slashIndex).toUpperCase();
    buffer.append("(\"").append(type).append("\" \"");
    int semiColonIndex = contentType.indexOf(';');
    if (semiColonIndex < 0) {
        buffer.append(contentType.substring(slashIndex + 1).toUpperCase()).append("\" NIL");
    } else {
        // extended content type
        buffer.append(contentType.substring(slashIndex + 1, semiColonIndex).trim().toUpperCase()).append('\"');
        int charsetindex = contentType.indexOf("charset=");
        int nameindex = contentType.indexOf("name=");
        if (charsetindex >= 0 || nameindex >= 0) {
            buffer.append(" (");

            if (charsetindex >= 0) {
                buffer.append("\"CHARSET\" ");
                int charsetSemiColonIndex = contentType.indexOf(';', charsetindex);
                int charsetEndIndex;
                if (charsetSemiColonIndex > 0) {
                    charsetEndIndex = charsetSemiColonIndex;
                } else {
                    charsetEndIndex = contentType.length();
                }
                String charSet = contentType.substring(charsetindex + "charset=".length(), charsetEndIndex);
                if (!charSet.startsWith("\"")) {
                    buffer.append('"');
                }
                buffer.append(charSet.trim().toUpperCase());
                if (!charSet.endsWith("\"")) {
                    buffer.append('"');
                }
            }

            if (nameindex >= 0) {
                if (charsetindex >= 0) {
                    buffer.append(' ');
                }

                buffer.append("\"NAME\" ");
                int nameSemiColonIndex = contentType.indexOf(';', nameindex);
                int nameEndIndex;
                if (nameSemiColonIndex > 0) {
                    nameEndIndex = nameSemiColonIndex;
                } else {
                    nameEndIndex = contentType.length();
                }
                String name = contentType.substring(nameindex + "name=".length(), nameEndIndex).trim();
                if (!name.startsWith("\"")) {
                    buffer.append('"');
                }
                buffer.append(name.trim());
                if (!name.endsWith("\"")) {
                    buffer.append('"');
                }
            }
            buffer.append(')');
        } else {
            buffer.append(" NIL");
        }
    }
    appendBodyStructureValue(buffer, bodyPart.getContentID());
    appendBodyStructureValue(buffer, bodyPart.getDescription());
    appendBodyStructureValue(buffer, bodyPart.getEncoding());
    appendBodyStructureValue(buffer, bodyPart.getSize());
    if ("MESSAGE".equals(type) || "TEXT".equals(type)) {
        // line count not implemented in JavaMail, return fake line count
        appendBodyStructureValue(buffer, bodyPart.getSize() / 80);
    } else {
        // do not send line count for non text bodyparts
        appendBodyStructureValue(buffer, -1);
    }
    buffer.append(')');
}

From source file:com.aimluck.eip.mail.util.ALMailUtils.java

/**
 * ???????//from  w  w  w . jav  a 2  s.  c o m
 *
 * @param subject
 * @return
 */
public static String decodeSubject(String subject) {
    try {
        subject = MimeUtility.decodeText(MimeUtility.unfold(subject));
        return UnicodeCorrecter.correctToCP932(MailUtility.decodeText(subject));
    } catch (UnsupportedEncodingException e) {
        return MailUtility.decodeText(subject);
    }
}

From source file:com.aimluck.eip.mail.util.ALMailUtils.java

public static String getFromInetAddressForBroken(MimeMessage msg) {
    String rawFrom = getRawFrom(msg);
    String inetAddress = null;/*from  w  ww .ja  v  a2 s  .c o m*/
    try {
        inetAddress = MimeUtility.decodeText(MimeUtility.unfold(rawFrom));
    } catch (UnsupportedEncodingException ignore) {
    }
    return null != inetAddress ? inetAddress : "";
}