Example usage for javax.mail Part INLINE

List of usage examples for javax.mail Part INLINE

Introduction

In this page you can find the example usage for javax.mail Part INLINE.

Prototype

String INLINE

To view the source code for javax.mail Part INLINE.

Click Source Link

Document

This part should be presented inline.

Usage

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

/**
 * ??// ww w  .j a  v  a2 s .c o m
 * 
 * @param part
 * @return
 * @throws MessagingException
 * @throws IOException
 */
public boolean isContainAttch(Part part) throws MessagingException, IOException {
    boolean flag = false;
    if (part.isMimeType("multipart/*")) {
        Multipart multipart = (Multipart) part.getContent();
        int count = multipart.getCount();
        for (int i = 0; i < count; i++) {
            BodyPart bodypart = multipart.getBodyPart(i);
            String dispostion = bodypart.getDisposition();
            if ((dispostion != null)
                    && (dispostion.equals(Part.ATTACHMENT) || dispostion.equals(Part.INLINE))) {
                flag = true;
            } else if (bodypart.isMimeType("multipart/*")) {
                flag = isContainAttch(bodypart);
            } else {
                String contentType = bodypart.getContentType();
                if (contentType.toLowerCase().indexOf("appliaction") != -1) {
                    flag = true;
                }
                if (contentType.toLowerCase().indexOf("name") != -1) {
                    flag = true;
                }
            }
        }
    } else if (part.isMimeType("message/rfc822")) {
        flag = isContainAttch((Part) part.getContent());
    }

    return flag;
}

From source file:org.silverpeas.util.mail.EMLExtractor.java

private boolean isTextPart(Part part) throws MessagingException {
    String disposition = part.getDisposition();
    if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) {
        try {//ww w. ja  v  a 2 s . c  om
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType());
        } catch (ParseException e) {
            SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE",
                    e);
        }
    } else if (Part.INLINE.equals(disposition)) {
        try {
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType()) && getFileName(part) == null;
        } catch (ParseException e) {
            SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE",
                    e);
        }
    }
    return false;
}

From source file:org.silverpeas.core.mail.extractor.EMLExtractor.java

private boolean isTextPart(Part part) throws MessagingException {
    String disposition = part.getDisposition();
    if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) {
        try {/*  w w w.ja  v  a 2s.  c o  m*/
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType());
        } catch (ParseException e) {
            SilverLogger.getLogger(this).error(e);
        }
    } else if (Part.INLINE.equals(disposition)) {
        try {
            ContentType type = new ContentType(part.getContentType());
            return "text".equalsIgnoreCase(type.getPrimaryType()) && getFileName(part) == null;
        } catch (ParseException e) {
            SilverLogger.getLogger(this).error(e);
        }
    }
    return false;
}

From source file:ru.retbansk.utils.scheduled.impl.ReadEmailAndConvertToXmlSpringImpl.java

/**
 * Loads email properties, reads email messages, 
 * converts their multipart bodies into string,
 * send error emails if message doesn't contain valid report and at last returns a reversed Set of the Valid Reports
 * <p><b> deletes messages/*from   w w  w. j a va 2 s.co m*/
 * @see #readEmail()
 * @see #convertToXml(HashSet)
 * @see ru.retbansk.utils.scheduled.ReplyManager
 * @return HashSet<DayReport> of the Valid Day Reports
 */
@Override
public HashSet<DayReport> readEmail() throws Exception {

    Properties prop = loadProperties();
    String host = prop.getProperty("host");
    String user = prop.getProperty("user");
    String password = prop.getProperty("password");
    path = prop.getProperty("path");

    // Get system properties
    Properties properties = System.getProperties();

    // Get the default Session object.
    Session session = Session.getDefaultInstance(properties);

    // Get a Store object that implements the specified protocol.
    Store store = session.getStore("pop3");

    // Connect to the current host using the specified username and
    // password.
    store.connect(host, user, password);

    // Create a Folder object corresponding to the given name.
    Folder folder = store.getFolder("inbox");

    // Open the Folder.
    folder.open(Folder.READ_WRITE);
    HashSet<DayReport> dayReportSet = new HashSet<DayReport>();
    try {

        // Getting messages from the folder
        Message[] message = folder.getMessages();
        // Reversing the order in the array with the use of Set to make the last one final
        Collections.reverse(Arrays.asList(message));

        // Reading messages.
        String body;
        for (int i = 0; i < message.length; i++) {
            DayReport dayReport = null;
            dayReport = new DayReport();
            dayReport.setPersonId(((InternetAddress) message[i].getFrom()[0]).getAddress());
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+3"));
            calendar.setTime(message[i].getSentDate());
            dayReport.setCalendar(calendar);
            dayReport.setSubject(message[i].getSubject());
            List<TaskReport> reportList = null;

            //Release the string from email message body
            body = "";
            Object content = message[i].getContent();
            if (content instanceof java.lang.String) {
                body = (String) content;
            } else if (content instanceof Multipart) {
                Multipart mp = (Multipart) content;

                for (int j = 0; j < mp.getCount(); j++) {
                    Part part = mp.getBodyPart(j);

                    String disposition = part.getDisposition();

                    if (disposition == null) {
                        MimeBodyPart mbp = (MimeBodyPart) part;
                        if (mbp.isMimeType("text/plain")) {
                            body += (String) mbp.getContent();
                        }
                    } else if ((disposition != null)
                            && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {
                        MimeBodyPart mbp = (MimeBodyPart) part;
                        if (mbp.isMimeType("text/plain")) {
                            body += (String) mbp.getContent();
                        }
                    }
                }
            }

            //Put the string (body of email message) and get list of valid reports else send error
            reportList = new ArrayList<TaskReport>();
            reportList = giveValidReports(body, message[i].getSentDate());
            //Check for valid day report. To be valid it should have size of reportList > 0.
            if (reportList.size() > 0) {
                // adding valid ones to Set
                dayReport.setReportList(reportList);
                dayReportSet.add(dayReport);
            } else {
                // This message doesn't have valid reports. Sending an error reply.
                logger.info("Invalid Day Report detected. Sending an error reply");
                ReplyManager man = new ReplyManagerSimpleImpl();
                man.sendError(dayReport);
            }
            // Delete message
            message[i].setFlag(Flags.Flag.DELETED, true);
        }

    } finally {
        // true tells the mail server to expunge deleted messages.
        folder.close(true);
        store.close();
    }

    return dayReportSet;
}

From source file:com.cubusmail.mail.util.MessageUtils.java

/**
 * @param part//from w w w . j ava 2s .  co  m
 * @return
 * @throws MessagingException
 * @throws IOException
 */
public static boolean hasAttachments(Part part) throws MessagingException, IOException {

    try {
        if (part.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) part.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                MimeBodyPart bodyPart = (MimeBodyPart) mp.getBodyPart(i);
                if (Part.ATTACHMENT.equals(bodyPart.getDisposition())
                        || Part.INLINE.equals(bodyPart.getDisposition())) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    return false;
}

From source file:org.simplejavamail.internal.util.MimeMessageParser.java

/**
 * Extracts the content of a MimeMessage recursively.
 *
 * @param part   the current MimePart//from w w w  .  ja  va  2s  . com
 * @throws MessagingException parsing the MimeMessage failed
 * @throws IOException        parsing the MimeMessage failed
 */
private void parse(final MimePart part) throws MessagingException, IOException {
    extractCustomUserHeaders(part);

    if (isMimeType(part, "text/plain") && plainContent == null
            && !Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
        plainContent = (String) part.getContent();
    } else {
        if (isMimeType(part, "text/html") && htmlContent == null
                && !Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
            htmlContent = (String) part.getContent();
        } else {
            if (isMimeType(part, "multipart/*")) {
                final Multipart mp = (Multipart) part.getContent();
                final int count = mp.getCount();

                // iterate over all MimeBodyPart
                for (int i = 0; i < count; i++) {
                    parse((MimeBodyPart) mp.getBodyPart(i));
                }
            } else {
                final DataSource ds = createDataSource(part);
                // If the diposition is not provided, the part should be treat as attachment
                if (part.getDisposition() == null || Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                    this.attachmentList.put(part.getContentID(), ds);
                } else if (Part.INLINE.equalsIgnoreCase(part.getDisposition())) {
                    this.cidMap.put(part.getContentID(), ds);
                } else {
                    throw new IllegalStateException("invalid attachment type");
                }
            }
        }
    }
}

From source file:mitm.common.mail.BodyPartUtils.java

/**
 * Creates a MimeBodyPart with the provided message attached as a RFC822 attachment. 
 *///from  w  ww. ja  v  a  2 s  .  c  o m
public static MimeBodyPart toRFC822(MimeMessage message, String filename) throws MessagingException {
    MimeBodyPart bodyPart = new MimeBodyPart();

    bodyPart.setContent(message, "message/rfc822");
    /* somehow the content-Type header is not set so we need to set it ourselves */
    bodyPart.setHeader("Content-Type", "message/rfc822");
    bodyPart.setDisposition(Part.INLINE);
    bodyPart.setFileName(filename);

    return bodyPart;
}

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

private void extractBody(Part Mess, cfArrayData AD, String attachURI, String attachDIR) throws Exception {

    if (Mess.isMimeType("multipart/*")) {

        Multipart mp = (Multipart) Mess.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            extractBody(mp.getBodyPart(i), AD, attachURI, attachDIR);

    } else {/*from  www  .  j  a va2 s.c o  m*/

        cfStructData sd = new cfStructData();

        String tmp = Mess.getContentType();
        if (tmp.indexOf(";") != -1)
            tmp = tmp.substring(0, tmp.indexOf(";"));

        sd.setData("mimetype", new cfStringData(tmp));

        String filename = getFilename(Mess);
        String dispos = getDisposition(Mess);

        MimeType messtype = new MimeType(tmp);
        // Note that we can't use Mess.isMimeType() here due to bug #2080
        if ((dispos == null || dispos.equalsIgnoreCase(Part.INLINE)) && messtype.match("text/*")) {

            Object content;
            String contentType = Mess.getContentType().toLowerCase();
            // support aliases of UTF-7 - UTF7, UNICODE-1-1-UTF-7, csUnicode11UTF7, UNICODE-2-0-UTF-7
            if (contentType.indexOf("utf-7") != -1 || contentType.indexOf("utf7") != -1) {
                InputStream ins = Mess.getInputStream();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                IOUtils.copy(ins, bos);
                content = new String(UTF7Converter.convert(bos.toByteArray()));
            } else {
                try {
                    content = Mess.getContent();
                } catch (UnsupportedEncodingException e) {
                    content = Mess.getInputStream();
                } catch (IOException ioe) {
                    // This will happen on BD/Java when the attachment has no content
                    // NOTE: this is the fix for bug NA#3198.
                    content = "";
                }
            }

            if (content instanceof InputStream) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                IOUtils.copy((InputStream) content, bos);
                sd.setData("content", new cfStringData(new String(bos.toByteArray())));
            } else {
                sd.setData("content", new cfStringData(content.toString()));
            }

            sd.setData("file", cfBooleanData.FALSE);
            sd.setData("filename", new cfStringData(filename == null ? "" : filename));

        } else if (attachDIR != null) {

            sd.setData("content", new cfStringData(""));

            if (filename == null || filename.length() == 0)
                filename = "unknownfile";

            filename = getAttachedFilename(attachDIR, filename);

            //--[ An attachment, save it out to disk
            try {
                BufferedInputStream in = new BufferedInputStream(Mess.getInputStream());
                BufferedOutputStream out = new BufferedOutputStream(
                        cfEngine.thisPlatform.getFileIO().getFileOutputStream(new File(attachDIR + filename)));
                IOUtils.copy(in, out);

                out.flush();
                out.close();
                in.close();

                sd.setData("file", cfBooleanData.TRUE);
                sd.setData("filename", new cfStringData(filename));
                if (attachURI.charAt(attachURI.length() - 1) != '/')
                    sd.setData("url", new cfStringData(attachURI + '/' + filename));
                else
                    sd.setData("url", new cfStringData(attachURI + filename));
                sd.setData("size", new cfNumberData((int) new File(attachDIR + filename).length()));

            } catch (Exception ignoreException) {
                // NOTE: this could happen when we don't have permission to write to the specified directory
                //       so let's log an error message to make this easier to debug.
                cfEngine.log("-] Failed to save attachment to " + attachDIR + filename + ", exception=["
                        + ignoreException.toString() + "]");
            }

        } else {
            sd.setData("file", cfBooleanData.FALSE);
            sd.setData("content", new cfStringData(""));
        }

        AD.addElement(sd);
    }
}

From source file:it.greenvulcano.gvesb.virtual.utils.MimeMessageHelper.java

public MimeMessage getMimeMessage() throws MessagingException {

    if (attachments.isEmpty()) {
        switch (messageType) {
        case HTML:
            mimeMessage.setContent(messageBody, "text/html; charset=utf-8");
            break;

        case TEXT:
            mimeMessage.setText(messageBody, "UTF-8");
            mimeMessage.addHeader("Content-Transfer-Encoding", "quoted-printable");
            break;
        }//from  w ww  . ja v  a 2 s.  com
    } else {

        MimeBodyPart mimeBodyPart = new MimeBodyPart();

        switch (messageType) {
        case HTML:
            mimeBodyPart.setContent(messageBody, "text/html; charset=utf-8");
            break;

        case TEXT:
            mimeBodyPart.setText(messageBody, "UTF-8");
            mimeMessage.addHeader("Content-Transfer-Encoding", "quoted-printable");
            break;
        }

        mimeBodyPart.setDisposition(Part.INLINE);

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mimeBodyPart);

        for (BodyPart attachmentPart : attachments) {
            multipart.addBodyPart(attachmentPart);
        }

        mimeMessage.setContent(multipart);
    }

    return mimeMessage;
}

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

/**
 * ?//from  ww w  .  java2 s.  co  m
 * 
 * @param part
 * @throws MessagingException
 * @throws IOException
 */
public void parseAttachment(Part part) throws MessagingException, IOException {
    String filename = "";
    if (part.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) part.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
            BodyPart mpart = mp.getBodyPart(i);
            String dispostion = mpart.getDisposition();
            if ((dispostion != null)
                    && (dispostion.equals(Part.ATTACHMENT) || dispostion.equals(Part.INLINE))) {
                filename = mpart.getFileName();
                if (filename != null) {
                    logger.debug("orig filename=" + filename);
                    if (filename.indexOf("?") != -1) {
                        filename = new String(filename.getBytes("GBK"), "UTF-8");
                    }
                    if (filename.toLowerCase().indexOf("gb2312") != -1) {
                        filename = MimeUtility.decodeText(filename);
                    }
                    filename = MailUtils.convertString(filename);
                    logger.debug("filename=" + filename);
                    parseFileContent(filename, mpart.getInputStream());
                }
            } else if (mpart.isMimeType("multipart/*")) {
                parseAttachment(mpart);
            } else {
                filename = mpart.getFileName();
                if (filename != null) {
                    logger.debug("orig filename=" + filename);
                    if (filename.indexOf("?") != -1) {
                        filename = new String(filename.getBytes("GBK"), "UTF-8");
                    }
                    if (filename.toLowerCase().indexOf("gb2312") != -1) {
                        filename = MimeUtility.decodeText(filename);
                    }
                    filename = MailUtils.convertString(filename);
                    parseFileContent(filename, mpart.getInputStream());
                    logger.debug("filename=" + filename);
                }
            }
        }

    } else if (part.isMimeType("message/rfc822")) {
        parseAttachment((Part) part.getContent());
    }
}