Java Code Examples for javax.mail.Part#getFileName()
The following examples show how to use
javax.mail.Part#getFileName() .
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: cfMailMessageData.java From openbd-core with GNU General Public License v3.0 | 7 votes |
public static String getFilename( Part Mess ) throws MessagingException{ // Part.getFileName() doesn't take into account any encoding that may have been // applied to the filename so in order to obtain the correct filename we // need to retrieve it from the Content-Disposition String [] contentType = Mess.getHeader( "Content-Disposition" ); if ( contentType != null && contentType.length > 0 ){ int nameStartIndx = contentType[0].indexOf( "filename=\"" ); if ( nameStartIndx != -1 ){ String filename = contentType[0].substring( nameStartIndx+10, contentType[0].indexOf( '\"', nameStartIndx+10 ) ); try { filename = MimeUtility.decodeText( filename ); return filename; } catch (UnsupportedEncodingException e) {} } } // couldn't determine it using the above, so fall back to more reliable but // less correct option return Mess.getFileName(); }
Example 2
Source File: MimeMessageParser.java From commons-email with Apache License 2.0 | 6 votes |
/** * Determines the name of the data source if it is not already set. * * @param part the mail part * @param dataSource the data source * @return the name of the data source or {@code null} if no name can be determined * @throws MessagingException accessing the part failed * @throws UnsupportedEncodingException decoding the text failed */ protected String getDataSourceName(final Part part, final DataSource dataSource) throws MessagingException, UnsupportedEncodingException { String result = dataSource.getName(); if (result == null || result.length() == 0) { result = part.getFileName(); } if (result != null && result.length() > 0) { result = MimeUtility.decodeText(result); } else { result = null; } return result; }
Example 3
Source File: SubethaEmailMessagePart.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * Object can be built on existing message part only. * * @param messagePart Message part. */ public SubethaEmailMessagePart(Part messagePart) { ParameterCheck.mandatory("messagePart", messagePart); try { fileSize = messagePart.getSize(); fileName = messagePart.getFileName(); contentType = messagePart.getContentType(); Matcher matcher = encodingExtractor.matcher(contentType); if (matcher.find()) { encoding = matcher.group(1); if (!Charset.isSupported(encoding)) { throw new EmailMessageException(ERR_UNSUPPORTED_ENCODING, encoding); } } try { contentInputStream = messagePart.getInputStream(); } catch (Exception ex) { throw new EmailMessageException(ERR_FAILED_TO_READ_CONTENT_STREAM, ex.getMessage()); } } catch (MessagingException e) { throw new EmailMessageException(ERR_INCORRECT_MESSAGE_PART, e.getMessage()); } }
Example 4
Source File: SubethaEmailMessage.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * Method extracts file name from a message part for saving its as aa attachment. If the file name can't be extracted, it will be generated based on defaultPrefix parameter. * * @param defaultPrefix This prefix fill be used for generating file name. * @param messagePart A part of message * @return File name. * @throws MessagingException */ private String getPartFileName(String defaultPrefix, Part messagePart) throws MessagingException { String fileName = messagePart.getFileName(); if (fileName != null) { try { fileName = MimeUtility.decodeText(fileName); } catch (UnsupportedEncodingException ex) { // Nothing to do :) } } else { fileName = defaultPrefix; if (messagePart.isMimeType(MIME_PLAIN_TEXT)) fileName += ".txt"; else if (messagePart.isMimeType(MIME_HTML_TEXT)) fileName += ".html"; else if (messagePart.isMimeType(MIME_XML_TEXT)) fileName += ".xml"; else if (messagePart.isMimeType(MIME_IMAGE)) fileName += ".gif"; } return fileName; }
Example 5
Source File: DefaultAttachmentPredicate.java From ogham with Apache License 2.0 | 5 votes |
private boolean isDownloadableAttachment(Part p) { try { return Part.ATTACHMENT.equalsIgnoreCase(p.getDisposition()) || p.getFileName() != null; } catch(MessagingException e) { throw new FilterException("Failed to check if attachment is downloadable", e); } }
Example 6
Source File: AttachmentFileNameIs.java From james-project with Apache License 2.0 | 4 votes |
/** * Checks if <I>part</I> matches with at least one of the <CODE>masks</CODE>. * * @param part */ protected boolean matchFound(Part part) throws Exception { /* * if there is an attachment and no inline text, * the content type can be anything */ if (part.getContentType() == null || part.getContentType().startsWith("multipart/alternative")) { return false; } Object content; try { content = part.getContent(); } catch (UnsupportedEncodingException uee) { // in this case it is not an attachment, so ignore it return false; } Exception anException = null; if (content instanceof Multipart) { Multipart multipart = (Multipart) content; for (int i = 0; i < multipart.getCount(); i++) { try { Part bodyPart = multipart.getBodyPart(i); if (matchFound(bodyPart)) { return true; // matching file found } } catch (MessagingException e) { anException = e; } // remember any messaging exception and process next bodypart } } else { String fileName = part.getFileName(); if (fileName != null) { fileName = cleanFileName(fileName); // check the file name if (matchFound(fileName)) { if (isDebug) { LOGGER.debug("matched {}", fileName); } return true; } if (unzipIsRequested && fileName.endsWith(ZIP_SUFFIX) && matchFoundInZip(part)) { return true; } } } // if no matching attachment was found and at least one exception was catched rethrow it up if (anException != null) { throw anException; } return false; }
Example 7
Source File: MailAccount.java From projectforge-webapp with GNU General Public License v3.0 | 4 votes |
private void getContent(final Part msg, final StringBuffer buf) throws MessagingException, IOException { if (log.isDebugEnabled() == true) { log.debug("CONTENT-TYPE: " + msg.getContentType()); } String filename = msg.getFileName(); if (filename != null) { log.debug("FILENAME: " + filename); } // Using isMimeType to determine the content type avoids // fetching the actual content data until we need it. if (msg.isMimeType("text/plain")) { log.debug("This is plain text"); try { buf.append(msg.getContent()); } catch (UnsupportedEncodingException ex) { buf.append("Unsupported charset by java mail, sorry: " + "CONTENT-TYPE=[" + msg.getContentType() + "]"); } } else if (msg.isMimeType("text/html")) { log.debug("This is html text"); buf.append(msg.getContent()); } else if (msg.isMimeType("multipart/*")) { log.debug("This is a Multipart"); final Multipart multiPart = (Multipart) msg.getContent(); int count = multiPart.getCount(); for (int i = 0; i < count; i++) { if (i > 0) { buf.append("\n----------\n"); } getContent(multiPart.getBodyPart(i), buf); } } else if (msg.isMimeType("message/rfc822")) { log.debug("This is a Nested Message"); buf.append(msg.getContent()); } else { log.debug("This is an unknown type"); // If we actually want to see the data, and it's not a // MIME type we know, fetch it and check its Java type. final Object obj = msg.getContent(); if (obj instanceof String) { buf.append(obj); } else if (obj instanceof InputStream) { log.debug("Inputstream"); buf.append("Attachement: "); if (filename != null) { buf.append(filename); } else { buf.append("Unsupported format (not a file)."); } } else { log.error("Should not occur"); buf.append("Unsupported type"); } } }
Example 8
Source File: Attachment.java From camunda-bpm-mail with Apache License 2.0 | 3 votes |
public static Attachment from(Part part) throws MessagingException { Attachment attachment = new Attachment(); attachment.fileName = part.getFileName(); attachment.part = part; return attachment; }