List of usage examples for javax.mail Multipart getBodyPart
public synchronized BodyPart getBodyPart(int index) throws MessagingException
From source file:org.apache.manifoldcf.crawler.connectors.email.EmailConnector.java
/** Process a set of documents. * This is the method that should cause each document to be fetched, processed, and the results either added * to the queue of documents for the current job, and/or entered into the incremental ingestion manager. * The document specification allows this class to filter what is done based on the job. * The connector will be connected before this method can be called. *@param documentIdentifiers is the set of document identifiers to process. *@param statuses are the currently-stored document versions for each document in the set of document identifiers * passed in above.//from w ww.ja va2 s. co m *@param activities is the interface this method should use to queue up new document references * and ingest documents. *@param jobMode is an integer describing how the job is being run, whether continuous or once-only. *@param usesDefaultAuthority will be true only if the authority in use for these documents is the default one. */ @Override public void processDocuments(String[] documentIdentifiers, IExistingVersions statuses, Specification spec, IProcessActivity activities, int jobMode, boolean usesDefaultAuthority) throws ManifoldCFException, ServiceInterruption { List<String> requiredMetadata = new ArrayList<String>(); for (int i = 0; i < spec.getChildCount(); i++) { SpecificationNode sn = spec.getChild(i); if (sn.getType().equals(EmailConfig.NODE_METADATA)) { String metadataAttribute = sn.getAttributeValue(EmailConfig.ATTRIBUTE_NAME); requiredMetadata.add(metadataAttribute); } } // Keep a cached set of open folders Map<String, Folder> openFolders = new HashMap<String, Folder>(); try { for (String documentIdentifier : documentIdentifiers) { String versionString = "_" + urlTemplate; // NOT empty; we need to make ManifoldCF understand that this is a document that never will change. // Check if we need to index if (!activities.checkDocumentNeedsReindexing(documentIdentifier, versionString)) continue; String compositeID = documentIdentifier; String version = versionString; String folderName = extractFolderNameFromDocumentIdentifier(compositeID); String id = extractEmailIDFromDocumentIdentifier(compositeID); String errorCode = null; String errorDesc = null; Long fileLengthLong = null; long startTime = System.currentTimeMillis(); try { try { Folder folder = openFolders.get(folderName); if (folder == null) { getSession(); OpenFolderThread oft = new OpenFolderThread(session, folderName); oft.start(); folder = oft.finishUp(); openFolders.put(folderName, folder); } if (Logging.connectors.isDebugEnabled()) Logging.connectors.debug("Email: Processing document identifier '" + compositeID + "'"); SearchTerm messageIDTerm = new MessageIDTerm(id); getSession(); SearchMessagesThread smt = new SearchMessagesThread(session, folder, messageIDTerm); smt.start(); Message[] message = smt.finishUp(); String msgURL = makeDocumentURI(urlTemplate, folderName, id); Message msg = null; for (Message msg2 : message) { msg = msg2; } if (msg == null) { // email was not found activities.deleteDocument(id); continue; } if (!activities.checkURLIndexable(msgURL)) { errorCode = activities.EXCLUDED_URL; errorDesc = "Excluded because of URL ('" + msgURL + "')"; activities.noDocument(id, version); continue; } long fileLength = msg.getSize(); if (!activities.checkLengthIndexable(fileLength)) { errorCode = activities.EXCLUDED_LENGTH; errorDesc = "Excluded because of length (" + fileLength + ")"; activities.noDocument(id, version); continue; } Date sentDate = msg.getSentDate(); if (!activities.checkDateIndexable(sentDate)) { errorCode = activities.EXCLUDED_DATE; errorDesc = "Excluded because of date (" + sentDate + ")"; activities.noDocument(id, version); continue; } String mimeType = "text/plain"; if (!activities.checkMimeTypeIndexable(mimeType)) { errorCode = activities.EXCLUDED_DATE; errorDesc = "Excluded because of mime type ('" + mimeType + "')"; activities.noDocument(id, version); continue; } RepositoryDocument rd = new RepositoryDocument(); rd.setFileName(msg.getFileName()); rd.setMimeType(mimeType); rd.setCreatedDate(sentDate); rd.setModifiedDate(sentDate); String subject = StringUtils.EMPTY; for (String metadata : requiredMetadata) { if (metadata.toLowerCase().equals(EmailConfig.EMAIL_TO)) { Address[] to = msg.getRecipients(Message.RecipientType.TO); String[] toStr = new String[to.length]; int j = 0; for (Address address : to) { toStr[j] = address.toString(); } rd.addField(EmailConfig.EMAIL_TO, toStr); } else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_FROM)) { Address[] from = msg.getFrom(); String[] fromStr = new String[from.length]; int j = 0; for (Address address : from) { fromStr[j] = address.toString(); } rd.addField(EmailConfig.EMAIL_TO, fromStr); } else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_SUBJECT)) { subject = msg.getSubject(); rd.addField(EmailConfig.EMAIL_SUBJECT, subject); } else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_BODY)) { Multipart mp = (Multipart) msg.getContent(); for (int k = 0, n = mp.getCount(); k < n; k++) { Part part = mp.getBodyPart(k); String disposition = part.getDisposition(); if ((disposition == null)) { MimeBodyPart mbp = (MimeBodyPart) part; if (mbp.isMimeType(EmailConfig.MIMETYPE_TEXT_PLAIN)) { rd.addField(EmailConfig.EMAIL_BODY, mbp.getContent().toString()); } else if (mbp.isMimeType(EmailConfig.MIMETYPE_HTML)) { rd.addField(EmailConfig.EMAIL_BODY, mbp.getContent().toString()); //handle html accordingly. Returns content with html tags } } } } else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_DATE)) { rd.addField(EmailConfig.EMAIL_DATE, sentDate.toString()); } else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_ATTACHMENT_ENCODING)) { Multipart mp = (Multipart) msg.getContent(); if (mp != null) { String[] encoding = new String[mp.getCount()]; for (int k = 0, n = mp.getCount(); k < n; k++) { Part part = mp.getBodyPart(k); String disposition = part.getDisposition(); if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) { encoding[k] = part.getFileName().split("\\?")[1]; } } rd.addField(EmailConfig.ENCODING_FIELD, encoding); } } else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_ATTACHMENT_MIMETYPE)) { Multipart mp = (Multipart) msg.getContent(); String[] MIMEType = new String[mp.getCount()]; for (int k = 0, n = mp.getCount(); k < n; k++) { Part part = mp.getBodyPart(k); String disposition = part.getDisposition(); if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) { MIMEType[k] = part.getContentType(); } } rd.addField(EmailConfig.MIMETYPE_FIELD, MIMEType); } } InputStream is = msg.getInputStream(); try { rd.setBinary(is, fileLength); activities.ingestDocumentWithException(id, version, msgURL, rd); errorCode = "OK"; fileLengthLong = new Long(fileLength); } finally { is.close(); } } catch (InterruptedException e) { throw new ManifoldCFException(e.getMessage(), ManifoldCFException.INTERRUPTED); } catch (MessagingException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = e.getMessage(); handleMessagingException(e, "processing email"); } catch (IOException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = e.getMessage(); handleIOException(e, "processing email"); throw new ManifoldCFException(e.getMessage(), e); } } catch (ManifoldCFException e) { if (e.getErrorCode() == ManifoldCFException.INTERRUPTED) errorCode = null; throw e; } finally { if (errorCode != null) activities.recordActivity(new Long(startTime), EmailConfig.ACTIVITY_FETCH, fileLengthLong, documentIdentifier, errorCode, errorDesc, null); } } } finally { for (Folder f : openFolders.values()) { try { CloseFolderThread cft = new CloseFolderThread(session, f); cft.start(); cft.finishUp(); } catch (InterruptedException e) { throw new ManifoldCFException(e.getMessage(), ManifoldCFException.INTERRUPTED); } catch (MessagingException e) { handleMessagingException(e, "closing folders"); } } } }
From source file:org.socraticgrid.displaymaildata.DisplayMailDataHandler.java
/** * Extract/format mail message content./* w w w . ja v a2 s .c om*/ * * @param msg * @return */ private String fetchMsgContent(Message msg) throws Exception { String content = null; String html = null; String text = null; if (msg.isMimeType("multipart/*")) { Multipart mp = (Multipart) msg.getContent(); // the content was not fetched from the server // parse each Part for (int i = 0; i < mp.getCount(); i++) { Part inner_part = mp.getBodyPart(i); if (inner_part.isMimeType("text/plain")) { text = (String) inner_part.getContent(); System.out.println("TEXT=\n" + text); } else if (inner_part.isMimeType("text/html")) { html = (String) inner_part.getContent(); System.out.println("HTML=\n" + content); } } } else if (msg.isMimeType("text/plain")) { text = (String) msg.getContent(); System.out.println("TEXT only=\n" + text); } if (!CommonUtil.strNullorEmpty(html)) { content = html; } else { content = text; } return content; }
From source file:edu.stanford.muse.email.EmailFetcherStats.java
/** * this method returns the text content of the message as a list of strings * // each element of the list could be the content of a multipart message * // m is the top level subject// w w w. j av a 2s. c om * // p is the specific part that we are processing (p could be == m) * also sets up names of attachments (though it will not download the * attachment unless downloadAttachments is true) */ private List<String> processMessagePart(int messageNum, Message m, Part p, List<Blob> attachmentsList) throws MessagingException, IOException { List<String> list = new ArrayList<String>(); // return list if (p == null) { dataErrors.add("part is null: " + folder_name() + " idx " + messageNum); return list; } if (p == m && p.isMimeType("text/html")) { /* String s = "top level part is html! message:" + m.getSubject() + " " + m.getDescription(); dataErrors.add(s); */ // we don't normally expect the top-level part to have content-type text/html // but we saw this happen on some sample archives pst -> emailchemy. so allow it and handle it by parsing the html String html = (String) p.getContent(); String text = Util.unescapeHTML(html); org.jsoup.nodes.Document doc = Jsoup.parse(text); StringBuilder sb = new StringBuilder(); HTMLUtils.extractTextFromHTML(doc.body(), sb); list.add(sb.toString()); return list; } if (p.isMimeType("text/plain")) { //make sure, p is not wrongly labelled as plain text. Enumeration headers = p.getAllHeaders(); boolean dirty = false; if (headers != null) while (headers.hasMoreElements()) { Header h = (Header) headers.nextElement(); String name = h.getName(); String value = h.getValue(); if (name != null && value != null) { if (name.equals("Content-transfer-encoding") && value.equals("base64")) { dirty = true; break; } } } String fname = p.getFileName(); if (fname != null) { int idx = fname.lastIndexOf('.'); if ((idx < fname.length()) && (idx >= 0)) { String extension = fname.substring(idx); //anything extension other than .txt is suspicious. if (!extension.equals(".txt")) dirty = true; } } if (dirty) { dataErrors.add("Dirty message part, has conflicting message part headers." + folder_name() + " Message# " + messageNum); return list; } log.debug("Message part with content type text/plain"); String content; String type = p.getContentType(); // new InputStreamReader(p.getInputStream(), "UTF-8"); try { // if forced encoding is set, we read the string with that encoding, otherwise we just use whatever p.getContent gives us if (FORCED_ENCODING != null) { byte b[] = Util.getBytesFromStream(p.getInputStream()); content = new String(b, FORCED_ENCODING); } else content = (String) p.getContent(); } catch (UnsupportedEncodingException uee) { dataErrors.add("Unsupported encoding: " + folder_name() + " Message #" + messageNum + " type " + type + ", using brute force conversion"); // a particularly nasty issue:javamail can't handle utf-7 encoding which is common with hotmail and exchange servers. // we're using the workaround suggested on this page: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4304013 // though it may be better to consider official support for utf-7 or other encodings. // TOFIX: I get an exception for utfutf8-encoding which has a base64 encoding embedded on it. // Unsupported encoding: gmail-sent Message #10477 type text/plain; charset=x-utf8utf8; name="newyorker.txt", // the hack below doesn't work for it. ByteArrayOutputStream bao = new ByteArrayOutputStream(); p.writeTo(bao); content = bao.toString(); } list.add(content); } else if (p.isMimeType("multipart/*") || p.isMimeType("message/rfc822")) { // rfc822 mime type is for embedded mbox format or some such (appears for things like // forwarded messages). the content appears to be just a multipart. Object o = p.getContent(); if (o instanceof Multipart) { Multipart allParts = (Multipart) o; if (p.isMimeType("multipart/alternative")) { // this is an alternative mime type. v common case to have text and html alternatives // so just process the text part if there is one, and avoid fetching the alternatives. // useful esp. because many ordinary messages are alternative: text and html and we don't want to fetch the html. // revisit in future we want to retain the html alternative for display purposes Part[] parts = new Part[allParts.getCount()]; for (int i = 0; i < parts.length; i++) parts[i] = allParts.getBodyPart(i); for (int i = 0; i < parts.length; i++) { Part thisPart = parts[i]; if (thisPart.isMimeType("text/plain")) { // common case, return quickly list.add((String) thisPart.getContent()); log.debug("Multipart/alternative with content type text/plain"); return list; } } // no text part, let's look for an html part. this happens for html parts. for (int i = 0; i < allParts.getCount(); i++) { Part thisPart = parts[i]; if (thisPart.isMimeType("text/html")) { // common case, return quickly String html = (String) thisPart.getContent(); String text = Util.unescapeHTML(html); org.jsoup.nodes.Document doc = Jsoup.parse(text); StringBuilder sb = new StringBuilder(); HTMLUtils.extractTextFromHTML(doc.body(), sb); list.add(sb.toString()); log.debug("Multipart/alternative with content type text/html"); return list; } } // no text or html part. hmmm... blindly process the first part only if (allParts.getCount() >= 1) list.addAll(processMessagePart(messageNum, m, allParts.getBodyPart(0), attachmentsList)); } else { // process it like a regular multipart for (int i = 0; i < allParts.getCount(); i++) { BodyPart bp = allParts.getBodyPart(i); list.addAll(processMessagePart(messageNum, m, bp, attachmentsList)); } } } else if (o instanceof Part) list.addAll(processMessagePart(messageNum, m, (Part) o, attachmentsList)); else dataErrors.add("Unhandled part content, " + folder_name() + " Message #" + messageNum + "Java type: " + o.getClass() + " Content-Type: " + p.getContentType()); } else { try { // do attachments only if downloadAttachments is set. // some apps do not need attachments, so this saves some time. // however, it seems like a lot of time is taken in imap prefetch, which gets attachments too? if (fetchConfig.downloadAttachments) handleAttachments(messageNum, m, p, list, attachmentsList); } catch (Exception e) { dataErrors.add("Ignoring attachment for " + folder_name() + " Message #" + messageNum + ": " + Util.stackTrace(e)); } } return list; }
From source file:com.flexoodb.common.FlexUtils.java
/** * method to obtain a FileContainer containing the file wrapped in a mimemessage. * * @param data the mimemessage in a byte array. * @return a FileContainer containing the file. */// w ww . j av a 2 s . c o m static public FileContainer extractFileFromMimeMessage(byte[] data) throws Exception { FileContainer fc = null; MimeMessage message = new MimeMessage(null, new ByteArrayInputStream(data)); Object content = message.getContent(); if (content instanceof Multipart) { Multipart multipart = (Multipart) content; for (int i = 0, n = multipart.getCount(); i < n; i++) { Part part = multipart.getBodyPart(i); String disposition = part.getDisposition(); if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE)))) { if (part.getFileName() != null) { fc = new FileContainer(part.getFileName(), part.getFileName(), getBytesFromInputStream(part.getInputStream())); break; } } } } return fc; }
From source file:org.nuclos.server.ruleengine.RuleInterface.java
private void getAttachments(Multipart multipart, NuclosMail mail) throws MessagingException, IOException { for (int i = 0; i < multipart.getCount(); i++) { Part part = multipart.getBodyPart(i); String disposition = part.getDisposition(); MimeBodyPart mimePart = (MimeBodyPart) part; logger.debug("Disposition: " + disposition + "; Part ContentType: " + mimePart.getContentType()); if (part.getContent() instanceof Multipart) { logger.debug("Start child Multipart."); Multipart childmultipart = (Multipart) part.getContent(); getAttachments(childmultipart, mail); logger.debug("Finished child Multipart."); } else if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) { logger.debug("Attachment: " + mimePart.getFileName()); InputStream in = mimePart.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[8192]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len);// w w w. j a va2 s . c o m } mail.addAttachment(new NuclosFile(mimePart.getFileName(), out.toByteArray())); } } }
From source file:org.nuclos.server.ruleengine.RuleInterface.java
/** * * @param pop3Host/*w w w . jav a2 s . co m*/ * @param pop3Port * @param pop3User * @param pop3Password * @param remove * @return * @throws NuclosFatalRuleException */ public List<NuclosMail> getMails(String pop3Host, String pop3Port, final String pop3User, final String pop3Password, boolean remove) throws NuclosFatalRuleException { try { Properties properties = new Properties(); properties.setProperty("mail.pop3.host", pop3Host); properties.setProperty("mail.pop3.port", pop3Port); properties.setProperty("mail.pop3.auth", "true"); properties.setProperty("mail.pop3.socketFactory.class", "javax.net.DefaultSocketFactory"); Session session = Session.getInstance(properties, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(pop3User, pop3Password); } }); session.setDebug(true); Store store = session.getStore("pop3"); store.connect(); Folder folder = store.getFolder("INBOX"); if (remove) { folder.open(Folder.READ_WRITE); } else { folder.open(Folder.READ_ONLY); } List<NuclosMail> result = new ArrayList<NuclosMail>(); Message message[] = folder.getMessages(); for (int i = 0; i < message.length; i++) { Message m = message[i]; NuclosMail mail = new NuclosMail(); logger.debug("Received mail: From: " + Arrays.toString(m.getFrom()) + "; To: " + Arrays.toString(m.getAllRecipients()) + "; ContentType: " + m.getContentType() + "; Subject: " + m.getSubject() + "; Sent: " + m.getSentDate()); Address[] senders = m.getFrom(); if (senders.length == 1 && senders[0] instanceof InternetAddress) { mail.setFrom(((InternetAddress) senders[0]).getAddress()); } else { mail.setFrom(Arrays.toString(m.getFrom())); } mail.setTo(Arrays.toString(m.getRecipients(RecipientType.TO))); mail.setSubject(m.getSubject()); if (m.isMimeType("text/plain")) { mail.setMessage((String) m.getContent()); } else { Multipart mp = (Multipart) m.getContent(); for (int j = 0; j < mp.getCount(); j++) { Part part = mp.getBodyPart(j); String disposition = part.getDisposition(); MimeBodyPart mimePart = (MimeBodyPart) part; logger.debug( "Disposition: " + disposition + "; Part ContentType: " + mimePart.getContentType()); if (disposition == null && (mimePart.isMimeType("text/plain") || mimePart.isMimeType("text/html"))) { mail.setMessage((String) mimePart.getDataHandler().getContent()); } } getAttachments(mp, mail); } result.add(mail); if (remove) { m.setFlag(Flags.Flag.DELETED, true); } } if (remove) { folder.close(true); } else { folder.close(false); } store.close(); return result; } catch (Exception e) { throw new NuclosFatalRuleException(e); } }
From source file:com.sonicle.webtop.mail.Service.java
private boolean appendReplyParts(Part p, StringBuffer htmlsb, StringBuffer textsb, ArrayList<String> attnames) throws MessagingException, IOException { boolean isHtml = false; String disp = p.getDisposition(); if (disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT) && !p.isMimeType("message/*")) { if (attnames != null) {// w w w .j a v a 2s. c o m String id[] = p.getHeader("Content-ID"); if (id == null || id[0] == null) { String filename = p.getFileName(); if (filename != null) { if (filename.startsWith("<")) { filename = filename.substring(1); } if (filename.endsWith(">")) { filename = filename.substring(0, filename.length() - 1); } } if (filename != null) { attnames.add(filename); } } } return false; } if (p.isMimeType("text/html")) { //String htmlcontent=(String)p.getContent(); String htmlcontent = getTextContentAsString(p); textsb.append(MailUtils.htmlToText(MailUtils.htmlunescapesource(htmlcontent))); htmlsb.append(MailUtils.htmlescapefixsource(/*getBodyInnerHtml(*/htmlcontent/*)*/)); isHtml = true; } else if (p.isMimeType("text/plain")) { String content = getTextContentAsString(p); textsb.append(content); htmlsb.append(startpre + MailUtils.htmlescape(content) + endpre); isHtml = false; } else if (p.isMimeType("message/delivery-status") || p.isMimeType("message/disposition-notification")) { InputStream is = (InputStream) p.getContent(); char cbuf[] = new char[8000]; byte bbuf[] = new byte[8000]; int n = 0; htmlsb.append(startpre); while ((n = is.read(bbuf)) >= 0) { if (n > 0) { for (int i = 0; i < n; ++i) { cbuf[i] = (char) bbuf[i]; } textsb.append(cbuf); htmlsb.append(MailUtils.htmlescape(new String(cbuf))); } } htmlsb.append(endpre); is.close(); isHtml = false; } else if (p.isMimeType("multipart/alternative")) { Multipart mp = (Multipart) p.getContent(); Part bestPart = null; for (int i = 0; i < mp.getCount(); ++i) { Part part = mp.getBodyPart(i); if (part.isMimeType("multipart/*")) { isHtml = appendReplyParts(part, htmlsb, textsb, attnames); if (isHtml) { bestPart = null; break; } } else if (part.isMimeType("text/html")) { bestPart = part; break; } else if (bestPart == null && part.isMimeType("text/plain")) { bestPart = part; } else if (bestPart == null && part.isMimeType("message/*")) { bestPart = part; } } if (bestPart != null) { isHtml = appendReplyParts(bestPart, htmlsb, textsb, attnames); } } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); ++i) { if (appendReplyParts(mp.getBodyPart(i), htmlsb, textsb, attnames)) { isHtml = true; } } } else if (p.isMimeType("message/*")) { Object content = p.getContent(); if (appendReplyParts((MimeMessage) content, htmlsb, textsb, attnames)) { isHtml = true; } } else { } textsb.append('\n'); textsb.append('\n'); return isHtml; }