Example usage for javax.mail.internet MimeUtility decodeText

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

Introduction

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

Prototype

public static String decodeText(String etext) throws UnsupportedEncodingException 

Source Link

Document

Decode "unstructured" headers, that is, headers that are defined as '*text' as per RFC 822.

Usage

From source file:com.naryx.tagfusion.cfm.mail.cfPOP3.java

private static String formatAddress(Address[] addList) {
    if (addList == null || addList.length == 0)
        return "";

    try {//from w w  w.  java  2  s  .  co m
        return MimeUtility.decodeText(javax.mail.internet.InternetAddress.toString(addList)).trim();
    } catch (UnsupportedEncodingException e) {
        return javax.mail.internet.InternetAddress.toString(addList);
    }
}

From source file:com.aurel.track.util.emailHandling.MailReader.java

private static void handleAttachment(Part part, List<EmailAttachment> attachments) throws MessagingException {
    String fileName = part.getFileName();
    try {/*from   w w  w.j  a  va  2 s  . c om*/
        if (fileName != null) {
            fileName = MimeUtility.decodeText(fileName);
        } else {
            fileName = "binaryPart.dat";
        }
        attachments.add(createEmailAttachment(part, fileName));
    } catch (Exception e) {
        // just ignore
    }
}

From source file:com.stimulus.archiva.extraction.MessageExtraction.java

private static String getFilename(String defaultFileName, Part p) throws Exception {
    if (defaultFileName == "" || defaultFileName == null)
        defaultFileName = "attachment.att";
    String fName = p.getFileName();
    try {//w  w w. j  a v a2  s . c  o  m
        fName = MimeUtility.decodeText(fName);
    } catch (Exception e) {
        logger.debug("cannot decode filename:" + e.getMessage());
        fName = p.getFileName();
    }
    return getFilename(fName, defaultFileName);

}

From source file:com.panet.imeta.job.entries.getpop.JobEntryGetPOP.java

public static void handlePart(String foldername, Part part) throws MessagingException, IOException {
    String disposition = part.getDisposition();
    // String contentType = part.getContentType();

    if ((disposition != null)
            && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) {
        saveFile(foldername, MimeUtility.decodeText(part.getFileName()), part.getInputStream());
    }/*from ww  w . j  a  v a2  s . co m*/
}

From source file:com.ikon.util.MailUtils.java

/**
 * Convert Mime Message to Mail/* w w  w . j  a v a 2 s  .  c o  m*/
 */
public static Mail messageToMail(Message msg) throws MessagingException, IOException {
    com.ikon.bean.Mail mail = new com.ikon.bean.Mail();
    Calendar receivedDate = Calendar.getInstance();
    Calendar sentDate = Calendar.getInstance();

    // Can be void
    if (msg.getReceivedDate() != null) {
        receivedDate.setTime(msg.getReceivedDate());
    }

    // Can be void
    if (msg.getSentDate() != null) {
        sentDate.setTime(msg.getSentDate());
    }

    String body = getText(msg);

    // log.info("getText: "+body);
    if (body.charAt(0) == 'H') {
        mail.setMimeType(MimeTypeConfig.MIME_HTML);
    } else if (body.charAt(0) == 'T') {
        mail.setMimeType(MimeTypeConfig.MIME_TEXT);
    } else {
        mail.setMimeType(MimeTypeConfig.MIME_UNDEFINED);
    }

    String content = body.substring(1);

    // Need to replace 0x00 because PostgreSQL does not accept string containing 0x00
    content = FormatUtil.fixUTF8(content);

    // Need to remove Unicode surrogate because of MySQL => SQL Error: 1366, SQLState: HY000
    content = FormatUtil.trimUnicodeSurrogates(content);

    mail.setContent(content);

    if (msg.getFrom().length > 0) {
        mail.setFrom(MimeUtility.decodeText(msg.getFrom()[0].toString()));
    }

    mail.setSize(msg.getSize());
    mail.setSubject((msg.getSubject() == null || msg.getSubject().isEmpty()) ? NO_SUBJECT : msg.getSubject());
    mail.setTo(addressToString(msg.getRecipients(Message.RecipientType.TO)));
    mail.setCc(addressToString(msg.getRecipients(Message.RecipientType.CC)));
    mail.setBcc(addressToString(msg.getRecipients(Message.RecipientType.BCC)));
    mail.setReceivedDate(receivedDate);
    mail.setSentDate(sentDate);

    return mail;
}

From source file:com.ikon.util.MailUtils.java

/**
 * Add attachments to an imported mail./*from w w w  .j  av a2s . c om*/
 */
public static void addAttachments(String token, com.ikon.bean.Mail mail, Part p, String userId)
        throws MessagingException, IOException, UnsupportedMimeTypeException, FileSizeExceededException,
        UserQuotaExceededException, VirusDetectedException, ItemExistsException, PathNotFoundException,
        AccessDeniedException, RepositoryException, DatabaseException, ExtensionException, AutomationException {
    if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();

        for (int i = 1; i < count; i++) {
            BodyPart bp = mp.getBodyPart(i);

            if (bp.getFileName() != null) {
                String name = MimeUtility.decodeText(bp.getFileName());
                String fileName = FileUtils.getFileName(name);
                String fileExtension = FileUtils.getFileExtension(name);
                String testName = name;

                // Test if already exists a document with the same name in the mail
                for (int j = 1; OKMRepository.getInstance().hasNode(token,
                        mail.getPath() + "/" + testName); j++) {
                    // log.info("Trying with: {}", testName);
                    testName = fileName + " (" + j + ")." + fileExtension;
                }

                Document attachment = new Document();
                String mimeType = MimeTypeConfig.mimeTypes.getContentType(bp.getFileName().toLowerCase());
                attachment.setMimeType(mimeType);
                attachment.setPath(mail.getPath() + "/" + testName);
                InputStream is = bp.getInputStream();

                if (Config.REPOSITORY_NATIVE) {
                    new DbDocumentModule().create(token, attachment, is, bp.getSize(), userId);
                } else {
                    new JcrDocumentModule().create(token, attachment, is, userId);
                }

                is.close();
            }
        }
    }
}

From source file:com.openkm.util.MailUtils.java

/**
 * Add attachments to an imported mail.//  w  w w  . ja  va 2 s. c  om
 */
public static void addAttachments(String token, com.openkm.bean.Mail mail, Part p, String userId)
        throws MessagingException, IOException, UnsupportedMimeTypeException, FileSizeExceededException,
        UserQuotaExceededException, VirusDetectedException, ItemExistsException, PathNotFoundException,
        AccessDeniedException, RepositoryException, DatabaseException, ExtensionException, AutomationException {
    if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();

        for (int i = 1; i < count; i++) {
            BodyPart bp = mp.getBodyPart(i);

            if (bp.getFileName() != null) {
                String name = MimeUtility.decodeText(bp.getFileName());
                String fileName = FileUtils.getFileName(name);
                String fileExtension = FileUtils.getFileExtension(name);
                String testName = name;

                // Test if already exists a document with the same name in the mail
                for (int j = 1; OKMRepository.getInstance().hasNode(token,
                        mail.getPath() + "/" + testName); j++) {
                    // log.info("Trying with: {}", testName);
                    testName = fileName + " (" + j + ")." + fileExtension;
                }

                Document attachment = new Document();
                String mimeType = MimeTypeConfig.mimeTypes.getContentType(bp.getFileName().toLowerCase());
                attachment.setMimeType(mimeType);
                attachment.setPath(mail.getPath() + "/" + testName);
                InputStream is = bp.getInputStream();

                if (Config.REPOSITORY_NATIVE) {
                    new DbDocumentModule().create(token, attachment, is, bp.getSize(), userId);
                } else {
                    new JcrDocumentModule().create(token, attachment, is, userId);
                }

                is.close();
            }
        }
    }
}

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>.
 *///from   www.  jav  a 2 s . c o m
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.
 *///from w ww  .  ja v a 2 s  .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.aimluck.eip.mail.util.ALMailUtils.java

/**
 * ???????//from  w w w.j a va2  s  .  c om
 *
 * @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);
    }
}