Example usage for javax.mail.internet MimePart getFileName

List of usage examples for javax.mail.internet MimePart getFileName

Introduction

In this page you can find the example usage for javax.mail.internet MimePart getFileName.

Prototype

public String getFileName() throws MessagingException;

Source Link

Document

Get the filename associated with this part, if possible.

Usage

From source file:mailbox.CreationViaEmail.java

@Nonnull
private static Content processPart(MimePart part, MimePart parent) throws MessagingException, IOException {
    if (part == null) {
        return new Content();
    }/*from  w  w  w  .j  av  a 2s . co m*/

    if (part.getFileName() != null) {
        // Assume that a part which has a filename is an attachment.
        return new Content(part);
    }

    if (part.isMimeType("text/*")) {
        return getContent(part);
    } else if (part.isMimeType("multipart/*")) {
        if (part.isMimeType(MimeType.MULTIPART_RELATED)) {
            return getContentWithAttachments(part);
        } else if (part.isMimeType(MimeType.MULTIPART_ALTERNATIVE)) {
            return getContentOfBestPart(part, parent);
        } else {
            return getJoinedContent(part);
        }
    }

    return new Content();
}

From source file:com.zimbra.cs.service.mail.CreateContact.java

static String fetchItemPart(ZimbraSoapContext zsc, OperationContext octxt, Mailbox mbox, ItemId iid,
        String part, String[] acceptableMimeTypes, String charsetWanted) throws ServiceException {
    String text = null;/*from   w ww.  ja  v  a2 s.  c  o  m*/
    try {
        if (iid.isLocal()) {
            // fetch from local store
            if (!mbox.getAccountId().equals(iid.getAccountId())) {
                mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
            }
            Message msg = mbox.getMessageById(octxt, iid.getId());
            MimePart mp = Mime.getMimePart(msg.getMimeMessage(), part);
            String ctype = new ContentType(mp.getContentType()).getContentType();
            String fname = mp.getFileName();
            if (fname != null && (MimeConstants.CT_APPLICATION_OCTET_STREAM.equals(ctype)
                    || MimeConstants.CT_APPLICATION_TNEF.equals(ctype))) {
                String guess = MimeDetect.getMimeDetect().detect(fname);
                if (guess != null) {
                    ctype = guess;
                }
            }
            boolean typeAcceptable;
            if (acceptableMimeTypes != null) {
                typeAcceptable = false;
                for (String type : acceptableMimeTypes) {
                    if (type != null && type.equalsIgnoreCase(ctype)) {
                        typeAcceptable = true;
                        break;
                    }
                }
            } else {
                typeAcceptable = true;
            }
            if (!typeAcceptable)
                throw MailServiceException.INVALID_CONTENT_TYPE(ctype);
            text = Mime.getStringContent(mp, charsetWanted);
        } else {
            // fetch from remote store
            Map<String, String> params = new HashMap<String, String>();
            params.put(UserServlet.QP_PART, part);
            byte[] content = UserServlet.getRemoteContent(zsc.getAuthToken(), iid, params);
            text = new String(content, MimeConstants.P_CHARSET_UTF8);
        }
    } catch (IOException e) {
        throw ServiceException.FAILURE("error fetching message part: iid=" + iid + ", part=" + part, e);
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("error fetching message part: iid=" + iid + ", part=" + part, e);
    }
    return text;
}

From source file:com.cubusmail.gwtui.server.services.RetrieveAttachmentServlet.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    WebApplicationContext context = WebApplicationContextUtils
            .getRequiredWebApplicationContext(request.getSession().getServletContext());

    try {//from  ww  w  .j a  va 2  s .  co  m
        String messageId = request.getParameter("messageId");
        String attachmentIndex = request.getParameter("attachmentIndex");
        boolean view = "1".equals(request.getParameter("view"));

        if (messageId != null) {
            IMailbox mailbox = SessionManager.get().getMailbox();
            Message msg = mailbox.getCurrentFolder().getMessageById(Long.parseLong(messageId));

            List<MimePart> attachmentList = MessageUtils.attachmentsFromPart(msg);
            int index = Integer.valueOf(attachmentIndex);

            MimePart retrievePart = attachmentList.get(index);

            ContentType contentType = new ContentType(retrievePart.getContentType());

            String fileName = retrievePart.getFileName();
            if (StringUtils.isEmpty(fileName)) {
                fileName = context.getMessage("message.unknown.attachment", null,
                        SessionManager.get().getLocale());
            }
            StringBuffer contentDisposition = new StringBuffer();
            if (!view) {
                contentDisposition.append("attachment; filename=\"");
                contentDisposition.append(fileName).append("\"");
            }

            response.setHeader("cache-control", "no-store");
            response.setHeader("pragma", "no-cache");
            response.setIntHeader("max-age", 0);
            response.setIntHeader("expires", 0);

            if (!StringUtils.isEmpty(contentDisposition.toString())) {
                response.setHeader("Content-disposition", contentDisposition.toString());
            }
            response.setContentType(contentType.getBaseType());
            // response.setContentLength(
            // MessageUtils.calculateSizeFromPart( retrievePart ) );

            BufferedInputStream bufInputStream = new BufferedInputStream(retrievePart.getInputStream());
            OutputStream outputStream = response.getOutputStream();

            byte[] inBuf = new byte[1024];
            int len = 0;
            int total = 0;
            while ((len = bufInputStream.read(inBuf)) > 0) {
                outputStream.write(inBuf, 0, len);
                total += len;
            }

            bufInputStream.close();
            outputStream.flush();
            outputStream.close();

        }
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
}

From source file:com.cubusmail.server.services.RetrieveAttachmentServlet.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    WebApplicationContext context = WebApplicationContextUtils
            .getRequiredWebApplicationContext(request.getSession().getServletContext());

    try {/*from ww w.  j  ava  2  s.  c o m*/
        String messageId = request.getParameter("messageId");
        String attachmentIndex = request.getParameter("attachmentIndex");
        boolean view = "1".equals(request.getParameter("view"));

        if (messageId != null) {
            IMailbox mailbox = SessionManager.get().getMailbox();
            Message msg = mailbox.getCurrentFolder().getMessageById(Long.parseLong(messageId));

            List<MimePart> attachmentList = MessageUtils.attachmentsFromPart(msg);
            int index = Integer.valueOf(attachmentIndex);

            MimePart retrievePart = attachmentList.get(index);

            ContentType contentType = new ContentType(retrievePart.getContentType());

            String fileName = retrievePart.getFileName();
            if (StringUtils.isEmpty(fileName)) {
                fileName = context.getMessage("message.unknown.attachment", null,
                        SessionManager.get().getLocale());
            }
            StringBuffer contentDisposition = new StringBuffer();
            if (!view) {
                contentDisposition.append("attachment; filename=\"");
                contentDisposition.append(fileName).append("\"");
            }

            response.setHeader("cache-control", "no-store");
            response.setHeader("pragma", "no-cache");
            response.setIntHeader("max-age", 0);
            response.setIntHeader("expires", 0);

            if (!StringUtils.isEmpty(contentDisposition.toString())) {
                response.setHeader("Content-disposition", contentDisposition.toString());
            }
            response.setContentType(contentType.getBaseType());
            // response.setContentLength(
            // MessageUtils.calculateSizeFromPart( retrievePart ) );

            BufferedInputStream bufInputStream = new BufferedInputStream(retrievePart.getInputStream());
            OutputStream outputStream = response.getOutputStream();

            byte[] inBuf = new byte[1024];
            int len = 0;
            int total = 0;
            while ((len = bufInputStream.read(inBuf)) > 0) {
                outputStream.write(inBuf, 0, len);
                total += len;
            }

            bufInputStream.close();
            outputStream.flush();
            outputStream.close();

        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }
}

From source file:com.consol.citrus.mail.message.MailMessageConverter.java

/**
 * Construct simple body part from binary data just adding file name as content.
 * @param mediaPart/*from  w  ww .  j  a v  a 2s. c o  m*/
 * @param contentType
 * @return
 * @throws IOException
 */
protected BodyPart handleBinaryPart(MimePart mediaPart, String contentType)
        throws IOException, MessagingException {
    String contentId = mediaPart.getContentID() != null ? "(" + mediaPart.getContentID() + ")" : "";
    return new BodyPart(mediaPart.getFileName() + contentId, contentType);
}

From source file:com.consol.citrus.mail.message.MailMessageConverter.java

/**
 * Construct multipart body with first part being the body content and further parts being the attachments.
 * @param body/* ww w  . j  a v  a 2 s  .  co  m*/
 * @return
 * @throws IOException
 */
private BodyPart handleMultiPart(Multipart body) throws IOException, MessagingException {
    BodyPart bodyPart = null;
    for (int i = 0; i < body.getCount(); i++) {
        MimePart entity = (MimePart) body.getBodyPart(i);

        if (bodyPart == null) {
            bodyPart = handlePart(entity);
        } else {
            BodyPart attachment = handlePart(entity);
            bodyPart.addPart(new AttachmentPart(attachment.getContent(),
                    parseContentType(attachment.getContentType()), entity.getFileName()));
        }
    }

    return bodyPart;
}