org.apache.james.mime4j.MimeException Java Examples

The following examples show how to use org.apache.james.mime4j.MimeException. 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: MessageStoreImplRepositoryTest.java    From sling-samples with Apache License 2.0 6 votes vote down vote up
private void assertSaveMessage(String messageFile) throws MimeException, IOException, FileNotFoundException {
	MessageBuilder builder = new DefaultMessageBuilder();
	Message msg = builder.parseMessage(new FileInputStream(new File(TU.TEST_FOLDER, messageFile)));

	store.save(msg);

	final Resource r = resolver.getResource(getResourcePath(msg, store));
	assertNotNull("Expecting non-null Resource", r);
	final ModifiableValueMap m = r.adaptTo(ModifiableValueMap.class);

	File bodyFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(messageFile, BODY_SUFFIX));
	if (bodyFile.exists()) {
		String expectedBody = readTextFile(bodyFile);
		assertValueMap(m, "Body", expectedBody);
	}

	File headersFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(messageFile, HEADERS_SUFFIX));
	if (headersFile.exists()) {
		MessageStoreImplRepositoryTestUtil.assertHeaders(headersFile, m);
	}

	assertTrue(headersFile.exists() || bodyFile.exists()); // test at least something 
}
 
Example #2
Source File: MimeUtils.java    From holdmail with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the provided inputstream into the message domain
 * @param rawContentStream The inputstream to the message RAW content
 * @return A {@link MessageContent} object representing the parsed content, or
 *          a {@link HoldMailException} if parsing failed.
 */
public static MessageContent parseMessageContent(final InputStream rawContentStream) {

    try {
        MessageContentExtractor bodyPartExtractor = new MessageContentExtractor();

        MimeStreamParser parser = new MimeStreamParser();
        parser.setContentDecoding(true);
        parser.setContentHandler(bodyPartExtractor);
        parser.parse(rawContentStream);
        return bodyPartExtractor.getParts();
    } catch (MimeException | IOException e) {
        throw new HoldMailException("Failed to parse body", e);
    }

}
 
Example #3
Source File: MboxHandler.java    From SnowGraph with Apache License 2.0 6 votes vote down vote up
@Override
public void body(BodyDescriptor bd, InputStream is) throws MimeException,
        IOException {
    String r = "";
    byte[] buffer = new byte[200];
    String s;
    int len;
    try {
        while ((len = is.read(buffer)) != -1) {
            if (len != 200) {
                byte buffer2[] = new byte[len];
                System.arraycopy(buffer, 0, buffer2, 0, len);
                s = new String(buffer2);
            } else {
                s = new String(buffer);
            }
            if (s != null)
                r += s;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    mailInfo.body = r;
    //System.out.println("body");
    //System.out.println(r);
}
 
Example #4
Source File: ContentDispositionDecoder.java    From email-mime-parser with Apache License 2.0 6 votes vote down vote up
private static String decodeBytes(String value, String charset)
		throws MimeException {
	byte[] b = new byte[value.length()];

	int i = 0;
	int temp = 0;
	for (int bi = 0; i < value.length(); ++i) {
		char c = value.charAt(i);
		if (c == '%') {
			String hex = value.substring(i + 1, i + 3);
			c = (char) Integer.parseInt(hex, 16);
			i += 2;
		}
		b[(bi++)] = (byte) c;
		temp = bi;
	}
	
	String str;
	try {
		str = new String(b, 0, temp, charset);
	} catch (UnsupportedEncodingException e) {
		throw new MimeException(e);
	}
	return str;
}
 
Example #5
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
private void parseEmail(String messageFileName, ContentHandler contentHandler) throws FileNotFoundException,
		MimeException, IOException {

	MimeConfig mime4jParserConfig = MimeConfig.DEFAULT;
	BodyDescriptorBuilder bodyDescriptorBuilder = new DefaultBodyDescriptorBuilder();		
	MimeStreamParser mime4jParser = new MimeStreamParser(mime4jParserConfig,DecodeMonitor.SILENT,bodyDescriptorBuilder);
	mime4jParser.setContentDecoding(true);
	mime4jParser.setContentHandler(contentHandler);
	
	URL url = this.getClass().getClassLoader().getResource(messageFileName);
	
	InputStream mailIn = new FileInputStream(new File(url.getFile()));
	mime4jParser.parse(mailIn);
	
}
 
Example #6
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void assertEmailContainingTraditionalChineseInHtmlBody() throws  MimeException, IOException{
	Email email = getParsedEmailWithTraditionalChineseInHtmlBody();		
	String actual = generateCheckSum(email.getHTMLEmailBody().getIs());
	String expected = "c3a8ec4cd790362af3cb146275819301774167b3";
	Assert.assertEquals(actual, expected);
}
 
Example #7
Source File: MboxHandler.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
@Override
public void endMessage() throws MimeException {
    Node node = db.createNode();
    createMailNode(node, mailInfo.subject, mailInfo.id, mailInfo.senderName, mailInfo.senderMail, mailInfo.receiverNames, mailInfo.receiverMails, mailInfo.replyTo, mailInfo.date, mailInfo.body);
    mailMap.put(mailInfo.id, node);
    createUserNode(node, mailInfo.senderName, mailInfo.senderMail, true);
    for (int i = 0; i < mailInfo.receiverMails.length; i++) {
        String name = mailInfo.receiverNames[i];
        String mail = mailInfo.receiverMails[i];
        createUserNode(node, name, mail, false);
    }
    mailInfo = new MailInfo();
}
 
Example #8
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void assertEmailSize() throws  MimeException, IOException{
	Email email = getParsedEmailWithLargeImageAttachments();		
	List<Attachment> attachments = email.getAttachments();		
	
	int actualEmailSize = 0; //in Bytes
	int expectedEmailSize = 2405993;

	actualEmailSize += email.getHTMLEmailBody().getAttachmentSize();
	actualEmailSize += email.getPlainTextEmailBody().getAttachmentSize();
	for (Attachment attachment : attachments) {
		actualEmailSize += attachment.getAttachmentSize();
	}		
	Assert.assertEquals(actualEmailSize, expectedEmailSize);
}
 
Example #9
Source File: EmailVerificationTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
private Email getParsedSimpleGmail() throws
        MimeException, IOException {
    ContentHandler basicGmailContentHandler = getContentHandler();
    Email email = getParsedEmail("gmailMessage.eml",
            basicGmailContentHandler);
    return email;
}
 
Example #10
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void validateChineseSubjectIsGettingDecoded() throws  MimeException, IOException{
	Email email = getParsedEmailWithChinesEmailSubject();		
	String actual = email.getEmailSubject();		
	String expected = getExpectedDecodedSubject();		
	Assert.assertEquals(actual, expected);
}
 
Example #11
Source File: EmailVerificationTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
private void assertGettingHeader(String header)
        throws MimeException, IOException {

    Email email = getParsedSimpleGmail();
    Header parsedHeader = email.getHeader();

    Field from = parsedHeader.getField(header);
    Assert.assertEquals(header, from.getName());
}
 
Example #12
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void validateChineseFileNameAttachmentIsGettingDecoded() throws  MimeException, IOException {
	Email email = getParsedEmailWithChinesFileAttachments();
	List<Attachment> attachments = email.getAttachments();
	StringBuilder strBuild = new StringBuilder();
	for (Attachment emailAttachment : attachments) {
		strBuild.append(emailAttachment.getAttachmentName());
	}		
	Assert.assertEquals(strBuild.toString(), getExpectedDecodedFileNames());		
}
 
Example #13
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void assertEmailAttachmentRenderingForLargeAttachedImages() throws  MimeException, IOException{
	
	Email email = getParsedEmailWithLargeImageAttachments();		

	List<Attachment> attachments = email.getAttachments();		
	Map<String, String> fileNameCheckSumPair = generateFileNameCheckSumPair();
	
	for (Attachment attachment : attachments) {
		String actualFileChecksum = generateCheckSum(attachment.getIs());
		
		Assert.assertEquals(actualFileChecksum, getExpectedFileCheckSum(fileNameCheckSumPair, attachment));
	}
}
 
Example #14
Source File: Common.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
private static String getDecodedDispositionFileName(BodyDescriptor bd){
	String attachmentName = null;
	try {
		attachmentName = ContentDispositionDecoder.decodeDispositionFileName(((MaximalBodyDescriptor)bd).getContentDispositionParameters());
	} catch (MimeException e) {
		throw new RuntimeException(e);
	}
	return attachmentName;
}
 
Example #15
Source File: MimeUtilsTest.java    From holdmail with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRethrowMimeExceptionOnParse() throws Exception {

    MimeException mimeException = new MimeException("mimeError");
    whenNew(MessageContentExtractor.class).withNoArguments()
                                          .thenThrow(mimeException);

    assertThatThrownBy(() -> MimeUtils.parseMessageContent(inputStreamMock))
            .isInstanceOf(HoldMailException.class)
            .hasMessage("Failed to parse body")
            .hasCause(mimeException);

}
 
Example #16
Source File: EmailParseManager.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
public Email getParsedEmail() throws MimeException, IOException {
	
	MimeConfig mime4jParserConfig = MimeConfig.DEFAULT;
	BodyDescriptorBuilder bodyDescriptorBuilder = new DefaultBodyDescriptorBuilder();
	MimeStreamParser mime4jParser = new MimeStreamParser(mime4jParserConfig,DecodeMonitor.SILENT,bodyDescriptorBuilder);
	mime4jParser.setContentDecoding(true);
	mime4jParser.setContentHandler(contentHandler);		
	
	mime4jParser.parse(rawEmailFile);
	
	return ((CustomContentHandler)contentHandler).getEmail();
}
 
Example #17
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void assertHtmlBodyForLargeInlineImages() throws IOException, MimeException{		
	Email email = getParsedEmailWithLargeImageAttachments();		
	Attachment htmlBody = email.getHTMLEmailBody();
	
	assertHtmlIsCorrectlyParsed(htmlBody);
}
 
Example #18
Source File: CustomContentHandler.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
@Override
public void startMessage() throws MimeException {
    if (email.getMessageStack().empty()) {
        email.getMessageStack().push(new EmailMessageType(EmailMessageTypeHierarchy.parent));
    } else {
        email.getMessageStack().push(new EmailMessageType(EmailMessageTypeHierarchy.child));
    }
}
 
Example #19
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
private Email getParsedEmailWithDecodedAttachments() throws  MimeException, IOException {
	ContentHandler contentHandler = getContentHandler();		
	Email email = getParsedEmail("inlineEmailWithPenguins.eml",contentHandler);
	return email;
}
 
Example #20
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
private Email getParsedEmailWithTraditionalChineseInHtmlBody() throws  MimeException, IOException {
	ContentHandler contentHandler = getContentHandler();		
	Email email = getParsedEmail("traditionalChineseInHtmlBody.eml",contentHandler);
	return email;
}
 
Example #21
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void decodeAppleFileNameHeader() throws  MimeException, IOException{
	Email email = getParsedEmailWithAppleFileNameHeader();
	assertDecodedFileName(email);
}
 
Example #22
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testEmailWithImageContentType() throws MimeException, IOException{
	Email email = getEmailWithImageContentType();
	assertEmailWithImageContentMerge(email);
	assertNumberOfAttachmnetInEmail(email);
}
 
Example #23
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
private Email getParsedEmailWithCalendarAttachments() throws  MimeException, IOException {
	ContentHandler contentHandler = getContentHandler();		
	Email email = getParsedEmail("emailWithCalendar.eml",contentHandler);
	return email;
}
 
Example #24
Source File: MboxHandler.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
@Override
public void endMultipart() throws MimeException {
}
 
Example #25
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void assertEmailAttachmentGettingReplacedInHtmlEmailBody() throws  MimeException, IOException{
	Email email = getParsedEmailWithLargeImageAttachments();		
	Assert.assertTrue(email.isAttachmentReplacedInHtmlBody());
}
 
Example #26
Source File: MboxHandler.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
@Override
public void raw(InputStream is) throws MimeException {
}
 
Example #27
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
private Email getParsedEmailWithChinesFileAttachments() throws  MimeException, IOException {
	ContentHandler contentHandler = getContentHandler();		
	Email email = getParsedEmail("chineseFileAttachmentsFromOutLook.eml",contentHandler);
	return email;
}
 
Example #28
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
private Email getParsedEmailWithLargeImageAttachments()
		throws  MimeException, IOException {
	ContentHandler imageAttachmentContentHandler = getContentHandler();		
	Email email = getParsedEmail("multipleLargeImage.eml",imageAttachmentContentHandler);
	return email;
}
 
Example #29
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
private Email getEmailWithImageContentType()
		throws  MimeException, IOException {
	ContentHandler inlineMessageContentHandler = getContentHandler();
	Email email = getParsedEmail("emailWithImageContentType.eml",inlineMessageContentHandler);
	return email;
}
 
Example #30
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void getDecodedEmailSize() throws  MimeException, IOException{
	Email email = getParsedEmailWithDecodedAttachments();
	assertDecodedEmailSize(email);
	assertDisplayEmailSize(email);
}