/** * Parses EML with provided EML content. * * @param emlContent {@link String} with EML content. * @return {@link ReceivedEmail}. * @throws MessagingException if {@link MimeMessage} cannot be created. * @see #parse(String, String) */ public ReceivedEmail parse(final String emlContent) throws MessagingException { try { return parse(emlContent, JoddCore.encoding); } catch (final UnsupportedEncodingException ignore) { return null; } }
public static EMLParser create() { return new EMLParser(); }
/** * Parses the EML content. If {@link Session} is not created, default one will be used. * * @param emlContentInputStream {@link InputStream} containing the EML content. * @return {@link ReceivedEmail}. * @throws MessagingException if {@link MimeMessage} cannot be created. */ protected ReceivedEmail parse(final InputStream emlContentInputStream) throws MessagingException { if (getSession() == null) { createSession(getProperties()); } try { final MimeMessage message = new MimeMessage(getSession(), emlContentInputStream); return new ReceivedEmail(message, false, null); } finally { StreamUtil.close(emlContentInputStream); } } }
@Test void testWriteSimpleReceivedEmail() throws FileNotFoundException, MessagingException { final URL data = EMLComposerTest.class.getResource("test"); final File emlFile = new File(data.getFile(), "simple.eml"); ReceivedEmail email = EMLParser.create().parse(emlFile); final String eml = EMLComposer.create().compose(email); assertTrue(eml.contains("From: sender@emailhost.com\r\n")); assertTrue(eml.contains("To: recipient@emailhost.com\r\n")); } }
@Test void testSimpleNullEML() throws FileNotFoundException, MessagingException { final File emlFile = new File(testDataRoot, "simple-null.eml"); final ReceivedEmail email = EMLParser.create().parse(emlFile); assertNull(email.from()); assertEquals("recipient@emailhost.com", email.to()[0].toString()); assertEquals("Email subject", email.subject()); final List<EmailMessage> messages = email.messages(); assertEquals(1, messages.size()); assertEquals(MimeTypes.MIME_TEXT_HTML, messages.get(0).getMimeType()); assertEquals("<p><strong>Project Name: Some Project and the body continues...</p>", messages.get(0).getContent().trim()); final List<EmailAttachment<? extends DataSource>> attachments = email.attachments(); assertEquals(2, attachments.size()); EmailAttachment att = attachments.get(0); assertEquals("no-name.pdf", att.getName()); att = attachments.get(1); assertEquals("no-name.excel", att.getName()); } }
@Test void testParseEMLCyrilic() throws FileNotFoundException, MessagingException { final File emlFile = new File(testDataRoot, "cyrilic.eml"); final ReceivedEmail email = EMLParser.create().parse(emlFile); assertEquals("Tijana <tijan@gmail.com>", email.from().toString()); assertEquals("testapp1@esolut.ions", email.to()[0].toString()); assertEquals("testtest", email.subject()); final List<EmailMessage> messages = email.messages(); assertEquals(2, messages.size()); assertEquals(MimeTypes.MIME_TEXT_PLAIN, messages.get(0).getMimeType()); assertEquals("", messages.get(0).getContent().trim()); assertEquals(MimeTypes.MIME_TEXT_HTML, messages.get(1).getMimeType()); assertEquals("<div dir=\"ltr\"><br></div>", messages.get(1).getContent().trim()); final List<EmailAttachment<? extends DataSource>> attachments = email.attachments(); assertEquals(1, attachments.size()); final EmailAttachment att = attachments.get(0); assertEquals("Copy of РЕКРЕАТИВНА ЕСТЕТСКА ГИМНАСТИКА-флајер - 4.docx", att.getName()); }
/** * Parses EML with provided EML content. * * @param content byte[] with EML content. * @return {@link ReceivedEmail}. * @throws MessagingException if {@link MimeMessage} cannot be created. * @see #parse(InputStream) */ public ReceivedEmail parse(final byte[] content) throws MessagingException { return parse(new ByteArrayInputStream(content)); }
/** * Parses the EML content. If {@link Session} is not created, default one will be used. * * @param emlContentInputStream {@link InputStream} containing the EML content. * @return {@link ReceivedEmail}. * @throws MessagingException if {@link MimeMessage} cannot be created. */ protected ReceivedEmail parse(final InputStream emlContentInputStream) throws MessagingException { if (getSession() == null) { createSession(getProperties()); } try { final MimeMessage message = new MimeMessage(getSession(), emlContentInputStream); return new ReceivedEmail(message, false, null); } finally { StreamUtil.close(emlContentInputStream); } } }
public static EMLParser create() { return new EMLParser(); }
@Test void testSimpleEML() throws FileNotFoundException, MessagingException { final File emlFile = new File(testDataRoot, "simple.eml"); final ReceivedEmail email = EMLParser.create().parse(emlFile); assertEquals("sender@emailhost.com", email.from().toString()); assertEquals("recipient@emailhost.com", email.to()[0].toString()); assertEquals("Email subject", email.subject()); final List<EmailMessage> messages = email.messages(); assertEquals(1, messages.size()); assertEquals(MimeTypes.MIME_TEXT_HTML, messages.get(0).getMimeType()); assertEquals("<p><strong>Project Name: Some Project and the body continues...</p>", messages.get(0).getContent().trim()); final List<EmailAttachment<? extends DataSource>> attachments = email.attachments(); assertEquals(2, attachments.size()); EmailAttachment att = attachments.get(0); assertEquals("AM22831 Cover Sheet.pdf", att.getName()); att = attachments.get(1); assertEquals("AM22831 Manufacturing Status.xls", att.getName()); }
/** * Parses EML with provided EML content. * * @param emlContent {@link String} with EML content. * @param charset String with charset. * @return {@link ReceivedEmail}. * @throws UnsupportedEncodingException if the named charset is not supported. * @throws MessagingException if {@link MimeMessage} cannot be created. * @see #parse(byte[]) */ public ReceivedEmail parse(final String emlContent, final String charset) throws UnsupportedEncodingException, MessagingException { final byte[] bytes = emlContent.getBytes(charset); return parse(bytes); }
/** * Starts EML parsing with provided EML {@link File}. * * @param emlFile {@link File} with EML content. * @return {@link ReceivedEmail}. * @throws FileNotFoundException if emlFile cannot be found * @throws MessagingException if {@link MimeMessage} cannot be created. * @see #parse(InputStream) */ public ReceivedEmail parse(final File emlFile) throws FileNotFoundException, MessagingException { final FileInputStream fileInputStream = new FileInputStream(emlFile); try { return parse(fileInputStream); } finally { StreamUtil.close(fileInputStream); } }
/** * Parses EML with provided EML content. * * @param emlContent {@link String} with EML content. * @return {@link ReceivedEmail}. * @throws MessagingException if {@link MimeMessage} cannot be created. * @see #parse(String, String) */ public ReceivedEmail parse(final String emlContent) throws MessagingException { try { return parse(emlContent, JoddCore.encoding); } catch (final UnsupportedEncodingException ignore) { return null; } }
/** * Parses EML with provided EML content. * * @param content byte[] with EML content. * @return {@link ReceivedEmail}. * @throws MessagingException if {@link MimeMessage} cannot be created. * @see #parse(InputStream) */ public ReceivedEmail parse(final byte[] content) throws MessagingException { return parse(new ByteArrayInputStream(content)); }
/** * Parses EML with provided EML content. * * @param emlContent {@link String} with EML content. * @param charset String with charset. * @return {@link ReceivedEmail}. * @throws UnsupportedEncodingException if the named charset is not supported. * @throws MessagingException if {@link MimeMessage} cannot be created. * @see #parse(byte[]) */ public ReceivedEmail parse(final String emlContent, final String charset) throws UnsupportedEncodingException, MessagingException { final byte[] bytes = emlContent.getBytes(charset); return parse(bytes); }
/** * Starts EML parsing with provided EML {@link File}. * * @param emlFile {@link File} with EML content. * @return {@link ReceivedEmail}. * @throws FileNotFoundException if emlFile cannot be found * @throws MessagingException if {@link MimeMessage} cannot be created. * @see #parse(InputStream) */ public ReceivedEmail parse(final File emlFile) throws FileNotFoundException, MessagingException { final FileInputStream fileInputStream = new FileInputStream(emlFile); try { return parse(fileInputStream); } finally { StreamUtil.close(fileInputStream); } }