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:Main.java

private static String getSubject(MimeMessage msg) throws MessagingException, UnsupportedEncodingException {
    return MimeUtility.decodeText(msg.getSubject());
}

From source file:Main.java

public static String decodeText(String encodeText) throws UnsupportedEncodingException {
    if (encodeText == null || "".equals(encodeText)) {
        return "";
    } else {//  w  ww  .j  a v a2  s. co  m
        encodeText = MimeUtility.decodeText(encodeText);
        return MimeUtility.decodeText(encodeText);
    }
}

From source file:Main.java

public static String getReplytoInetAddressForBroken(MimeMessage msg) {
    String rawReplyto = getRawReplyTo(msg);
    String inetAddress = null;//from ww w  .j  a v a 2  s .  c  om
    try {
        inetAddress = MimeUtility.decodeText(MimeUtility.unfold(rawReplyto));
    } catch (UnsupportedEncodingException ignore) {
    }
    return null != inetAddress ? inetAddress : "";
}

From source file:ru.codemine.ccms.mail.EmailAttachmentExtractor.java

public void saveAllAttachment() {
    String protocol = ssl ? "imaps" : "imap";

    Properties props = new Properties();
    props.put("mail.store.protocol", protocol);

    try {//  ww w .j ava2 s.c  om
        Session session = Session.getInstance(props);
        Store store = session.getStore();
        store.connect(host, port, username, password);

        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);

        for (Message msg : inbox.getMessages()) {
            if (!msg.isSet(Flags.Flag.SEEN)) {
                String fileNamePrefix = settingsService.getStorageEmailPath() + "msg-" + msg.getMessageNumber();
                Multipart multipart = (Multipart) msg.getContent();
                for (int i = 0; i < multipart.getCount(); i++) {
                    BodyPart bodyPart = multipart.getBodyPart(i);
                    if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())
                            && StringUtils.isNotEmpty(bodyPart.getFileName())) {
                        MimeBodyPart mimimi = (MimeBodyPart) bodyPart; // =(^.^)= 
                        mimimi.saveFile(fileNamePrefix + MimeUtility.decodeText(bodyPart.getFileName()));
                        msg.setFlag(Flags.Flag.SEEN, true);
                    }
                }
            }
        }
    } catch (IOException | MessagingException e) {
        log.error("? ? ?,  : "
                + e.getMessage());
    }

}

From source file:com.glaf.mail.business.MailBean.java

/**
 * ?????<br/>/*  w w w  .j  a v  a 2s . c  o  m*/
 * ???? "to"-->,"cc"-->??,"bcc"-->??
 * 
 * @param type
 * @return
 * @throws MessagingException
 * @throws UnsupportedEncodingException
 */
public String getMailAddress(String type) throws MessagingException, UnsupportedEncodingException {
    String mailAddr = "";
    String addrType = type.toUpperCase();
    InternetAddress[] address = null;

    if (addrType.equals("TO") || addrType.equals("CC") || addrType.equals("BCC")) {
        if (addrType.equals("TO")) {
            address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.TO);
        }
        if (addrType.equals("CC")) {
            address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.CC);
        }
        if (addrType.equals("BCC")) {
            address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.BCC);
        }
        if (address != null) {
            for (int i = 0; i < address.length; i++) {
                String mail = address[i].getAddress();
                if (mail == null) {
                    mail = "";
                } else {
                    mail = MimeUtility.decodeText(mail);
                }
                String personal = address[i].getPersonal();
                if (personal == null) {
                    personal = "";
                } else {
                    personal = MimeUtility.decodeText(personal);
                }
                String compositeto = personal + "<" + mail + ">";
                mailAddr += "," + compositeto;
            }
            mailAddr = mailAddr.substring(1);
        }
    } else {
        throw new RuntimeException("Error email Type!");
    }
    mailAddr = MailUtils.convertString(mailAddr);
    return mailAddr;
}

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

private static void dumpPart(Part p, Hashtable<String, Part> attachments, Hashtable<String, String> inlines,
        Hashtable<String, String> images, Hashtable<String, String> nonImages, ArrayList<String> mimeTypes,
        String subject) throws Exception {
    mimeTypes.add(p.getContentType());/*from ww  w .j a va2s.co  m*/
    if (!p.isMimeType("multipart/*")) {

        String disp = p.getDisposition();
        String fname = p.getFileName();
        if (fname != null) {
            try {
                fname = MimeUtility.decodeText(fname);
            } catch (Exception e) {
                logger.debug("cannot decode filename:" + e.getMessage());
            }
        }
        if ((disp != null && Compare.equalsIgnoreCase(disp, Part.ATTACHMENT)) || fname != null) {
            String filename = getFilename(subject, p);
            if (!filename.equalsIgnoreCase("winmail.dat")) {
                attachments.put(filename, p);
            }
            /*if (p.isMimeType("image/*")) {
               String str[] = p.getHeader("Content-ID");
               if (str != null) images.put(filename,str[0]);
               else images.put(filename, filename);
            }
            return;*/
        }
    }
    if (p.isMimeType("text/plain")) {
        String str = "";
        if (inlines.containsKey("text/plain"))
            str = (String) inlines.get("text/plain") + "\n\n-------------------------------\n";
        inlines.put("text/plain", str + getTextContent(p));
    } else if (p.isMimeType("text/html")) {
        inlines.put("text/html", getTextContent(p));
    } else if (p.isMimeType("text/xml")) {
        attachments.put(getFilename(subject, p), p);
    } else if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        for (int i = 0; i < mp.getCount(); i++)
            dumpPart(mp.getBodyPart(i), attachments, inlines, images, nonImages, mimeTypes, subject);
    } else if (p.isMimeType("message/rfc822")) {
        dumpPart((Part) p.getContent(), attachments, inlines, images, nonImages, mimeTypes, subject);
    } else if (p.isMimeType("application/ms-tnef")) {
        Part tnefpart = TNEFMime.convert(null, p, false);
        if (tnefpart != null) {
            dumpPart((Part) tnefpart, attachments, inlines, images, nonImages, mimeTypes, subject);
        }
    } else if (p.isMimeType("application/*")) {
        String filename = getFilename("application", p);
        attachments.put(filename, p);
        String str[] = p.getHeader("Content-ID");
        if (str != null)
            nonImages.put(filename, str[0]);
    } else if (p.isMimeType("image/*")) {
        String fileName = getFilename("image", p);
        attachments.put(fileName, p);
        String str[] = p.getHeader("Content-ID");
        if (str != null)
            images.put(fileName, str[0]);
        else
            images.put(fileName, fileName);
    } else {
        String contentType = p.getContentType();
        Object o = p.getContent();
        if (o instanceof String) {
            String str = "";
            if (inlines.containsKey("text/plain"))
                str = (String) inlines.get("text/plain") + "\n\n-------------------------------\n";
            inlines.put(contentType, str + (String) o);
        } else {
            String fileName = getFilenameFromContentType("attach", contentType);
            attachments.put(fileName, p);
        }
    }
}

From source file:dtw.webmail.model.JwmaMessagePartImpl.java

/**
 * Creates a <tt>JwmaMessagePartImpl</tt> instance from a given
 * <tt>javax.mail.Part</tt> instance.
 *
 * @param part a <tt>javax.mail.Part</tt> instance.
 * @param number the number of the part as <tt>int</tt>.
 *
 * @return the newly created instance./*from   ww w .ja  v a  2s .  c  o m*/
 * @throws JwmaException if it fails to create the new instance.
 */
public static JwmaMessagePartImpl createJwmaMessagePartImpl(Part part, int number) throws JwmaException {
    JwmaMessagePartImpl partinfo = new JwmaMessagePartImpl(part, number);

    //content type
    try {
        partinfo.setContentType(part.getContentType());

        //size
        int size = part.getSize();
        //JwmaKernel.getReference().debugLog().write("Part size="+size);
        String fileName = part.getFileName();
        //correct size of encoded parts
        String[] encoding = part.getHeader("Content-Transfer-Encoding");
        if (fileName != null && encoding != null && encoding.length > 0
                && (encoding[0].equalsIgnoreCase("base64") || encoding[0].equalsIgnoreCase("uuencode"))) {
            if ((fileName.startsWith("=?GB2312?B?") && fileName.endsWith("?="))) {
                byte[] decoded = Base64.decodeBase64(fileName.substring(11, fileName.indexOf("?=")).getBytes());
                fileName = new String(decoded, "GB2312");
                /*fileName = CipherUtils.decrypt(fileName);
                System.out.println("fileName----"+fileName);*/
                //fileName = getFromBASE64(fileName.substring(11,fileName.indexOf("?=")));  
            } else if ((fileName.startsWith("=?UTF-8?") || fileName.startsWith("=?UTF8?"))
                    && fileName.endsWith("?=")) {
                fileName = MimeUtility.decodeText(fileName);
            }
            //an encoded file is about 35% smaller in reality,
            //so correct the size
            size = (int) (size * 0.65);
        }

        partinfo.setSize(size);
        //description
        partinfo.setDescription(part.getDescription());

        //filename
        partinfo.setName(fileName);

        //textcontent
        if (partinfo.isMimeType("text/*")) {
            Object content = part.getContent();
            if (content instanceof String) {
                partinfo.setTextContent((String) content);
            } else if (content instanceof InputStream) {
                InputStream in = (InputStream) content;
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                byte[] buffer = new byte[8192];
                int amount = 0;
                while ((amount = in.read(buffer)) >= 0) {
                    bout.write(buffer, 0, amount);
                }
                partinfo.setTextContent(new String(bout.toString()));
            }
        }
    } catch (Exception mex) {
        throw new JwmaException("jwma.messagepart.failedcreation", true).setException(mex);
    }
    return partinfo;
}

From source file:com.glaf.mail.util.MailUtils.java

public static String encodeFilename(String pFilename, boolean needEncode) throws Exception {
    String filename = pFilename;/*  w ww . ja  v  a 2s . com*/
    try {
        filename = MimeUtility.decodeText(filename);
    } catch (Exception ex) {
        filename = "";
    }
    boolean b_else = needEncode;
    if (b_else) {
        String s = filename;
        try {
            filename = new String(s.getBytes("8859-1"));
        } catch (Exception ex) {
            try {
                filename = new String(s.getBytes("ISO-8859-1"));
                if (filename.indexOf("?") != -1) {
                    filename = new String(s.getBytes("GBK"));
                }
            } catch (Exception eee) {
                filename = new String(s.getBytes("GBK"));
            }
        }
    } else {
        filename = new String(filename.getBytes("8859_1"));
    }
    return filename;
}

From source file:com.glaf.mail.business.MailBean.java

/**
 * ?//  w w w.  j  av a  2  s . co  m
 * 
 * @return
 * @throws UnsupportedEncodingException
 * @throws MessagingException
 */
public String getSubject() throws UnsupportedEncodingException, MessagingException {
    String subject = "";
    if (mimeMessage.getSubject() != null) {
        String text = mimeMessage.getSubject();
        logger.debug("orgi:" + text);
        text = MimeUtility.decodeText(mimeMessage.getSubject());
        logger.debug("MimeUtility.decodeText:" + text);
        text = MailUtils.convertString(mimeMessage.getSubject());
        logger.debug("MailUtils.convertString:" + text);
        subject = text;
    }
    if (subject == null) {
        subject = "";
    }
    return subject;
}

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

/**
 * @return/* ww  w  . j  ava2s. c  o 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;
}