javax.mail.internet.MimePart Java Examples
The following examples show how to use
javax.mail.internet.MimePart.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Get an attachment's file name * * @param partIndex * @return * @throws PackageException */ @PublicAtsApi public String getAttachmentFileName( int partIndex ) throws PackageException { // first check if there is part at this position at all if (partIndex >= attachmentPartIndices.size()) { throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'"); } try { MimePart part = getPart(attachmentPartIndices.get(partIndex)); // get the attachment file name String fileName = part.getFileName(); if (fileName == null) { throw new PackageException("Could not determine file name for attachment at position " + partIndex); } return fileName; } catch (MessagingException me) { throw new PackageException(me); } }
Example #2
Source File: MimeMessageHelper.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Set the given text directly as content in non-multipart mode * or as default body part in multipart mode. * The "html" flag determines the content type to apply. * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText}; * else, mail readers might not be able to resolve inline references correctly. * @param text the text for the message * @param html whether to apply content type "text/html" for an * HTML mail, using default content type ("text/plain") else * @throws MessagingException in case of errors */ public void setText(String text, boolean html) throws MessagingException { Assert.notNull(text, "Text must not be null"); MimePart partToUse; if (isMultipart()) { partToUse = getMainPart(); } else { partToUse = this.mimeMessage; } if (html) { setHtmlTextToMimePart(partToUse, text); } else { setPlainTextToMimePart(partToUse, text); } }
Example #3
Source File: Test_MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
@Test public void setBody_multiParts_with_htmlPart_only() throws Exception { // create a new message and add HTML part to it MimePackage newMailMessage = new MimePackage(); newMailMessage.addPart("html body", MimePackage.PART_TYPE_TEXT_HTML); MimePart htmlPart = newMailMessage.getPart(0, false); assertEquals(htmlPart.getContent(), "html body"); assertEquals(htmlPart.getContentType(), "text/html; charset=us-ascii"); // modify the only part newMailMessage.setBody("new body"); // verify the modifications MimePart newHtmlPart = newMailMessage.getPart(0, false); assertEquals(newHtmlPart.getContent(), "new body"); assertEquals(newHtmlPart.getContentType(), "text/html; charset=us-ascii"); // verify there are no more parts try { newMailMessage.getPart(1, false); assertTrue("There is more than 1 part, while we expect to have just 1", false); } catch (NoSuchMimePartException e) {} }
Example #4
Source File: Test_MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
@Test public void setBody_multiParts_with_textPart_only() throws Exception { // create a new message and add TEXT part to it MimePackage newMailMessage = new MimePackage(); newMailMessage.addPart("text plain body", MimePackage.PART_TYPE_TEXT_PLAIN); MimePart textPart = newMailMessage.getPart(0, false); assertEquals(textPart.getContent(), "text plain body"); assertEquals(textPart.getContentType(), "text/plain; charset=us-ascii"); // modify the only part newMailMessage.setBody("new body"); // verify the modifications MimePart newTextPart = newMailMessage.getPart(0, false); assertEquals(newTextPart.getContent(), "new body"); assertEquals(newTextPart.getContentType(), "text/plain; charset=us-ascii"); // verify there are no more parts try { newMailMessage.getPart(1, false); assertTrue("There is more than 1 part, while we expect to have just 1", false); } catch (NoSuchMimePartException e) {} }
Example #5
Source File: MimeMessageHelper.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Set the given text directly as content in non-multipart mode * or as default body part in multipart mode. * The "html" flag determines the content type to apply. * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText}; * else, mail readers might not be able to resolve inline references correctly. * @param text the text for the message * @param html whether to apply content type "text/html" for an * HTML mail, using default content type ("text/plain") else * @throws MessagingException in case of errors */ public void setText(String text, boolean html) throws MessagingException { Assert.notNull(text, "Text must not be null"); MimePart partToUse; if (isMultipart()) { partToUse = getMainPart(); } else { partToUse = this.mimeMessage; } if (html) { setHtmlTextToMimePart(partToUse, text); } else { setPlainTextToMimePart(partToUse, text); } }
Example #6
Source File: ContentTypeCleaner.java From eml-to-pdf-converter with Apache License 2.0 | 6 votes |
/** * Attempt to repair the given contentType if broken. * @param mp Mimepart holding the contentType * @param contentType ContentType * @return fixed contentType String * @throws MessagingException */ public static String cleanContentType(MimePart mp, String contentType) throws MessagingException { ContentType ct = parseContentType(contentType); if (ct == null) { ct = getParsableContentType(contentType); } if (ct.getBaseType().equalsIgnoreCase("text/plain") || ct.getBaseType().equalsIgnoreCase("text/html")) { Charset charset = parseCharset(ct); if (charset == null) { Logger.debug("Charset of the ContentType could not be read, try to decode the contentType as quoted-printable"); ContentType ctTmp = decodeContentTypeAsQuotedPrintable(contentType); if (parseCharset(ctTmp) != null) { ct = ctTmp; } else { ct.setParameter("charset", ContentTypeCleaner.DEFAULT_CHARSET); } } } return ct.toString(); }
Example #7
Source File: MultiContentHandler.java From ogham with Apache License 2.0 | 6 votes |
@Override public void setContent(MimePart message, Multipart multipart, Email email, Content content) throws ContentHandlerException { try { MultiContent multiContent = (MultiContent) content; MimeMultipart mp = new MimeMultipart("alternative"); for (Content c : multiContent.getContents()) { delegate.setContent(message, mp, email, c); } // add the part MimeBodyPart part = new MimeBodyPart(); part.setContent(mp); multipart.addBodyPart(part); } catch (MessagingException e) { throw new ContentHandlerException("Failed to generate alternative content", content, e); } }
Example #8
Source File: Converter7Bit.java From james-project with Apache License 2.0 | 6 votes |
private void convertPart(MimePart part) throws MessagingException, IOException { if ("8bit".equals(part.getEncoding())) { // The content may already be in encoded the form (likely with mail // created from a // stream). In that case, just changing the encoding to // quoted-printable will mangle // the result when this is transmitted. We must first convert the // content into its // native format, set it back, and only THEN set the transfer // encoding to force the // content to be encoded appropriately. // if the part doesn't contain text it will be base64 encoded. String contentTransferEncoding = part.isMimeType("text/*") ? "quoted-printable" : "base64"; part.setContent(part.getContent(), part.getContentType()); part.setHeader("Content-Transfer-Encoding", contentTransferEncoding); part.addHeader("X-MIME-Autoconverted", "from 8bit to " + contentTransferEncoding + " by " + mailetContext.getServerInfo()); } else if (part.isMimeType("multipart/*")) { MultipartUtil.retrieveBodyParts((MimeMultipart) part.getContent()) .forEach(Throwing.consumer(bodyPart -> convertPart((MimePart) bodyPart))); } }
Example #9
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Return the attachment data * * @param partIndex * the index of the attachment * @return an InputStream with the attachment data * @throws PackageException */ @PublicAtsApi public InputStream getAttachmentPartData( int partIndex ) throws PackageException { try { boolean storeReconnected = reconnectStoreIfClosed(); // store should be opened for actions including getting InputStream. Hence store open is not in getPart try { MimePart part = getPart(partIndex, true); return part.getInputStream(); } finally { closeStoreConnection(storeReconnected); } } catch (MessagingException me) { throw new PackageException("Error getting attachment data for part " + partIndex, me); } catch (IOException ioe) { throw new PackageException("Error getting attachment data for part " + partIndex, ioe); } }
Example #10
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Get the attachment character set * * @param partIndex * the index of the attachment * @return the character set for this attachment, null if there is no such * @throws PackageException */ @PublicAtsApi public String getAttachmentCharset( int partIndex ) throws PackageException { // first check if there is part at this position at all if (partIndex >= attachmentPartIndices.size()) { throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'"); } try { MimePart part = getPart(attachmentPartIndices.get(partIndex)); // get the content type header ContentType contentType = new ContentType(part.getContentType()); return contentType.getParameter("charset"); } catch (MessagingException me) { throw new PackageException(me); } }
Example #11
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Get the attachment content type * * @param partIndex * @return * @throws PackageException */ @PublicAtsApi public String getAttachmentContentType( int partIndex ) throws PackageException { // first check if there is part at this position at all if (partIndex >= attachmentPartIndices.size()) { throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'"); } try { MimePart part = getPart(attachmentPartIndices.get(partIndex)); // get the content type header ContentType contentType = new ContentType(part.getContentType()); return contentType.getBaseType(); } catch (MessagingException me) { throw new PackageException(me); } }
Example #12
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Set/modify attachment file name * * @param attachmentPartIndex index among attachment parts only * @param fileName the file name. Add one or reset existing one * @throws NoSuchMimePartException if not such attachment part is found * @throws PackageException in case of other mail messaging exception */ @PublicAtsApi public void setAttachmentFileName( int attachmentPartIndex, String fileName ) throws PackageException { // first check if there is part at this position at all if (attachmentPartIndex >= attachmentPartIndices.size()) { throw new NoSuchMimePartException("No attachment at position '" + attachmentPartIndex + "'"); } try { MimePart part = getPart(attachmentPartIndices.get(attachmentPartIndex)); // set the attachment file name part.setFileName(fileName); // must save now this.message.saveChanges(); } catch (MessagingException me) { throw new PackageException(me); } }
Example #13
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Get the character set of a regular part * * @param partIndex * the index of the part * @return the charset * @throws PackageException */ @PublicAtsApi public String getRegularPartCharset( int partIndex ) throws PackageException { // first check if there is part at this position at all if (partIndex >= regularPartIndices.size()) { throw new NoSuchMimePartException("No regular part at position '" + partIndex + "'"); } try { MimePart part = getPart(regularPartIndices.get(partIndex)); // get the content type header ContentType contentType = new ContentType(part.getContentType()); return contentType.getParameter("charset"); } catch (MessagingException me) { throw new PackageException(me); } }
Example #14
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Get a MIME part based on it's index and type * * @param partIndex * the index of the MIME part * @param isAttachment * the type - true if the part is attachment, false otherwise * @return the mime part * @throws NoSuchMimePartException * if there is no such part */ @PublicAtsApi public MimePart getPart( int partIndex, boolean isAttachment ) throws NoSuchMimePartException { // first check if there is part at this position at all if (isAttachment) { if (partIndex >= attachmentPartIndices.size()) { throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'"); } } else { if (partIndex >= regularPartIndices.size()) { throw new NoSuchMimePartException("No regular part at position '" + partIndex + "'"); } } MimePart part; if (isAttachment) { part = getPart(attachmentPartIndices.get(partIndex)); } else { part = getPart(regularPartIndices.get(partIndex)); } return part; }
Example #15
Source File: MimeMessageHelper.java From spring-analysis-note with MIT License | 6 votes |
/** * Set the given text directly as content in non-multipart mode * or as default body part in multipart mode. * The "html" flag determines the content type to apply. * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText}; * else, mail readers might not be able to resolve inline references correctly. * @param text the text for the message * @param html whether to apply content type "text/html" for an * HTML mail, using default content type ("text/plain") else * @throws MessagingException in case of errors */ public void setText(String text, boolean html) throws MessagingException { Assert.notNull(text, "Text must not be null"); MimePart partToUse; if (isMultipart()) { partToUse = getMainPart(); } else { partToUse = this.mimeMessage; } if (html) { setHtmlTextToMimePart(partToUse, text); } else { setPlainTextToMimePart(partToUse, text); } }
Example #16
Source File: MimeMessageHelper.java From java-technology-stack with MIT License | 6 votes |
/** * Set the given text directly as content in non-multipart mode * or as default body part in multipart mode. * The "html" flag determines the content type to apply. * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText}; * else, mail readers might not be able to resolve inline references correctly. * @param text the text for the message * @param html whether to apply content type "text/html" for an * HTML mail, using default content type ("text/plain") else * @throws MessagingException in case of errors */ public void setText(String text, boolean html) throws MessagingException { Assert.notNull(text, "Text must not be null"); MimePart partToUse; if (isMultipart()) { partToUse = getMainPart(); } else { partToUse = this.mimeMessage; } if (html) { setHtmlTextToMimePart(partToUse, text); } else { setPlainTextToMimePart(partToUse, text); } }
Example #17
Source File: MimeMessageParser.java From commons-email with Apache License 2.0 | 6 votes |
/** * Checks whether the MimePart contains an object of the given mime type. * * @param part the current MimePart * @param mimeType the mime type to check * @return {@code true} if the MimePart matches the given mime type, {@code false} otherwise * @throws MessagingException parsing the MimeMessage failed * @throws IOException parsing the MimeMessage failed */ private boolean isMimeType(final MimePart part, final String mimeType) throws MessagingException, IOException { // Do not use part.isMimeType(String) as it is broken for MimeBodyPart // and does not really check the actual content type. try { final ContentType ct = new ContentType(part.getDataHandler().getContentType()); return ct.match(mimeType); } catch (final ParseException ex) { return part.getContentType().equalsIgnoreCase(mimeType); } }
Example #18
Source File: ImapConnection.java From davmail with GNU General Public License v2.0 | 6 votes |
/** * Compute body part size with failover. * @param bodyPart MIME body part * @return body part size or 0 on error */ private int getBodyPartSize(MimePart bodyPart) { int bodySize = 0; try { bodySize = bodyPart.getSize(); if (bodySize == -1) { // failover, try to get size ByteArrayOutputStream baos = new ByteArrayOutputStream(); bodyPart.writeTo(baos); bodySize = baos.size(); } } catch (IOException | MessagingException e) { LOGGER.warn("Unable to get body part size " + e.getMessage(), e); } return bodySize; }
Example #19
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
@PublicAtsApi public List<InputStream> getAllStreams() throws PackageException { boolean storeReconnected = false; try { // store should be opened for actions including getting InputStream. storeReconnected = reconnectStoreIfClosed(); ArrayList<InputStream> streams = new ArrayList<InputStream>(); try { for (MimePart part : parts) { streams.add(part.getInputStream()); } } finally { closeStoreConnection(storeReconnected); } return streams; } catch (MessagingException me) { throw new PackageException("Could not read mime parts", me); } catch (IOException ioe) { throw new PackageException("Could not read mime parts", ioe); } }
Example #20
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Get the specified header value from a specified MIME part * * @param headerName * @param partNum * @param headerIndex * @return * @throws PackageException */ @PublicAtsApi public String getPartHeader( String headerName, int partNum, int headerIndex ) throws PackageException { try { String[] headers; if (partNum >= parts.size()) { throw new NoSuchMimePartException("No MIME part at position '" + partNum + "'"); } MimePart part = parts.get(partNum); headers = part.getHeader(headerName); if ( (headers != null) && (headers.length > headerIndex)) { return headers[headerIndex]; } else { throw new NoSuchHeaderException(headerName, partNum, headerIndex); } } catch (MessagingException me) { throw new PackageException(me); } }
Example #21
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Get all header values from a specified MIME part * * @param headerName * @param partNum * @return * @throws PackageException */ @PublicAtsApi public String[] getPartHeaderValues( String headerName, int partNum ) throws PackageException { try { String[] headers; if (partNum >= parts.size()) { throw new NoSuchMimePartException("No MIME part at position '" + partNum + "'"); } MimePart part = parts.get(partNum); headers = part.getHeader(headerName); if ( (headers != null) && (headers.length > 0)) { return headers; } else { throw new NoSuchHeaderException(headerName, partNum); } } catch (MessagingException me) { throw new PackageException(me); } }
Example #22
Source File: MimePackage.java From ats-framework with Apache License 2.0 | 6 votes |
/** * Get the content type of a regular part * * @param partIndex * the index of the regular part * @return the content type as string * @throws PackageException */ @PublicAtsApi public String getRegularPartContentType( int partIndex ) throws PackageException { // first check if there is part at this position at all if (partIndex >= regularPartIndices.size()) { throw new NoSuchMimePartException("No regular part at position '" + partIndex + "'"); } try { MimePart part = getPart(regularPartIndices.get(partIndex)); // get the content type header ContentType contentType = new ContentType(part.getContentType()); return contentType.getBaseType(); } catch (MessagingException me) { throw new PackageException(me); } }
Example #23
Source File: AddFooter.java From james-project with Apache License 2.0 | 5 votes |
private Optional<String> attachFooterToTextPart(MimePart part) throws MessagingException, IOException { String content = (String) part.getContent(); if (part.isMimeType("text/plain")) { return Optional.of(attachFooterToText(content)); } else if (part.isMimeType("text/html")) { return Optional.of(attachFooterToHTML(content)); } return Optional.empty(); }
Example #24
Source File: URIRBLHandler.java From james-project with Apache License 2.0 | 5 votes |
/** * Recursively scans all MimeParts of an email for domain strings. Domain * strings that are found are added to the supplied HashSet. * * @param part * MimePart to scan * @param session * not null * @return domains The HashSet that contains the domains which were * extracted */ private HashSet<String> scanMailForDomains(MimePart part, SMTPSession session) throws MessagingException, IOException { HashSet<String> domains = new HashSet<>(); LOGGER.debug("mime type is: \"{}\"", part.getContentType()); if (part.isMimeType("text/plain") || part.isMimeType("text/html")) { LOGGER.debug("scanning: \"{}\"", part.getContent()); HashSet<String> newDom = URIScanner.scanContentForDomains(domains, part.getContent().toString()); // Check if new domains are found and add the domains if (newDom != null && newDom.size() > 0) { domains.addAll(newDom); } } else if (part.isMimeType("multipart/*")) { MimeMultipart multipart = (MimeMultipart) part.getContent(); int count = multipart.getCount(); LOGGER.debug("multipart count is: {}", count); for (int index = 0; index < count; index++) { LOGGER.debug("recursing index: {}", index); MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(index); HashSet<String> newDomains = scanMailForDomains(mimeBodyPart, session); // Check if new domains are found and add the domains if (newDomains != null && newDomains.size() > 0) { domains.addAll(newDomains); } } } return domains; }
Example #25
Source File: PriorizedContentHandler.java From ogham with Apache License 2.0 | 5 votes |
@Override public void setContent(MimePart message, Multipart multipart, Email email, Content content) throws ContentHandlerException { JavaMailContentHandler contentHandler = getContentHandler(content); if (contentHandler == null) { throw new NoContentHandlerException("there is no content handler defined for managing " + content.getClass().getSimpleName() + " content class", content); } contentHandler.setContent(message, multipart, email, content); }
Example #26
Source File: ContentTypeCleaner.java From james-project with Apache License 2.0 | 5 votes |
public static String cleanContentType(MimePart mimePart, String contentType) { if (Strings.isNullOrEmpty(contentType)) { return null; } if (REGEX.matcher(contentType).find()) { return contentType; } LOGGER.warn("Can not parse Content-Type: " + contentType); return null; }
Example #27
Source File: MultiPartEmail.java From commons-email with Apache License 2.0 | 5 votes |
/** * Set the message of the email. * * @param msg A String. * @return An Email. * @throws EmailException see javax.mail.internet.MimeBodyPart * for definitions * @since 1.0 */ @Override public Email setMsg(final String msg) throws EmailException { // throw exception on null message if (EmailUtils.isEmpty(msg)) { throw new EmailException("Invalid message supplied"); } try { final BodyPart primary = getPrimaryBodyPart(); if (primary instanceof MimePart && EmailUtils.isNotEmpty(charset)) { ((MimePart) primary).setText(msg, charset); } else { primary.setText(msg); } } catch (final MessagingException me) { throw new EmailException(me); } return this; }
Example #28
Source File: MimeMessageHelper.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void setHtmlTextToMimePart(MimePart mimePart, String text) throws MessagingException { if (getEncoding() != null) { mimePart.setContent(text, CONTENT_TYPE_HTML + CONTENT_TYPE_CHARSET_SUFFIX + getEncoding()); } else { mimePart.setContent(text, CONTENT_TYPE_HTML); } }
Example #29
Source File: MimeMessageHelper.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void setPlainTextToMimePart(MimePart mimePart, String text) throws MessagingException { if (getEncoding() != null) { mimePart.setText(text, getEncoding()); } else { mimePart.setText(text); } }
Example #30
Source File: MimeMessageHelper.java From spring-analysis-note with MIT License | 5 votes |
private void setPlainTextToMimePart(MimePart mimePart, String text) throws MessagingException { if (getEncoding() != null) { mimePart.setText(text, getEncoding()); } else { mimePart.setText(text); } }