Java Code Examples for javax.mail.Part#getHeader()

The following examples show how to use javax.mail.Part#getHeader() . 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 vote down vote up
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: FluentPartAssert.java    From ogham with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("squid:S1168")
private static List<String> getHeaderValues(Part part, String headerName) throws MessagingException {
	if (part != null) {
		String[] vals = part.getHeader(headerName);
		if (vals != null) {
			return asList(vals);
		}
	}
	return null;
}
 
Example 3
Source File: OnlyText.java    From james-project with Apache License 2.0 5 votes vote down vote up
private static void setContentFromPart(Message m, Part p, String newText, boolean setTextPlain) throws MessagingException, IOException {
    String contentType = p.getContentType();
    if (setTextPlain) {
        ContentType ct = new ContentType(contentType);
        ct.setPrimaryType("text");
        ct.setSubType("plain");
        contentType = ct.toString();
    }
    m.setContent(newText != null ? newText : p.getContent(), contentType);
    String[] h = p.getHeader("Content-Transfer-Encoding");
    if (h != null && h.length > 0) {
        m.setHeader("Content-Transfer-Encoding", h[0]);
    }
    m.saveChanges();
}
 
Example 4
Source File: cfMailMessageData.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public static String getDisposition( Part _mess ) throws MessagingException{
	String dispos = _mess.getDisposition();
// getDisposition isn't 100% reliable 
	if ( dispos == null ){
		String [] disposHdr = _mess.getHeader( "Content-Disposition" );
		if ( disposHdr != null && disposHdr.length > 0 && disposHdr[0].startsWith( Part.ATTACHMENT ) ){
			return Part.ATTACHMENT;
		}
	}
	
	return dispos;
}
 
Example 5
Source File: MessageParser.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获取消息附件中的文件.
 * @param part
 *            Part 对象
 * @param resourceList
 *            文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象)
 * @throws MessagingException
 *             the messaging exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private void getRelatedPart(Part part, List<ResourceFileBean> resourceList) throws MessagingException, IOException {
	//验证 Part 对象的 MIME 类型是否与指定的类型匹配。
	if (part.isMimeType("multipart/related")) {
		Multipart mulContent = (Multipart) part.getContent();
		for (int j = 0, m = mulContent.getCount(); j < m; j++) {
			Part contentPart = mulContent.getBodyPart(j);
			if (!contentPart.isMimeType("text/*")) {
				String fileName = "Resource";
				//此方法返回 Part 对象的内容类型。
				String type = contentPart.getContentType();
				if (type != null) {
					type = type.substring(0, type.indexOf("/"));
				}
				fileName = fileName + "[" + type + "]";
				if (contentPart.getHeader("Content-Location") != null
						&& contentPart.getHeader("Content-Location").length > 0) {
					fileName = contentPart.getHeader("Content-Location")[0];
				}
				InputStream is = contentPart.getInputStream();
				resourceList.add(new ResourceFileBean(fileName, is));
			}
		}
	} else {
		Multipart mp = null;
		Object content = part.getContent();
		if (content instanceof Multipart) {
			mp = (Multipart) content;
		} else {
			return;
		}
		for (int i = 0, n = mp.getCount(); i < n; i++) {
			Part body = mp.getBodyPart(i);
			getRelatedPart(body, resourceList);
		}
	}
}
 
Example 6
Source File: MessageParser.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获取消息附件中的文件.
 * @param part
 *            Part 对象
 * @param resourceList
 *            文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象)
 * @throws MessagingException
 *             the messaging exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private void getRelatedPart(Part part, List<ResourceFileBean> resourceList) throws MessagingException, IOException {
	//验证 Part 对象的 MIME 类型是否与指定的类型匹配。
	if (part.isMimeType("multipart/related")) {
		Multipart mulContent = (Multipart) part.getContent();
		for (int j = 0, m = mulContent.getCount(); j < m; j++) {
			Part contentPart = mulContent.getBodyPart(j);
			if (!contentPart.isMimeType("text/*")) {
				String fileName = "Resource";
				//此方法返回 Part 对象的内容类型。
				String type = contentPart.getContentType();
				if (type != null) {
					type = type.substring(0, type.indexOf("/"));
				}
				fileName = fileName + "[" + type + "]";
				if (contentPart.getHeader("Content-Location") != null
						&& contentPart.getHeader("Content-Location").length > 0) {
					fileName = contentPart.getHeader("Content-Location")[0];
				}
				InputStream is = contentPart.getInputStream();
				resourceList.add(new ResourceFileBean(fileName, is));
			}
		}
	} else {
		Multipart mp = null;
		Object content = part.getContent();
		if (content instanceof Multipart) {
			mp = (Multipart) content;
		} else {
			return;
		}
		for (int i = 0, n = mp.getCount(); i < n; i++) {
			Part body = mp.getBodyPart(i);
			getRelatedPart(body, resourceList);
		}
	}
}
 
Example 7
Source File: EmailFormatterInbound.java    From matrix-appservice-email with GNU Affero General Public License v3.0 4 votes vote down vote up
protected List<_BridgeMessageContent> extractContent(Part p) throws MessagingException, IOException {
    if (p.isMimeType("multipart/*")) {
        log.info("Found multipart content, extracting");

        List<_BridgeMessageContent> contents = new ArrayList<>();
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++) {
            contents.addAll(extractContent(mp.getBodyPart(i)));
        }
        return contents;
    }

    if (p.isMimeType("message/rfc822")) {
        log.info("Found nested content, extracting");
        return extractContent((Part) p.getContent());
    }

    String content = p.getContent().toString();
    String[] encodings = p.getHeader("Content-Transfer-Encoding");
    String encoding = (encodings != null && encodings.length > 0) ? encodings[0] : null;

    if (StringUtils.equalsIgnoreCase("quoted-printable", encoding)) {
        try {
            // TODO actually extract the charset properly
            // TODO read RFC to know default charset
            log.info("Transfer encoding is {}, decoding", encoding);
            content = new String(QuotedPrintableCodec.decodeQuotedPrintable(content.getBytes()));
        } catch (DecoderException e) {
            log.warn("Content transfer encoding is set to {} but enable to decode: {}", encoding, e.getMessage());
        }
    }

    if (p.isMimeType(MimeTypeUtils.TEXT_PLAIN_VALUE)) {
        log.info("Found plain text content");
        return Collections.singletonList(new BridgeMessageTextContent(content, encoding));
    }

    if (p.isMimeType(MimeTypeUtils.TEXT_HTML_VALUE)) {
        log.info("Found HTML content");
        return Collections.singletonList(new BridgeMessageHtmlContent(content, encoding));
    }

    return Collections.emptyList();
}