List of usage examples for javax.mail Multipart getCount
public synchronized int getCount() throws MessagingException
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./*from w w w.j a va 2s . 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.application.djigzo.james.mailets.PDFEncryptTest.java
private void checkEncryption(MimeMessage message, String password, boolean hasReplyLink) throws Exception { /*/*w w w . j a v a2 s .c o m*/ * The message should be a mime multipart mixed with two parts. The first part should be readable text * and the second part should be the encrypted PDF */ assertTrue(message.isMimeType("multipart/mixed")); Multipart mp = (Multipart) message.getContent(); assertEquals(2, mp.getCount()); BodyPart textPart = mp.getBodyPart(0); assertTrue(textPart.isMimeType("text/plain")); BodyPart pdfPart = mp.getBodyPart(1); assertTrue(pdfPart.isMimeType("application/pdf")); PdfReader reader = new PdfReader(pdfPart.getInputStream(), password.getBytes(CharacterEncoding.US_ASCII)); String firstPageContent = new String(reader.getPageContent(1), CharacterEncoding.US_ASCII); /* * We just check whether the raw content contains (Reply) or not. */ if (hasReplyLink) { assertTrue(firstPageContent.contains("(Reply)")); assertTrue(((String) textPart.getContent()).contains("reply URL: http://127.0.0.1?env=")); } else { assertFalse(firstPageContent.contains("(Reply)")); } }
From source file:mitm.application.djigzo.james.mailets.PDFEncryptTest.java
@Test public void testEncryptPDFFromPersonalUTF8() throws Exception { MockMailetConfig mailetConfig = new MockMailetConfig("test"); SendMailEventListenerImpl listener = new SendMailEventListenerImpl(); mailetConfig.getMailetContext().setSendMailEventListener(listener); PDFEncrypt mailet = new PDFEncrypt(); mailetConfig.setInitParameter("template", "encrypted-pdf.ftl"); mailetConfig.setInitParameter("encryptedProcessor", "encryptedProcessor"); mailetConfig.setInitParameter("notEncryptedProcessor", "notEncryptedProcessor"); mailetConfig.setInitParameter("passwordMode", "single"); mailetConfig.setInitParameter("passThrough", "false"); mailet.init(mailetConfig);/* www .j a va 2s . c om*/ MockMail mail = new MockMail(); MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/normal-message-with-attach.eml")); message.setFrom(new InternetAddress("test@example.com", "=?UTF-8?B?w6TDtsO8IMOEw5bDnA==?=")); mail.setMessage(message); Set<MailAddress> recipients = new HashSet<MailAddress>(); recipients.add(new MailAddress("m.brinkers@pobox.com")); recipients.add(new MailAddress("123@example.com")); mail.setRecipients(recipients); mail.setSender(new MailAddress("sender@example.com")); // password is test when encrypted with password 'djigzo' new DjigzoMailAttributesImpl(mail).setEncryptedPassword(Base64.decodeBase64(MiscStringUtils .toAsciiBytes("lklfx6SWxIkAAAAQ1VTbMJjznNZjVvdggckSPQAACAAAAAAQKAxcw630UmyVhyZPiW9xhg=="))); mailet.service(mail); MailUtils.validateMessage(mail.getMessage()); TestUtils.saveMessages(tempDir, "testEncryptPDFFromPersonalUTF8", listener.getMessages()); assertEquals(1, listener.getMessages().size()); assertEquals("encryptedProcessor", listener.getStates().get(0)); assertEquals(2, listener.getRecipients().get(0).size()); assertTrue(listener.getRecipients().get(0).contains(new MailAddress("123@example.com"))); assertTrue(listener.getRecipients().get(0).contains(new MailAddress("m.brinkers@pobox.com"))); assertEquals("sender@example.com", listener.getSenders().get(0).toString()); assertEquals(Mail.GHOST, mail.getState()); assertNotNull(listener.getMessages().get(0)); assertTrue(message != listener.getMessages().get(0)); MimeMessage encrypted = listener.getMessages().get(0); assertTrue(encrypted.isMimeType("multipart/mixed")); Multipart mp = (Multipart) encrypted.getContent(); assertEquals(2, mp.getCount()); BodyPart messagePart = mp.getBodyPart(0); BodyPart pdfPart = mp.getBodyPart(1); assertTrue(messagePart.isMimeType("text/plain")); assertTrue(pdfPart.isMimeType("application/pdf")); // check if the body contains (which is the decoded from personal name) String text = (String) messagePart.getContent(); assertTrue(text.contains(" ")); MailUtils.validateMessage(listener.getMessages().get(0)); }
From source file:org.sakaiproject.email.impl.BasicEmailService.java
protected void sendMessageAndLog(InternetAddress from, InternetAddress[] to, String subject, Map<RecipientType, InternetAddress[]> headerTo, long start, MimeMessage msg, Session session) throws MessagingException { long preSend = 0; if (M_log.isDebugEnabled()) preSend = System.currentTimeMillis(); if (allowTransport) { msg.saveChanges();// w w w . j a va 2 s . co m Transport transport = session.getTransport(protocol); if (m_smtpUser != null && m_smtpPassword != null) transport.connect(m_smtp, m_smtpUser, m_smtpPassword); else transport.connect(); transport.sendMessage(msg, to); transport.close(); } long end = 0; if (M_log.isDebugEnabled()) end = System.currentTimeMillis(); if (M_log.isInfoEnabled()) { StringBuilder buf = new StringBuilder(); buf.append("Email.sendMail: from: "); buf.append(from); buf.append(" subject: "); buf.append(subject); buf.append(" to:"); for (int i = 0; i < to.length; i++) { buf.append(" "); buf.append(to[i]); } if (headerTo != null) { if (headerTo.containsKey(RecipientType.TO)) { buf.append(" headerTo{to}:"); InternetAddress[] headerToTo = headerTo.get(RecipientType.TO); for (int i = 0; i < headerToTo.length; i++) { buf.append(" "); buf.append(headerToTo[i]); } } if (headerTo.containsKey(RecipientType.CC)) { buf.append(" headerTo{cc}:"); InternetAddress[] headerToCc = headerTo.get(RecipientType.CC); for (int i = 0; i < headerToCc.length; i++) { buf.append(" "); buf.append(headerToCc[i]); } } if (headerTo.containsKey(RecipientType.BCC)) { buf.append(" headerTo{bcc}:"); InternetAddress[] headerToBcc = headerTo.get(RecipientType.BCC); for (int i = 0; i < headerToBcc.length; i++) { buf.append(" "); buf.append(headerToBcc[i]); } } } try { if (msg.getContent() instanceof Multipart) { Multipart parts = (Multipart) msg.getContent(); buf.append(" with ").append(parts.getCount() - 1).append(" attachments"); } } catch (IOException ioe) { } if (M_log.isDebugEnabled()) { buf.append(" time: "); buf.append("" + (end - start)); buf.append(" in send: "); buf.append("" + (end - preSend)); } M_log.info(buf.toString()); } }
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./*w w w .j a v a 2 s. c o 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./*from ww w. j av a 2s .c o m*/ * * @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 ww. j a v a2s . c o m * // 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. */// ww w .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 ww .j a va 2 s .c o m } mail.addAttachment(new NuclosFile(mimePart.getFileName(), out.toByteArray())); } } }
From source file:org.nuclos.server.ruleengine.RuleInterface.java
/** * * @param pop3Host/*from www. ja va 2s. c o 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); } }