List of usage examples for javax.mail Part getContentType
public String getContentType() throws MessagingException;
From source file:com.glaf.mail.business.MailBean.java
/** * ??stringBuffer? ??MimeType??????//from w ww . j a va 2s .c o 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.silverpeas.components.mailinglist.service.job.MailProcessor.java
/** * Analyze the part to check if it is an attachment, a base64 encoded file or some text. * @param part the part to be analyzed.//from w ww .ja va 2 s . c o m * @return true if it is some text - false otherwise. * @throws MessagingException */ protected boolean isTextPart(Part part) throws MessagingException { String disposition = part.getDisposition(); if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) { try { ContentType type = new ContentType(part.getContentType()); return "text".equalsIgnoreCase(type.getPrimaryType()); } catch (ParseException e) { logger.error(e.getMessage(), 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) { logger.error(e.getMessage(), e); } } return false; }
From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java
/** * Analyze the part to check if it is an attachment, a base64 encoded file or some text. * * @param part the part to be analyzed./*w w w . ja va 2 s .com*/ * @return true if it is some text - false otherwise. * @throws MessagingException */ protected boolean isTextPart(Part part) throws MessagingException { String disposition = part.getDisposition(); if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) { try { ContentType type = new ContentType(part.getContentType()); return "text".equalsIgnoreCase(type.getPrimaryType()); } catch (ParseException e) { e.printStackTrace(); } } else if (Part.INLINE.equals(disposition)) { try { ContentType type = new ContentType(part.getContentType()); return "text".equalsIgnoreCase(type.getPrimaryType()) && getFileName(part) == null; } catch (ParseException e) { e.printStackTrace(); } } return false; }
From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java
private boolean isApplicationType(Part part) throws MessagingException { return (part.getContentType() != null && part.getContentType().trim().startsWith(APPLICATION_TYPE)); }
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 ww . java2s .co m * * @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:mitm.common.mail.filter.UnsupportedFormatStripper.java
private void skipPart(Part part, boolean addSkipWarning, PartsContext context) throws MessagingException { List<Part> parts = context.getParts(); if (logger.isDebugEnabled()) { logger.debug("skipping part with content-type: " + part.getContentType()); }// w w w.ja v a2s .com if (addSkipWarning) { MimeBodyPart skipWarningPart = new MimeBodyPart(); String filename = HeaderUtils.decodeTextQuietly(MimeUtils.getFilenameQuietly(part)); if (StringUtils.isBlank(filename)) { filename = "<unknown>"; } String body = String.format("Attachment with filename %s was removed.", filename); skipWarningPart.setText(body); parts.add(skipWarningPart); } context.setModified(true); }
From source file:org.xmlactions.email.EMailParser.java
private void showPart(Part part) throws IOException, MessagingException { log.info("\n\n\nshowPart ==>>"); log.info("part.toString():" + part.toString()); log.info("part.getContent():" + (part.getFileName() == null ? part.getContent().toString() : "Attachment")); log.info("part.getContentType():" + part.getContentType()); log.info("part.getFilename():" + part.getFileName()); log.info("part.isAttachment:" + part.getFileName()); log.info("part.isMessage:" + (part.getContent() instanceof Message)); Object obj = part.getContent(); if (obj instanceof Multipart) { log.info("MultiPart"); Multipart mmp = (Multipart) obj; for (int i = 0; i < mmp.getCount(); i++) { Part bodyPart = mmp.getBodyPart(i); showPart(bodyPart);//from w w w.j a va 2 s . c o m } } else if (obj instanceof Part) { showPart((Part) obj); } else { log.info("=== Add Part ==="); log.info((String) (part.getFileName() != null ? "isAttachment" : part.getContent())); // log.info("not recognised class:" + obj.getClass().getName() + // "\n" + obj); } log.info("<<== showPart"); }
From source file:eu.openanalytics.rsb.component.EmailDepositHandler.java
private void addEmailAttachmentsToJob(final DepositEmailConfiguration depositEmailConfiguration, final MimeMessage mimeMessage, final MultiFilesJob job) throws MessagingException, IOException, FileNotFoundException { final String jobConfigurationFileName = depositEmailConfiguration.getJobConfigurationFileName(); if (StringUtils.isNotBlank(jobConfigurationFileName)) { final File jobConfigurationFile = getJobConfigurationFile( depositEmailConfiguration.getApplicationName(), jobConfigurationFileName); job.addFile(Constants.MULTIPLE_FILES_JOB_CONFIGURATION, new FileInputStream(jobConfigurationFile)); }/*w ww .java2 s . c om*/ final Object content = mimeMessage.getContent(); Validate.isTrue(content instanceof Multipart, "only multipart emails can be processed"); final Multipart multipart = (Multipart) content; for (int i = 0, n = multipart.getCount(); i < n; i++) { final Part part = multipart.getBodyPart(i); final String disposition = part.getDisposition(); if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) { final String name = part.getFileName(); final String contentType = StringUtils.substringBefore(part.getContentType(), ";"); MultiFilesJob.addDataToJob(contentType, name, part.getInputStream(), job); } } }
From source file:org.xmlactions.email.EMailParser.java
private void handlePart(Part part) throws MessagingException, IOException, DocumentException { log.debug("\n\n\nhandlePart ==>>"); log.debug("part.toString():" + part.toString()); log.debug(/*from www. java 2 s.c o m*/ "part.getContent():" + (part.getFileName() == null ? part.getContent().toString() : "Attachment")); log.debug("part.getContentType():" + part.getContentType()); log.debug("part.getFilename():" + part.getFileName()); log.debug("part.isAttachment:" + part.getFileName()); log.debug("part.isMessage:" + (part.getContent() instanceof Message)); Object obj = part.getContent(); if (obj instanceof Multipart) { Multipart mmp = (Multipart) obj; for (int i = 0; i < mmp.getCount(); i++) { Part bodyPart = mmp.getBodyPart(i); if (bodyPart instanceof Message) { setFirstMessageProcessed(true);// need to mark this when we // get a forwarded message // so we don't look for case // numbers in forwarded // emails. } handlePart(bodyPart); } } else if (obj instanceof Part) { if (obj instanceof Message) { setFirstMessageProcessed(true);// need to mark this when we get // a forwarded message so we // don't look for case numbers // in forwarded emails. } handlePart((Part) obj); } else { if (part instanceof MimeBodyPart) { MimeBodyPart p = (MimeBodyPart) part; Enumeration enumeration = p.getAllHeaders(); while (enumeration.hasMoreElements()) { Object e = enumeration.nextElement(); if (e == null) e = null; } Object content = p.getContent(); enumeration = p.getAllHeaderLines(); while (enumeration.hasMoreElements()) { Object e = enumeration.nextElement(); if (e == null) e = null; } DataHandler dh = p.getDataHandler(); if (dh == null) dh = null; } addPart(part); log.debug("=== Add Part ==="); log.debug((String) (part.getFileName() != null ? "isAttachment" : part.getContent())); // log.info("not recognised class:" + obj.getClass().getName() + // "\n" + obj); } log.debug("<<== handlePart"); }
From source file:org.alfresco.email.server.impl.subetha.SubethaEmailMessage.java
private void parseMessagePart(Part messagePart) { try {//from w ww. jav a 2s. c o m if (messagePart.isMimeType(MIME_PLAIN_TEXT) || messagePart.isMimeType(MIME_HTML_TEXT)) { if (log.isDebugEnabled()) { log.debug("Text or HTML part was found. ContentType: " + messagePart.getContentType()); } addBody(messagePart); } else if (messagePart.isMimeType(MIME_XML_TEXT)) { if (log.isDebugEnabled()) { log.debug("XML part was found."); } addAttachment(messagePart); } else if (messagePart.isMimeType(MIME_APPLICATION)) { if (log.isDebugEnabled()) { log.debug("Application part was found."); } addAttachment(messagePart); } else if (messagePart.isMimeType(MIME_IMAGE)) { if (log.isDebugEnabled()) { log.debug("Image part was found."); } addAttachment(messagePart); } else if (messagePart.isMimeType(MIME_MULTIPART)) { // if multipart, this method will be called recursively // for each of its parts Multipart mp = (Multipart) messagePart.getContent(); int count = mp.getCount(); if (log.isDebugEnabled()) { log.debug("MULTIPART with " + count + " part(s) found. Processin each part..."); } for (int i = 0; i < count; i++) { BodyPart bp = mp.getBodyPart(i); if (bp.getContent() instanceof MimeMultipart) { // It's multipart. Recurse. parseMessagePart(bp); } else { // It's the body addBody(bp); } } if (log.isDebugEnabled()) { log.debug("MULTIPART processed."); } } else if (messagePart.isMimeType(MIME_RFC822)) { // if rfc822, call this method with its content as the part if (log.isDebugEnabled()) { log.debug("MIME_RFC822 part found. Processing inside part..."); } parseMessagePart((Part) messagePart.getContent()); if (log.isDebugEnabled()) { log.debug("MIME_RFC822 processed."); } } else { // if all else fails, put this in the attachments map. // Actually we don't know what it is. if (log.isDebugEnabled()) { log.debug("Unrecognized part was found. Put it into attachments."); } addAttachment(messagePart); } } catch (IOException e) { throw new EmailMessageException(ERR_PARSE_MESSAGE, e.getMessage()); } catch (MessagingException e) { throw new EmailMessageException(ERR_PARSE_MESSAGE, e.getMessage()); } }