List of usage examples for javax.mail Part getInputStream
public InputStream getInputStream() throws IOException, MessagingException;
From source file:de.kp.ames.web.function.access.imap.ImapConsumer.java
/** * @param part/*from w w w .j a va 2 s . c o m*/ * @return * @throws MessagingException */ private String readPart(Part part) throws MessagingException { try { return readStream(part.getInputStream()); } catch (IOException e) { /* * Try to get message from raw input stream */ final InputStream stream; if (part instanceof MimeBodyPart) { final MimeBodyPart mimeBodyPart = (MimeBodyPart) part; stream = mimeBodyPart.getRawInputStream(); } else if (part instanceof MimeMessage) { final MimeMessage mm = (MimeMessage) part; stream = mm.getRawInputStream(); } else { stream = null; } if (stream == null) { /* * Neither a MimeBodyPart nor a MimeMessage */ return ""; } try { return readStream(stream); } catch (IOException e1) { return ""; } finally { try { stream.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }
From source file:org.alfresco.email.server.impl.subetha.SubethaEmailMessagePart.java
/** * Object can be built on existing message part only. * // ww w . j a v a2 s . c o m * @param messagePart Message part. */ public SubethaEmailMessagePart(Part messagePart) { ParameterCheck.mandatory("messagePart", messagePart); try { fileSize = messagePart.getSize(); fileName = messagePart.getFileName(); contentType = messagePart.getContentType(); Matcher matcher = encodingExtractor.matcher(contentType); if (matcher.find()) { encoding = matcher.group(1); if (!Charset.isSupported(encoding)) { throw new EmailMessageException(ERR_UNSUPPORTED_ENCODING, encoding); } } try { contentInputStream = messagePart.getInputStream(); } catch (Exception ex) { throw new EmailMessageException(ERR_FAILED_TO_READ_CONTENT_STREAM, ex.getMessage()); } } catch (MessagingException e) { throw new EmailMessageException(ERR_INCORRECT_MESSAGE_PART, e.getMessage()); } }
From source file:com.googlecode.gmail4j.javamail.JavaMailGmailMessage.java
@Override public GmailAttachment getAttachment(int partIndex) { GmailAttachment result = null;/*from w ww . j ava 2s. co m*/ try { Object content = this.source.getContent(); if (content instanceof Multipart) { Multipart multipart = (Multipart) content; Part bodyPart = multipart.getBodyPart(partIndex); if (bodyPart.getDisposition() != null) { if (bodyPart.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) { result = new GmailAttachment(partIndex, bodyPart.getFileName(), bodyPart.getContentType(), bodyPart.getInputStream()); } } } else { throw new GmailException("Failed to get attachement with partIndex :" + partIndex); } } catch (Exception e) { throw new GmailException("Failed to get attachement with partIndex :" + partIndex, e); } return result; }
From source file:com.googlecode.gmail4j.javamail.JavaMailGmailMessage.java
@Override public List<GmailAttachment> getAttachements() { List<GmailAttachment> result = new ArrayList<GmailAttachment>(); try {/* w w w.j ava 2s . c o m*/ Object content = this.source.getContent(); if (content instanceof Multipart) { Multipart multipart = (Multipart) content; for (int i = 0; i < multipart.getCount(); i++) { Part bodyPart = multipart.getBodyPart(i); if (bodyPart.getDisposition() != null) { if (bodyPart.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) { result.add(new GmailAttachment(i, bodyPart.getFileName(), bodyPart.getContentType(), bodyPart.getInputStream())); } } } } } catch (Exception e) { throw new GmailException("Failed to get attachements", e); } return result; }
From source file:mitm.common.pdf.MessagePDFBuilder.java
private void addAttachment(final Part part, PdfWriter pdfWriter) throws IOException, MessagingException { byte[] content = IOUtils.toByteArray(part.getInputStream()); String filename = StringUtils .defaultString(HeaderUtils.decodeTextQuietly(MimeUtils.getFilenameQuietly(part)), "attachment.bin"); String baseType;/* w w w . j a v a2 s.co m*/ String contentType = null; try { contentType = part.getContentType(); MimeType mimeType = new MimeType(contentType); baseType = mimeType.getBaseType(); } catch (MimeTypeParseException e) { /* * Can happen when the content-type is not correct. Example with missing ; between charset and name: * * Content-Type: application/pdf; * charset="Windows-1252" name="postcard2010.pdf" */ logger.warn("Unable to infer MimeType from content type. Fallback to application/octet-stream. " + "Content-Type: " + contentType); baseType = MimeTypes.OCTET_STREAM; } PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(pdfWriter, null, filename, content, true /* compress */, baseType, null); pdfWriter.addFileAttachment(fileSpec); }
From source file:org.silverpeas.core.mail.extractor.EMLExtractor.java
private String processMailPart(Part part, List<MailAttachment> attachments) throws MessagingException, IOException { if (!isTextPart(part)) { Object content = part.getContent(); if (content instanceof Multipart) { Multipart mContent = (Multipart) content; return processMultipart(mContent, attachments); } else if (attachments != null) { String fileName = getFileName(part); if (fileName != null) { MailAttachment attachment = new MailAttachment(fileName); String dir = FileRepositoryManager.getTemporaryPath() + "mail" + System.currentTimeMillis(); File file = new File(dir, fileName); FileUtils.copyInputStreamToFile(part.getInputStream(), file); attachment.setPath(file.getAbsolutePath()); attachment.setSize(file.length()); attachments.add(attachment); }/*from www.j a v a 2s . co m*/ } } else { if (part.getContentType().indexOf(MimeTypes.HTML_MIME_TYPE) >= 0) { return (String) part.getContent(); } else if (part.getContentType().indexOf(MimeTypes.PLAIN_TEXT_MIME_TYPE) >= 0) { return WebEncodeHelper.javaStringToHtmlParagraphe((String) part.getContent()); } } return ""; }
From source file:org.silverpeas.util.mail.EMLExtractor.java
private String processMailPart(Part part, List<MailAttachment> attachments) throws MessagingException, IOException { if (!isTextPart(part)) { Object content = part.getContent(); if (content instanceof Multipart) { Multipart mContent = (Multipart) content; return processMultipart(mContent, attachments); } else if (attachments != null) { String fileName = getFileName(part); if (fileName != null) { MailAttachment attachment = new MailAttachment(fileName); String dir = FileRepositoryManager.getTemporaryPath() + "mail" + System.currentTimeMillis(); File file = new File(dir, fileName); FileUtils.copyInputStreamToFile(part.getInputStream(), file); attachment.setPath(file.getAbsolutePath()); attachment.setSize(file.length()); attachments.add(attachment); }/*w w w.j a va2 s. c om*/ } } else { if (part.getContentType().indexOf(MimeTypes.HTML_MIME_TYPE) >= 0) { return (String) part.getContent(); } else if (part.getContentType().indexOf(MimeTypes.PLAIN_TEXT_MIME_TYPE) >= 0) { return EncodeHelper.javaStringToHtmlParagraphe((String) part.getContent()); } } return ""; }
From source file:com.liferay.mail.imap.IMAPAccessor.java
public AttachmentHandler getAttachment(long folderId, long messageId, String contentPath) throws IOException, PortalException { Folder jxFolder = null;//w w w .j a v a2 s .c o m try { jxFolder = openFolder(folderId); Message jxMessage = getMessage(jxFolder, messageId); if (jxMessage == null) { throw new MailException(MailException.MESSAGE_NOT_FOUND_ON_SERVER); } Part part = getPart(jxMessage, contentPath); AttachmentHandler attachmentHandler = new IMAPAttachmentHandler(part.getInputStream(), jxFolder); return attachmentHandler; } catch (MessagingException me) { throw new MailException(me); } }
From source file:mitm.common.security.smime.SMIMEUtils.java
/** * Returns the S/MIME type of the message (if S/MIME). It first checks the message headers to see if the * message can be a S/MIME message. If so it checks if it's a clear signed message by checking the * headers further. If it's not a clear signed message it will try to determine the CMS content type. *//*w w w.j a v a2s. co m*/ public static SMIMEType getSMIMEType(Part part) throws MessagingException, IOException { /* first check the headers */ SMIMEHeader.Type headerType = SMIMEHeader.getSMIMEContentType(part); if (headerType == SMIMEHeader.Type.NO_SMIME) { return SMIMEType.NONE; } if (headerType == SMIMEHeader.Type.CLEAR_SIGNED || headerType == SMIMEHeader.Type.UNKNOWN_CLEAR_SIGNED) { Object content = part.getContent(); if (!(content instanceof Multipart)) { logger.warn("Header says clear signed but content is not multipart."); return SMIMEType.NONE; } Multipart multipart = (Multipart) content; /* the content should be a multipart/mixed with 2 parts */ BodyPart[] parts = SMIMEUtils.dissectSigned(multipart); if (parts == null) { if (headerType == SMIMEHeader.Type.CLEAR_SIGNED) { logger.warn("Header says s/mime but missing message part and/or signature part."); } else { logger.debug("Message is clear signed but not S/MIME signed."); } return SMIMEType.NONE; } CMSContentType cmsType = CMSContentTypeClassifier.getContentType(parts[1].getInputStream()); /* check if the signed part is really a CMS structure */ if (cmsType != CMSContentType.SIGNEDDATA) { logger.warn("Header says s/mime but signature part is not a valid CMS signed data."); return SMIMEType.NONE; } return SMIMEType.SIGNED; } else { /* check the CMS structure */ CMSContentType cmsType = CMSContentTypeClassifier.getContentType(part.getInputStream()); switch (cmsType) { case SIGNEDDATA: return SMIMEType.SIGNED; case ENVELOPEDDATA: return SMIMEType.ENCRYPTED; case COMPRESSEDDATA: return SMIMEType.COMPRESSED; default: break; } logger.warn("Header says s/mime but the message is not a valid CMS structure."); } return SMIMEType.NONE; }
From source file:org.alfresco.repo.imap.AttachmentsExtractor.java
/** * Create an attachment given a mime part * //from ww w . ja va 2 s. c om * @param messageFile the file containing the message * @param attachmentsFolderRef where to put the attachment * @param part the mime part * @throws MessagingException * @throws IOException */ private void createAttachment(NodeRef messageFile, NodeRef attachmentsFolderRef, Part part) throws MessagingException, IOException { String fileName = part.getFileName(); if (fileName == null || fileName.isEmpty()) { fileName = "unnamed"; } try { fileName = MimeUtility.decodeText(fileName); } catch (UnsupportedEncodingException e) { if (logger.isWarnEnabled()) { logger.warn("Cannot decode file name '" + fileName + "'", e); } } ContentType contentType = new ContentType(part.getContentType()); if (contentType.getBaseType().equalsIgnoreCase("application/ms-tnef")) { // The content is TNEF HMEFMessage hmef = new HMEFMessage(part.getInputStream()); // hmef.getBody(); List<org.apache.poi.hmef.Attachment> attachments = hmef.getAttachments(); for (org.apache.poi.hmef.Attachment attachment : attachments) { String subName = attachment.getLongFilename(); NodeRef attachmentNode = fileFolderService.searchSimple(attachmentsFolderRef, subName); if (attachmentNode == null) { /* * If the node with the given name does not already exist Create the content node to contain the attachment */ FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, subName, ContentModel.TYPE_CONTENT); attachmentNode = createdFile.getNodeRef(); serviceRegistry.getNodeService().createAssociation(messageFile, attachmentNode, ImapModel.ASSOC_IMAP_ATTACHMENT); byte[] bytes = attachment.getContents(); ContentWriter writer = fileFolderService.getWriter(attachmentNode); // TODO ENCODING - attachment.getAttribute(TNEFProperty.); String extension = attachment.getExtension(); String mimetype = mimetypeService.getMimetype(extension); if (mimetype != null) { writer.setMimetype(mimetype); } OutputStream os = writer.getContentOutputStream(); ByteArrayInputStream is = new ByteArrayInputStream(bytes); FileCopyUtils.copy(is, os); } } } else { // not TNEF NodeRef attachmentFile = fileFolderService.searchSimple(attachmentsFolderRef, fileName); // The one possible behaviour /* * if (result.size() > 0) { for (FileInfo fi : result) { fileFolderService.delete(fi.getNodeRef()); } } */ // And another one behaviour which will overwrite the content of the existing file. It is performance preferable. if (attachmentFile == null) { FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, fileName, ContentModel.TYPE_CONTENT); nodeService.createAssociation(messageFile, createdFile.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENT); attachmentFile = createdFile.getNodeRef(); } else { String newFileName = imapService.generateUniqueFilename(attachmentsFolderRef, fileName); FileInfo createdFile = fileFolderService.create(attachmentsFolderRef, newFileName, ContentModel.TYPE_CONTENT); nodeService.createAssociation(messageFile, createdFile.getNodeRef(), ImapModel.ASSOC_IMAP_ATTACHMENT); attachmentFile = createdFile.getNodeRef(); } nodeService.setProperty(attachmentFile, ContentModel.PROP_DESCRIPTION, nodeService.getProperty(messageFile, ContentModel.PROP_NAME)); ContentWriter writer = fileFolderService.getWriter(attachmentFile); writer.setMimetype(contentType.getBaseType()); OutputStream os = writer.getContentOutputStream(); FileCopyUtils.copy(part.getInputStream(), os); } }