List of usage examples for javax.mail Part INLINE
String INLINE
To view the source code for javax.mail Part INLINE.
Click Source Link
From source file:Main.java
public static void main(String[] args) throws Exception { URLName server = new URLName("protocol://username@host/foldername"); Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator()); Folder folder = session.getFolder(server); if (folder == null) { System.out.println("Folder " + server.getFile() + " not found."); System.exit(1);/*from w w w . j a va2 s . c o m*/ } folder.open(Folder.READ_ONLY); Message[] messages = folder.getMessages(); for (int i = 0; i < messages.length; i++) { System.out.println(messages[i].getSize() + " bytes long."); System.out.println(messages[i].getLineCount() + " lines."); String disposition = messages[i].getDisposition(); if (disposition == null) { ; // do nothing } else if (disposition.equals(Part.INLINE)) { System.out.println("This part should be displayed inline"); } else if (disposition.equals(Part.ATTACHMENT)) { System.out.println("This part is an attachment"); String fileName = messages[i].getFileName(); System.out.println("The file name of this attachment is " + fileName); } String description = messages[i].getDescription(); if (description != null) { System.out.println("The description of this message is " + description); } } folder.close(false); }
From source file:Main.java
public static boolean isContainAttachment(Part part) throws MessagingException, IOException { boolean flag = false; if (part.isMimeType("multipart/*")) { MimeMultipart multipart = (MimeMultipart) part.getContent(); int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); String disp = bodyPart.getDisposition(); if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { flag = true;//from w w w. ja v a 2 s . c o m } else if (bodyPart.isMimeType("multipart/*")) { flag = isContainAttachment(bodyPart); } else { String contentType = bodyPart.getContentType(); if (contentType.indexOf("application") != -1) { flag = true; } if (contentType.indexOf("name") != -1) { flag = true; } } if (flag) break; } } else if (part.isMimeType("message/rfc822")) { flag = isContainAttachment((Part) part.getContent()); } return flag; }
From source file:it.greenvulcano.gvesb.virtual.utils.MimeMessageHelper.java
public static Body getMessageBody(MimeMessage message, String mimeType) { try {/* ww w. jav a 2s .c o m*/ if (message.getContent() instanceof Multipart) { Multipart multipartMessage = (Multipart) message.getContent(); for (int i = 0; i < multipartMessage.getCount(); i++) { BodyPart bodyPart = multipartMessage.getBodyPart(i); if (bodyPart.isMimeType(mimeType) && (Part.INLINE.equalsIgnoreCase(bodyPart.getDisposition()) || Objects.isNull(bodyPart.getDisposition()))) { return new Body(bodyPart.getContentType(), bodyPart.getContent().toString()); } } } else { return new Body(message.getContentType(), message.getContent().toString()); } } catch (Exception e) { // do nothing } return null; }
From source file:ch.algotrader.util.mail.EmailTransformer.java
/** * Parses any {@link Multipart} instances that contains attachments * * Will create the respective {@link EmailFragment}s representing those attachments. * @throws MessagingException/* w ww . j a v a 2s. c o m*/ */ public void handleMultipart(Multipart multipart, List<EmailFragment> emailFragments) throws MessagingException { final int count = multipart.getCount(); for (int i = 0; i < count; i++) { BodyPart bodyPart = multipart.getBodyPart(i); String filename = bodyPart.getFileName(); String disposition = bodyPart.getDisposition(); if (filename == null && bodyPart instanceof MimeBodyPart) { filename = ((MimeBodyPart) bodyPart).getContentID(); } if (disposition == null) { //ignore message body } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE)) { try { try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); BufferedInputStream bis = new BufferedInputStream(bodyPart.getInputStream())) { IOUtils.copy(bis, bos); emailFragments.add(new EmailFragment(filename, bos.toByteArray())); if (LOGGER.isInfoEnabled()) { LOGGER.info(String.format("processing file: %s", new Object[] { filename })); } } } catch (IOException e) { throw new MessagingException("error processing streams", e); } } else { throw new MessagingException("unkown disposition " + disposition); } } }
From source file:de.elomagic.mag.AbstractTest.java
protected MimeMessage createMimeMessage(String filename) throws Exception { MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent("This is some text to be displayed inline", "text/plain"); textPart.setDisposition(Part.INLINE); MimeBodyPart binaryPart = new MimeBodyPart(); InputStream in = getClass().getResourceAsStream("/TestFile.pdf"); binaryPart.setContent(getOriginalMailAttachment(), "application/pdf"); binaryPart.setDisposition(Part.ATTACHMENT); binaryPart.setFileName(filename);/*from www .java2 s .co m*/ Multipart mp = new MimeMultipart(); mp.addBodyPart(textPart); mp.addBodyPart(binaryPart); MimeMessage message = new MimeMessage(greenMail.getImap().createSession()); message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress("mailuser@localhost.com") }); message.setSubject("Another test mail subject"); message.setContent(mp); // return message; // GreenMailUtil.createTextEmail("to@localhost.com", "from@localhost.com", "subject", "body", greenMail.getImap().getServerSetup()); }
From source file:com.silverpeas.mailinglist.service.job.TestMessageCheckerWithStubs.java
@Test public void testCheckNewMessages() throws MessagingException, IOException { org.jvnet.mock_javamail.Mailbox.clearAll(); MessageChecker messageChecker = getMessageChecker(); messageChecker.removeListener("componentId"); messageChecker.removeListener("thesimpsons@silverpeas.com"); messageChecker.removeListener("theflanders@silverpeas.com"); StubMessageListener mockListener1 = new StubMessageListener("thesimpsons@silverpeas.com"); StubMessageListener mockListener2 = new StubMessageListener("theflanders@silverpeas.com"); messageChecker.addMessageListener(mockListener1); messageChecker.addMessageListener(mockListener2); MimeMessage mail = new MimeMessage(messageChecker.getMailSession()); InternetAddress bart = new InternetAddress("bart.simpson@silverpeas.com"); InternetAddress theSimpsons = new InternetAddress("thesimpsons@silverpeas.com"); mail.addFrom(new InternetAddress[] { bart }); mail.addRecipient(RecipientType.TO, theSimpsons); mail.setSubject("Plain text Email test with attachment"); MimeBodyPart attachment = new MimeBodyPart( TestMessageCheckerWithStubs.class.getResourceAsStream("lemonde.html")); attachment.setDisposition(Part.INLINE); attachment.setFileName("lemonde.html"); MimeBodyPart body = new MimeBodyPart(); body.setText(textEmailContent);/*from w w w. j av a2 s. c o m*/ Multipart multiPart = new MimeMultipart(); multiPart.addBodyPart(body); multiPart.addBodyPart(attachment); mail.setContent(multiPart); mail.setSentDate(new Date()); Date sentDate1 = new Date(mail.getSentDate().getTime()); Transport.send(mail); mail = new MimeMessage(messageChecker.getMailSession()); bart = new InternetAddress("bart.simpson@silverpeas.com"); theSimpsons = new InternetAddress("thesimpsons@silverpeas.com"); mail.addFrom(new InternetAddress[] { bart }); mail.addRecipient(RecipientType.TO, theSimpsons); mail.setSubject("Plain text Email test"); mail.setText(textEmailContent); mail.setSentDate(new Date()); Date sentDate2 = new Date(mail.getSentDate().getTime()); Transport.send(mail); //Unauthorized email mail = new MimeMessage(messageChecker.getMailSession()); bart = new InternetAddress("marge.simpson@silverpeas.com"); theSimpsons = new InternetAddress("thesimpsons@silverpeas.com"); mail.addFrom(new InternetAddress[] { bart }); mail.addRecipient(RecipientType.TO, theSimpsons); mail.setSubject("Plain text Email test"); mail.setText(textEmailContent); mail.setSentDate(new Date()); Transport.send(mail); assertThat(org.jvnet.mock_javamail.Mailbox.get("thesimpsons@silverpeas.com").size(), is(3)); messageChecker.checkNewMessages(new Date()); assertThat(mockListener2.getMessageEvent(), is(nullValue())); MessageEvent event = mockListener1.getMessageEvent(); assertThat(event, is(notNullValue())); assertThat(event.getMessages(), is(notNullValue())); assertThat(event.getMessages(), hasSize(2)); Message message = event.getMessages().get(0); assertThat(message.getSender(), is("bart.simpson@silverpeas.com")); assertThat(message.getTitle(), is("Plain text Email test with attachment")); assertThat(message.getBody(), is(textEmailContent)); assertThat(message.getSummary(), is(textEmailContent.substring(0, 200))); assertThat(message.getSentDate().getTime(), is(sentDate1.getTime())); assertThat(message.getAttachmentsSize(), greaterThan(0L)); assertThat(message.getAttachments(), hasSize(1)); String path = MessageFormat.format(theSimpsonsAttachmentPath, new Object[] { messageChecker.getMailProcessor().replaceSpecialChars(message.getMessageId()) }); Attachment attached = message.getAttachments().iterator().next(); assertThat(attached.getPath(), is(path)); assertThat(message.getComponentId(), is("thesimpsons@silverpeas.com")); message = event.getMessages().get(1); assertThat(message.getSender(), is("bart.simpson@silverpeas.com")); assertThat(message.getTitle(), is("Plain text Email test")); assertThat(message.getBody(), is(textEmailContent)); assertThat(message.getSummary(), is(textEmailContent.substring(0, 200))); assertThat(message.getAttachmentsSize(), is(0L)); assertThat(message.getAttachments(), hasSize(0)); assertThat(message.getComponentId(), is("thesimpsons@silverpeas.com")); assertThat(message.getSentDate().getTime(), is(sentDate2.getTime())); }
From source file:org.elasticsearch.river.email.EmailToJson.java
public static List<AttachmentInfo> saveAttachmentToWeedFs(Part message, List<AttachmentInfo> attachments, EmailRiverConfig config)//w ww .ja v a 2 s. co m throws UnsupportedEncodingException, MessagingException, FileNotFoundException, IOException { if (attachments == null) { attachments = new ArrayList<AttachmentInfo>(); } boolean hasAttachment = false; try { hasAttachment = isContainAttachment(message); } catch (MessagingException e) { logger.error("save attachment", e); } catch (IOException e) { logger.error("save attachment", e); } if (hasAttachment) { if (message.isMimeType("multipart/*")) { Multipart multipart = (Multipart) message.getContent(); int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); String disp = bodyPart.getDisposition(); if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { InputStream is = bodyPart.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = -1; while ((len = bis.read()) != -1) { bos.write(len); } bos.close(); bis.close(); byte[] data = bos.toByteArray(); String fileId = uploadFileToWeedfs(data, config); if (fileId != null) { AttachmentInfo info = new AttachmentInfo(); info.fileId = fileId; info.fileName = decodeText(bodyPart.getFileName()); info.fileSize = data.length; attachments.add(info); } } else if (bodyPart.isMimeType("multipart/*")) { attachments.addAll(saveAttachmentToWeedFs(bodyPart, attachments, config)); } else { String contentType = bodyPart.getContentType(); if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) { attachments.addAll(saveAttachmentToWeedFs(bodyPart, attachments, config)); } } } } else if (message.isMimeType("message/rfc822")) { attachments.addAll(saveAttachmentToWeedFs(message, attachments, config)); } } return attachments; }
From source file:org.nuxeo.cm.mail.actionpipe.ExtractMessageInformation.java
@SuppressWarnings("unchecked") protected static void getAttachmentParts(Part p, String defaultFilename, MimetypeRegistry mimeService, ExecutionContext context) throws MessagingException, IOException { String filename = getFilename(p, defaultFilename); List<Blob> blobs = (List<Blob>) context.get(ATTACHMENTS_KEY); if (!p.isMimeType("multipart/*")) { String disp = p.getDisposition(); // no disposition => consider it may be body if (disp == null && !context.containsKey(BODY_KEY)) { // this will need to be parsed context.put(BODY_KEY, p.getContent()); } else if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || (disp.equalsIgnoreCase(Part.INLINE) && !(p.isMimeType("text/*") || p.isMimeType("image/*"))))) { log.debug("Saving attachment to file " + filename); Blob blob = Blobs.createBlob(p.getInputStream()); String mime = DEFAULT_BINARY_MIMETYPE; try { if (mimeService != null) { ContentType contentType = new ContentType(p.getContentType()); mime = mimeService.getMimetypeFromFilenameAndBlobWithDefault(filename, blob, contentType.getBaseType()); }/*from w w w . j a v a 2 s. co m*/ } catch (MimetypeDetectionException e) { log.error(e); } blob.setMimeType(mime); blob.setFilename(filename); blobs.add(blob); // for debug // File f = new File(filename); // ((MimeBodyPart) p).saveFile(f); log.debug("---------------------------"); } else { log.debug(String.format("Ignoring part with type %s", p.getContentType())); } } if (p.isMimeType("multipart/*")) { log.debug("This is a Multipart"); log.debug("---------------------------"); Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) { getAttachmentParts(mp.getBodyPart(i), defaultFilename, mimeService, context); } } else if (p.isMimeType(MESSAGE_RFC822_MIMETYPE)) { log.debug("This is a Nested Message"); log.debug("---------------------------"); getAttachmentParts((Part) p.getContent(), defaultFilename, mimeService, context); } }
From source file:com.haulmont.cuba.core.app.EmailSender.java
protected MimeBodyPart createAttachmentPart(SendingAttachment attachment) throws MessagingException { DataSource source = new MyByteArrayDataSource(attachment.getContent()); String mimeType = FileTypesHelper.getMIMEType(attachment.getName()); String encodedFileName = encodeAttachmentName(attachment); String contentId = attachment.getContentId(); if (contentId == null) { contentId = encodedFileName;/*w ww . j ava 2 s . c o m*/ } String disposition = attachment.getDisposition() != null ? attachment.getDisposition() : Part.INLINE; String charset = MimeUtility.mimeCharset( attachment.getEncoding() != null ? attachment.getEncoding() : StandardCharsets.UTF_8.name()); String contentTypeValue = String.format("%s; charset=%s; name=\"%s\"", mimeType, charset, encodedFileName); MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDataHandler(new DataHandler(source)); attachmentPart.setHeader("Content-ID", "<" + contentId + ">"); attachmentPart.setHeader("Content-Type", contentTypeValue); attachmentPart.setFileName(encodedFileName); attachmentPart.setDisposition(disposition); return attachmentPart; }
From source file:com.cubusmail.mail.util.MessageUtils.java
/** * Method for checking if the message has attachments. *//*www .j a v a 2 s . c om*/ public static List<MimePart> attachmentsFromPart(Part part) throws MessagingException, IOException { List<MimePart> attachmentParts = new ArrayList<MimePart>(); if (part instanceof MimePart) { MimePart mimePart = (MimePart) part; if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) mimePart.getContent(); for (int i = 0; i < mp.getCount(); i++) { MimeBodyPart bodyPart = (MimeBodyPart) mp.getBodyPart(i); if (Part.ATTACHMENT.equals(bodyPart.getDisposition()) || Part.INLINE.equals(bodyPart.getDisposition())) { attachmentParts.add(bodyPart); } } } else if (part.isMimeType("application/*")) { attachmentParts.add(mimePart); } } return attachmentParts; }