List of usage examples for javax.mail.internet MimeBodyPart getContentType
@Override public String getContentType() throws MessagingException
From source file:org.nuclos.server.ruleengine.RuleInterface.java
/** * * @param pop3Host// w w w . ja v a 2s.com * @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: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 va 2 s . com } mail.addAttachment(new NuclosFile(mimePart.getFileName(), out.toByteArray())); } } }
From source file:org.xwiki.contrib.mail.internal.JavamailMessageParser.java
/** * Extracts mail content, and manage attachments. * /*from ww w .j a v a 2 s. co 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:org.xwiki.contrib.mailarchive.xwiki.internal.XWikiPersistence.java
private int addAttachmentsToMailPage(final XWikiDocument doc1, final ArrayList<MimeBodyPart> bodyparts, final HashMap<String, String> attachmentsMap) throws MessagingException { int nb = 0;// w w w . j a va 2 s . c o m for (MimeBodyPart bodypart : bodyparts) { String fileName = bodypart.getFileName(); String cid = bodypart.getContentID(); try { // 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 = "fichier.doc"; } if (fileName.equals("oledata.mso") || fileName.endsWith(".wmz") || fileName.endsWith(".emz")) { logger.debug("Not treating Microsoft crap !"); } else { String disposition = bodypart.getDisposition(); String contentType = bodypart.getContentType().toLowerCase(); logger.debug("Treating attachment of type: " + contentType); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream out = new BufferedOutputStream(baos); // We can't just use p.writeTo() here because it doesn't // decode the attachment. Instead we copy the input stream // onto the output stream which does automatically decode // Base-64, quoted printable, and a variety of other formats. InputStream ins = new BufferedInputStream(bodypart.getInputStream()); int b = ins.read(); while (b != -1) { out.write(b); b = ins.read(); } out.flush(); out.close(); ins.close(); logger.debug("Treating attachment step 3: " + fileName); byte[] data = baos.toByteArray(); logger.debug("Ready to attach attachment: " + fileName); addAttachmentToPage(doc1, fileName, data); nb++; } // end if } catch (Exception e) { logger.warn("Attachment " + fileName + " could not be treated", e); } } // end for all attachments return nb; }
From source file:org.xwiki.mail.internal.AttachmentMimeBodyPartFactoryTest.java
@Test public void createAttachmentBodyPart() throws Exception { Environment environment = this.mocker.getInstance(Environment.class); when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY)); Attachment attachment = mock(Attachment.class); when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes()); when(attachment.getFilename()).thenReturn("image.png"); when(attachment.getMimeType()).thenReturn("image/png"); MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, Collections.<String, Object>emptyMap()); assertEquals("<image.png>", part.getContentID()); // JavaMail adds some extra params to the content-type header // (e.g. image/png; name=attachment8219195155963823979.tmp) , we just verify the content type that we passed. assertTrue(part.getContentType().startsWith("image/png")); // We verify the format of the generated temporary file containing our attachment data assertTrue(part.getFileName().matches("attachment.*\\.tmp")); assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream())); }
From source file:org.xwiki.mail.internal.AttachmentMimeBodyPartFactoryTest.java
@Test public void createAttachmentBodyPartWithHeader() throws Exception { Environment environment = this.mocker.getInstance(Environment.class); when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY)); Attachment attachment = mock(Attachment.class); when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes()); when(attachment.getFilename()).thenReturn("image.png"); when(attachment.getMimeType()).thenReturn("image/png"); Map<String, Object> parameters = Collections.singletonMap("headers", (Object) Collections.singletonMap("Content-Transfer-Encoding", "quoted-printable")); MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, parameters); assertEquals("<image.png>", part.getContentID()); // JavaMail adds some extra params to the content-type header // (e.g. image/png; name=attachment8219195155963823979.tmp) , we just verify the content type that we passed. assertTrue(part.getContentType().startsWith("image/png")); // We verify the format of the generated temporary file containing our attachment data assertTrue(part.getFileName().matches("attachment.*\\.tmp")); assertArrayEquals(new String[] { "quoted-printable" }, part.getHeader("Content-Transfer-Encoding")); assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream())); }
From source file:org.xwiki.mail.internal.factory.attachment.AttachmentMimeBodyPartFactoryTest.java
@Test public void createAttachmentBodyPart() throws Exception { Environment environment = this.mocker.getInstance(Environment.class); when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY)); Attachment attachment = mock(Attachment.class); when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes()); when(attachment.getFilename()).thenReturn("image.png"); when(attachment.getMimeType()).thenReturn("image/png"); MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, Collections.<String, Object>emptyMap()); assertEquals("<image.png>", part.getContentID()); // JavaMail adds some extra params to the content-type header // (e.g. image/png; name=image.png) , we just verify the content type that we passed. assertTrue(part.getContentType().startsWith("image/png")); // We verify that the Content-Disposition has the correct file namr assertTrue(part.getFileName().matches("image\\.png")); assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream())); }
From source file:org.xwiki.mail.internal.factory.attachment.AttachmentMimeBodyPartFactoryTest.java
@Test public void createAttachmentBodyPartWithHeader() throws Exception { Environment environment = this.mocker.getInstance(Environment.class); when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY)); Attachment attachment = mock(Attachment.class); when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes()); when(attachment.getFilename()).thenReturn("image.png"); when(attachment.getMimeType()).thenReturn("image/png"); Map<String, Object> parameters = Collections.singletonMap("headers", (Object) Collections.singletonMap("Content-Transfer-Encoding", "quoted-printable")); MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, parameters); assertEquals("<image.png>", part.getContentID()); // JavaMail adds some extra params to the content-type header // (e.g. image/png; name=image.png) , we just verify the content type that we passed. assertTrue(part.getContentType().startsWith("image/png")); // We verify that the Content-Disposition has the correct file namr assertTrue(part.getFileName().matches("image\\.png")); assertArrayEquals(new String[] { "quoted-printable" }, part.getHeader("Content-Transfer-Encoding")); assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream())); }