Java Code Examples for javax.mail.internet.ContentType#toString()

The following examples show how to use javax.mail.internet.ContentType#toString() . 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: ContentTypeCleaner.java    From eml-to-pdf-converter with Apache License 2.0 6 votes vote down vote up
/**
 * 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 2
Source File: ContentModelMessage.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public AlfrescoMimeMultipart(String subtype, FileInfo messageFileInfo)
{
    super();
    String boundary = getBoundaryValue(messageFileInfo);
    ContentType cType = new ContentType("multipart", subtype, null);
    cType.setParameter("boundary", boundary);
    contentType = cType.toString();
}
 
Example 3
Source File: ContentReplacer.java    From james-project with Apache License 2.0 5 votes vote down vote up
private String getContentType(Mail mail, Optional<Charset> charset) throws MessagingException, ParseException {
    String contentTypeAsString = mail.getMessage().getContentType();
    if (charset.isPresent()) {
        ContentType contentType = new ContentType(contentTypeAsString);
        contentType.setParameter("charset", charset.get().name());
        return contentType.toString();
    }
    return contentTypeAsString;
}
 
Example 4
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 5
Source File: MimeMultipartReport.java    From james-project with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the content type
 * @param aContentType
 */
protected void setContentType(ContentType aContentType) {
    contentType = aContentType.toString();
}