List of usage examples for javax.mail Part isMimeType
public boolean isMimeType(String mimeType) throws MessagingException;
From source file:org.xwiki.contrib.mail.internal.JavamailMessageParser.java
/** * Extracts mail content, and manage attachments. * /*w w w. ja v a 2 s .c o m*/ * @param part * @return * @throws MessagingException * @throws IOException * @throws UnsupportedEncodingException */ public MailContent extractMailContent(Part part) throws MessagingException, IOException { logger.debug("extractMailContent..."); if (part == null) { return null; } MailContent mailContent = new MailContent(); if (part.isMimeType("application/pkcs7-mime") || part.isMimeType("multipart/encrypted")) { logger.debug("Mail content is ENCRYPTED"); mailContent.setText( "<<<This e-mail part is encrypted. Text Content and attachments of encrypted e-mails are not published in Mail Archiver to avoid disclosure of restricted or confidential information.>>>"); mailContent.setHtml( "<i><<<This e-mail is encrypted. Text Content and attachments of encrypted e-mails are not published in Mail Archiver to avoid disclosure of restricted or confidential information.>>></i>"); mailContent.setEncrypted(true); return mailContent; } else { mailContent = extractPartsContent(part); } // TODO : filling attachment cids and creating xwiki attachments should be done in same method HashMap<String, String> attachmentsMap = fillAttachmentContentIds(mailContent.getAttachments()); String fileName = ""; for (MimeBodyPart currentbodypart : mailContent.getAttachments()) { try { String cid = currentbodypart.getContentID(); fileName = currentbodypart.getFileName(); // replace by correct name if filename was renamed (multiple attachments with same name) if (attachmentsMap.containsKey(cid)) { fileName = attachmentsMap.get(cid); } logger.debug("Treating attachment: " + fileName + " with contentid " + cid); if (fileName == null) { fileName = "file.ext"; } if (fileName.equals("oledata.mso") || fileName.endsWith(".wmz") || fileName.endsWith(".emz")) { logger.debug("Garbaging Microsoft crap !"); } else { String disposition = currentbodypart.getDisposition(); String attcontentType = currentbodypart.getContentType().toLowerCase(); logger.debug("Treating attachment of type: " + attcontentType); /* * XWikiAttachment wikiAttachment = new XWikiAttachment(); wikiAttachment.setFilename(fileName); * wikiAttachment.setContent(currentbodypart.getInputStream()); */ MailAttachment wikiAttachment = new MailAttachment(); wikiAttachment.setCid(cid); wikiAttachment.setFilename(fileName); byte[] filedatabytes = IOUtils.toByteArray(currentbodypart.getInputStream()); wikiAttachment.setData(filedatabytes); mailContent.addWikiAttachment(cid, wikiAttachment); } // end if } catch (Exception e) { logger.warn("Attachment " + fileName + " could not be treated", e); } } return mailContent; }
From source file:de.kp.ames.web.function.access.imap.ImapConsumer.java
/** * @param part/*w w w .j a v a2 s.c o m*/ * @return * @throws Exception */ private String messageFromPart(Part part) throws Exception { if (part.isMimeType(GlobalConstants.MT_TEXT)) { String text = readPart(part); return text; } else if (part.isMimeType(GlobalConstants.MT_HTML)) { String text = readPart(part); return text; } else if (part.isMimeType(GlobalConstants.MT_MULTIPART)) { Multipart mp = (Multipart) part.getContent(); return messageFromMultipart(mp); } return ""; }
From source file:com.glaf.mail.business.MailBean.java
/** * ??stringBuffer? ??MimeType??????/* w w w . ja v a 2 s.co m*/ * * @param part * @throws MessagingException * @throws IOException */ public void parseMailContent(Part part) throws MessagingException, IOException { String contentType = part.getContentType(); int nameindex = contentType.indexOf("name"); boolean conname = false; if (nameindex != -1) { conname = true; } logger.debug("contentType:" + contentType); if (part.isMimeType("text/plain") && !conname) { mail.setContent((String) part.getContent()); } else if (part.isMimeType("text/html") && !conname) { mail.setHtml((String) part.getContent()); } else if (part.isMimeType("multipart/*")) { Multipart multipart = (Multipart) part.getContent(); int count = multipart.getCount(); for (int i = 0; i < count; i++) { parseMailContent(multipart.getBodyPart(i)); } } else if (part.isMimeType("message/rfc822")) { parseMailContent((Part) part.getContent()); } }
From source file:org.nuxeo.ecm.platform.mail.listener.action.ExtractMessageInformationAction.java
protected void getAttachmentParts(Part part, String defaultFilename, MimetypeRegistry mimeService, ExecutionContext context) throws MessagingException, IOException { String filename = getFilename(part, defaultFilename); List<Blob> blobs = (List<Blob>) context.get(ATTACHMENTS_KEY); if (part.isMimeType("multipart/alternative")) { bodyContent += getText(part);/*from ww w . j a v a 2 s . c o m*/ } else { if (!part.isMimeType("multipart/*")) { String disp = part.getDisposition(); // no disposition => mail body, which can be also blob (image for // instance) if (disp == null && // convert only text part.getContentType().toLowerCase().startsWith("text/")) { bodyContent += decodeMailBody(part); } else { Blob blob; try (InputStream in = part.getInputStream()) { blob = Blobs.createBlob(in); } String mime = DEFAULT_BINARY_MIMETYPE; try { if (mimeService != null) { ContentType contentType = new ContentType(part.getContentType()); mime = mimeService.getMimetypeFromFilenameAndBlobWithDefault(filename, blob, contentType.getBaseType()); } } catch (MessagingException | MimetypeDetectionException e) { log.error(e); } blob.setMimeType(mime); blob.setFilename(filename); blobs.add(blob); } } if (part.isMimeType("multipart/*")) { // This is a Multipart Multipart mp = (Multipart) part.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) { getAttachmentParts(mp.getBodyPart(i), defaultFilename, mimeService, context); } } else if (part.isMimeType(MESSAGE_RFC822_MIMETYPE)) { // This is a Nested Message getAttachmentParts((Part) part.getContent(), defaultFilename, mimeService, context); } } }
From source file:org.xwiki.contrib.mail.internal.JavamailMessageParser.java
/** * Recursively extracts content of an email. Every Part that has a file name, or is neither multipart, plain text or * html, is considered an attachment./*w w w . j av a 2 s .c om*/ * * @param part * @return * @throws MessagingException * @throws UnsupportedEncodingException * @throws IOException */ public MailContent extractPartsContent(Part part) throws MessagingException, IOException { MailContent mailContent = new MailContent(); String contentType = part.getContentType().toLowerCase(); if (!StringUtils.isBlank(part.getFileName()) || (!contentType.startsWith("multipart/") && !part.isMimeType("text/plain") && !part.isMimeType("text/html"))) { mailContent.addAttachment((MimeBodyPart) part); } else if (part.isMimeType("text/plain")) { logger.debug("Extracting part PLAIN TEXT"); mailContent.appendText(MimeUtility.decodeText((String) part.getContent())); } else if (part.isMimeType("text/html")) { logger.debug("Extracting part HTML"); mailContent.appendHtml(MimeUtility.decodeText((String) part.getContent())); } else if (part.isMimeType("message/rfc822")) { logger.debug("Extracting part message/rfc822"); Message innerMessage = (Message) part.getContent(); mailContent.addAttachedMail(innerMessage); // FIXME attached mails should be loaded previously to their container } else if (contentType.startsWith("multipart/")) { logger.debug("Extracting MULTIPART"); Multipart multipart = (Multipart) part.getContent(); if (contentType.startsWith("multipart/signed")) { // Signed multiparts contain 2 parts: first is the content, second is the control information // We just ignore the control information logger.debug("Extracting SIGNED MULTIPART"); mailContent.append(extractPartsContent(multipart.getBodyPart(0))); } else if (part.isMimeType("multipart/related") || part.isMimeType("multipart/mixed") || part.isMimeType("multipart/alternative")) { logger.debug("Extracting multipart / related or mixed or alternative"); // FIXME multipart/alternative should be treated differently than other parts, though the same treatment // should be ok most of the time // (multipart/alternative is usually one part text/plain and the alternative text/html, so as text and // html // are always considered alternates by this algorithm, it's ok) int i = 0; int mcount = multipart.getCount(); while (i < mcount) { logger.debug("Adding MULTIPART #{}", i); try { final MailContent innerMailContent = extractPartsContent(multipart.getBodyPart(i)); mailContent.append(innerMailContent); } catch (Exception e) { logger.warn("Could not add MULTIPART #{} because of {}", i, ExceptionUtils.getRootCause(e)); } i++; } } else { logger.info("Multipart subtype {} not managed", contentType.substring(0, contentType.indexOf(' '))); } } else { logger.info("Message Type {} not managed", contentType.substring(0, contentType.indexOf('/'))); } return mailContent; }
From source file:org.apache.camel.component.mail.MailBinding.java
protected void extractAttachmentsFromMultipart(Multipart mp, Map<String, DataHandler> map) throws javax.mail.MessagingException, IOException { for (int i = 0; i < mp.getCount(); i++) { Part part = mp.getBodyPart(i); LOG.trace("Part #" + i + ": " + part); if (part.isMimeType("multipart/*")) { LOG.trace("Part #" + i + ": is mimetype: multipart/*"); extractAttachmentsFromMultipart((Multipart) part.getContent(), map); } else {//from w ww . j ava2 s.c o m String disposition = part.getDisposition(); if (LOG.isTraceEnabled()) { LOG.trace("Part #" + i + ": Disposition: " + part.getDisposition()); LOG.trace("Part #" + i + ": Description: " + part.getDescription()); LOG.trace("Part #" + i + ": ContentType: " + part.getContentType()); LOG.trace("Part #" + i + ": FileName: " + part.getFileName()); LOG.trace("Part #" + i + ": Size: " + part.getSize()); LOG.trace("Part #" + i + ": LineCount: " + part.getLineCount()); } if (disposition != null && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) { // only add named attachments String fileName = part.getFileName(); if (fileName != null) { LOG.debug("Mail contains file attachment: " + fileName); // Parts marked with a disposition of Part.ATTACHMENT are clearly attachments CollectionHelper.appendValue(map, fileName, part.getDataHandler()); } } } } }
From source file:org.alfresco.email.server.impl.subetha.SubethaEmailMessage.java
/** * Method extracts file name from a message part for saving its as aa attachment. If the file name can't be extracted, it will be generated based on defaultPrefix parameter. * //from www . j a va 2s . co m * @param defaultPrefix This prefix fill be used for generating file name. * @param messagePart A part of message * @return File name. * @throws MessagingException */ private String getPartFileName(String defaultPrefix, Part messagePart) throws MessagingException { String fileName = messagePart.getFileName(); if (fileName != null) { try { fileName = MimeUtility.decodeText(fileName); } catch (UnsupportedEncodingException ex) { // Nothing to do :) } } else { fileName = defaultPrefix; if (messagePart.isMimeType(MIME_PLAIN_TEXT)) fileName += ".txt"; else if (messagePart.isMimeType(MIME_HTML_TEXT)) fileName += ".html"; else if (messagePart.isMimeType(MIME_XML_TEXT)) fileName += ".xml"; else if (messagePart.isMimeType(MIME_IMAGE)) fileName += ".gif"; } return fileName; }
From source file:com.naryx.tagfusion.cfm.mail.cfPOP3.java
private void retrieveBody(cfSession _Session, Part Mess, cfQueryResultData popData, int Row, File attachmentDir) throws Exception { if (Mess.isMimeType("multipart/*")) { Multipart mp = (Multipart) Mess.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) retrieveBody(_Session, mp.getBodyPart(i), popData, Row, attachmentDir); } else {/*from ww w.j av a2 s . c om*/ String filename = cfMailMessageData.getFilename(Mess); String dispos = Mess.getDisposition(); // note: text/enriched shouldn't be treated as a text part of the email (see bug #2227) if ((dispos == null || dispos.equalsIgnoreCase(Part.INLINE)) && Mess.isMimeType("text/*") && !Mess.isMimeType("text/enriched")) { String 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) { content = new String(UTF7Converter.convert(readInputStream(Mess))); } else { try { content = (String) Mess.getContent(); } catch (UnsupportedEncodingException e) { content = "Unable to retrieve message body due to UnsupportedEncodingException:" + e.getMessage(); } catch (ClassCastException e) { // shouldn't happen but handle it gracefully content = new String(readInputStream(Mess)); } } if (Mess.isMimeType("text/html")) { popData.setCell(Row, 14, new cfStringData(content)); } else if (Mess.isMimeType("text/plain")) { popData.setCell(Row, 15, new cfStringData(content)); } popData.setCell(Row, 11, new cfStringData(content)); } else if (attachmentDir != null) { File outFile; if (filename == null) filename = "unknownfile"; outFile = getAttachedFilename(attachmentDir, filename, getDynamic(_Session, "GENERATEUNIQUEFILENAMES").getBoolean()); try { BufferedInputStream in = new BufferedInputStream(Mess.getInputStream()); BufferedOutputStream out = new BufferedOutputStream( cfEngine.thisPlatform.getFileIO().getFileOutputStream(outFile)); IOUtils.copy(in, out); out.flush(); out.close(); in.close(); //--[ Update the fields cfStringData cell = (cfStringData) popData.getCell(Row, 12); if (cell.getString().length() == 0) cell = new cfStringData(filename); else cell = new cfStringData(cell.getString() + "," + filename); popData.setCell(Row, 12, cell); cell = (cfStringData) popData.getCell(Row, 13); if (cell.getString().length() == 0) cell = new cfStringData(outFile.toString()); else cell = new cfStringData(cell.getString() + "," + outFile.toString()); popData.setCell(Row, 13, cell); } catch (Exception ignoreException) { } } } }
From source file:mitm.common.pdf.MessagePDFBuilder.java
private Collection<Part> preprocessAttachments(final Collection<Part> attachments) { Collection<Part> prepared = new LinkedList<Part>(); for (Part attachment : attachments) { try {/*from w w w. j a va2 s . co m*/ if (attachment.isMimeType("message/rfc822")) { /* * We need to convert the attached message to a PDF. */ Part pdfAttachment = convertRFC822(attachment); prepared.add(pdfAttachment); } else { prepared.add(attachment); } } catch (MessagingException e) { prepared.add(attachment); } } return prepared; }
From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java
public void removeAll(Part part, BodyPart notDelete) throws MessagingException, IOException { if (isAttach(part)) { if (part != notDelete) { BodyPart multi = (BodyPart) part; Multipart parent = multi.getParent(); parent.removeBodyPart(multi); }/*from ww w . j av a2 s . co m*/ } else if (part.isMimeType(MULTIPART_TYPE)) { Multipart mp = (Multipart) part.getContent(); List<BodyPart> toRemove = new ArrayList<BodyPart>(); for (int i = 0; i < mp.getCount(); i++) { BodyPart bodyPart = mp.getBodyPart(i); if (removePart(bodyPart, notDelete)) { toRemove.add(bodyPart); } else { removeAll(bodyPart, notDelete); } } for (BodyPart bp : toRemove) mp.removeBodyPart(bp); } }