List of usage examples for javax.mail.util SharedByteArrayInputStream SharedByteArrayInputStream
public SharedByteArrayInputStream(byte[] buf)
From source file:com.zimbra.cs.mime.MimeTest.java
@Test public void imgCid() throws Exception { String content = baseMpMixedContent + "------------1111971890AC3BB91\r\n" + "Content-Type: text/html; charset=windows-1250\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n\r\n" + "<html>Email with img<img src=\"cid:12345_testemail\"/></html>\r\n" + "------------1111971890AC3BB91\r\n" + "Content-Type: image/jpeg;\r\n" + "name=\"img.jpg\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-ID: <12345_testemail>\r\n\r\n" + "R0a1231312ad124svsdsal=="; //obviously not a real image MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes())); List<MPartInfo> parts = Mime.getParts(mm); Assert.assertNotNull(parts);/* ww w . j av a 2s . c o m*/ Assert.assertEquals(3, parts.size()); MPartInfo mpart = parts.get(0); Assert.assertEquals("multipart/mixed", mpart.getContentType()); List<MPartInfo> children = mpart.getChildren(); Assert.assertEquals(2, children.size()); Set<MPartInfo> bodies = Mime.getBody(parts, false); Assert.assertEquals(1, bodies.size()); MPartInfo body = bodies.iterator().next(); Assert.assertEquals("text/html", body.getContentType()); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void bracketsInMailAndRcpt() throws Exception { server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO .sendLine("250 OK").recvLine() // MAIL FROM .sendLine("250 OK").recvLine() // RCPT TO .sendLine("250 OK").recvLine() // DATA .sendLine("354 OK").swallowUntil("\r\n.\r\n").sendLine("250 OK").recvLine() // QUIT .sendLine("221 bye").build().start(PORT); Session session = JMSession.getSession(); session.getProperties().setProperty("mail.smtp.from", "<>"); Transport transport = session.getTransport("smtp"); transport.connect("localhost", PORT, null, null); String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest"; MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))); transport.sendMessage(msg, new Address[] { new InternetAddress("<rcpt@zimbra.com>") }); transport.close();// w w w .j a v a 2 s.c om server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<>\r\n", server.replay()); Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay()); Assert.assertEquals("DATA\r\n", server.replay()); Assert.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mime.MimeTest.java
@Test public void textCid() throws Exception { String content = baseMpMixedContent + "------------1111971890AC3BB91\r\n" + "Content-Type: text/html; charset=windows-1250\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + "Content-ID: <text_testemail>\r\n\r\n" //barely valid, but we shouldn't break if a bad agent sends like this + "<html>Email with img<img src=\"cid:12345_testemail\"/></html>\r\n" + "------------1111971890AC3BB91\r\n" + "Content-Type: image/jpeg;\r\n" + "name=\"img.jpg\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-ID: <12345_testemail>\r\n\r\n" + "R0a1231312ad124svsdsal=="; //obviously not a real image MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes())); List<MPartInfo> parts = Mime.getParts(mm); Assert.assertNotNull(parts);// w ww .j a v a 2 s . c o m Assert.assertEquals(3, parts.size()); MPartInfo mpart = parts.get(0); Assert.assertEquals("multipart/mixed", mpart.getContentType()); List<MPartInfo> children = mpart.getChildren(); Assert.assertEquals(2, children.size()); Set<MPartInfo> bodies = Mime.getBody(parts, false); Assert.assertEquals(1, bodies.size()); MPartInfo body = bodies.iterator().next(); Assert.assertEquals("text/html", body.getContentType()); }
From source file:com.zimbra.qa.unittest.TestUserServlet.java
private void verifyZipFile(ZMailbox mbox, String relativePath, boolean hasBody) throws Exception { InputStream in = mbox.getRESTResource(relativePath); ZipInputStream zipIn = new ZipInputStream(in); ZipEntry entry;/*from www. j a v a 2s .c o m*/ boolean foundMessage = false; while ((entry = zipIn.getNextEntry()) != null) { if (entry.getName().endsWith(".eml")) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); ByteUtil.copy(zipIn, false, buf, true); byte[] content = buf.toByteArray(); MimeMessage message = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content)); byte[] body = ByteUtil.getContent(message.getInputStream(), 0); if (hasBody) { assertTrue(entry.getName() + " has no body", body.length > 0); } else { assertEquals(entry.getName() + " has a body", 0, body.length); } foundMessage = true; } } zipIn.close(); assertTrue(foundMessage); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void authLogin() throws Exception { server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO .sendLine("250-smtp.zimbra.com").sendLine("250 AUTH LOGIN").recvLine() // AUTH LOGIN .sendLine("334 OK").recvLine() // USER .sendLine("334").recvLine() // PASSWORD .sendLine("235 Authentication successful").recvLine() // MAIL FROM .sendLine("250 OK").recvLine() // RCPT TO .sendLine("250 OK").recvLine() // DATA .sendLine("354 OK").swallowUntil("\r\n.\r\n").sendLine("250 OK").recvLine() // QUIT .sendLine("221 bye").build().start(PORT); Session session = JMSession.getSession(); Transport transport = session.getTransport("smtp"); transport.connect("localhost", PORT, "zimbra", "secret"); String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest"; MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))); transport.sendMessage(msg, msg.getAllRecipients()); transport.close();// w w w . java 2 s . co m server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("AUTH LOGIN\r\n", server.replay()); Assert.assertEquals(base64("zimbra") + "\r\n", server.replay()); Assert.assertEquals(base64("secret") + "\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay()); Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay()); Assert.assertEquals("DATA\r\n", server.replay()); Assert.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mime.MimeTest.java
@Test public void imgNoCid() throws Exception { String content = baseMpMixedContent + "------------1111971890AC3BB91\r\n" + "Content-Type: text/html; charset=windows-1250\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n\r\n" + "<html>Email no img</html>\r\n" + "------------1111971890AC3BB91\r\n" + "Content-Type: image/jpeg;\r\n" + "name=\"img.jpg\"\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n" //no CID here, so sender means for us to show it as body + "R0a1231312ad124svsdsal=="; //obviously not a real image MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes())); List<MPartInfo> parts = Mime.getParts(mm); Assert.assertNotNull(parts);/*from w ww. j av a2 s . co m*/ Assert.assertEquals(3, parts.size()); MPartInfo mpart = parts.get(0); Assert.assertEquals("multipart/mixed", mpart.getContentType()); List<MPartInfo> children = mpart.getChildren(); Assert.assertEquals(2, children.size()); Set<MPartInfo> bodies = Mime.getBody(parts, false); Assert.assertEquals(2, bodies.size()); Set<String> types = Sets.newHashSet("text/html", "image/jpeg"); for (MPartInfo body : bodies) { Assert.assertTrue("Expected: " + body.getContentType(), types.remove(body.getContentType())); } }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void authPlain() throws Exception { server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO .sendLine("250-smtp.zimbra.com").sendLine("250 AUTH PLAIN").recvLine() // AUTH PLAIN initial-response .sendLine("235 Authentication successful").recvLine() // MAIL FROM .sendLine("250 OK").recvLine() // RCPT TO .sendLine("250 OK").recvLine() // DATA .sendLine("354 OK").swallowUntil("\r\n.\r\n").sendLine("250 OK").recvLine() // QUIT .sendLine("221 bye").build().start(PORT); Session session = JMSession.getSession(); Transport transport = session.getTransport("smtp"); transport.connect("localhost", PORT, "zimbra", "secret"); String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest"; MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))); transport.sendMessage(msg, msg.getAllRecipients()); transport.close();//from ww w. ja v a 2 s . c o m server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("AUTH PLAIN " + base64("\0zimbra\0secret") + "\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay()); Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay()); Assert.assertEquals("DATA\r\n", server.replay()); Assert.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mime.MimeTest.java
private void fileAsStream(String extension, boolean expectText) throws MessagingException, IOException { if (extension.charAt(0) == '.') { extension = extension.substring(1); }//ww w . j a v a 2 s . c om String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n" + "Content-Type: application/octet-stream;name=\"test." + extension + "\"\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n" + "R0a1231312ad124svsdsal=="; //obviously not a real file MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes())); MimePart part = Mime.getMimePart(mm, "1"); String expectedType = expectText ? "text/plain" : "application/octet-stream"; Assert.assertEquals(expectedType, Mime.getContentType(part.getContentType())); }
From source file:org.jasig.portlet.emailpreview.dao.javamail.JavamailAccountDaoImpl.java
private EmailMessage wrapMessage(Message msg, boolean populateContent, Session session) throws MessagingException, IOException, ScanException, PolicyException { // Prepare subject String subject = msg.getSubject(); if (!StringUtils.isBlank(subject)) { AntiSamy as = new AntiSamy(); CleanResults cr = as.scan(subject, policy); subject = cr.getCleanHTML();/*from w w w .jav a 2 s . c o m*/ } // Prepare content if requested EmailMessageContent msgContent = null; // default... if (populateContent) { // Defend against the dreaded: "Unable to load BODYSTRUCTURE" try { msgContent = getMessageContent(msg.getContent(), msg.getContentType()); } catch (MessagingException me) { // We are unable to read digitally-signed messages (perhaps // others?) in the API-standard way; we have to use a work around. // See: http://www.oracle.com/technetwork/java/faq-135477.html#imapserverbug // Logging as DEBUG because this behavior is known & expected. log.debug("Difficulty reading a message (digitally signed?). Attempting workaround..."); try { MimeMessage mm = (MimeMessage) msg; ByteArrayOutputStream bos = new ByteArrayOutputStream(); mm.writeTo(bos); bos.close(); SharedByteArrayInputStream bis = new SharedByteArrayInputStream(bos.toByteArray()); MimeMessage copy = new MimeMessage(session, bis); bis.close(); msgContent = getMessageContent(copy.getContent(), copy.getContentType()); } catch (Throwable t) { log.error("Failed to read message body", t); msgContent = new EmailMessageContent("UNABLE TO READ MESSAGE BODY: " + t.getMessage(), false); } } // Sanitize with AntiSamy String content = msgContent.getContentString(); if (!StringUtils.isBlank(content)) { AntiSamy as = new AntiSamy(); CleanResults cr = as.scan(content, policy); content = cr.getCleanHTML(); } msgContent.setContentString(content); } int messageNumber = msg.getMessageNumber(); // Prepare the UID if present String uid = null; // default if (msg.getFolder() instanceof UIDFolder) { uid = Long.toString(((UIDFolder) msg.getFolder()).getUID(msg)); } Address[] addr = msg.getFrom(); String sender = getFormattedAddresses(addr); Date sentDate = msg.getSentDate(); boolean unread = !msg.isSet(Flag.SEEN); boolean answered = msg.isSet(Flag.ANSWERED); boolean deleted = msg.isSet(Flag.DELETED); // Defend against the dreaded: "Unable to load BODYSTRUCTURE" boolean multipart = false; // sensible default; String contentType = null; // sensible default try { multipart = msg.getContentType().toLowerCase().startsWith(CONTENT_TYPE_ATTACHMENTS_PATTERN); contentType = msg.getContentType(); } catch (MessagingException me) { // Message was digitally signed and we are unable to read it; // logging as DEBUG because this issue is known/expected, and // because the user's experience is in no way affected (at this point) log.debug("Message content unavailable (digitally signed?); " + "message will appear in the preview table correctly, " + "but the body will not be viewable"); log.trace(me.getMessage(), me); } String to = getTo(msg); String cc = getCc(msg); String bcc = getBcc(msg); return new EmailMessage(messageNumber, uid, sender, subject, sentDate, unread, answered, deleted, multipart, contentType, msgContent, to, cc, bcc); }
From source file:com.zimbra.cs.mime.MimeTest.java
@Test public void pdfAsStream() throws Exception { String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n" + "Content-Type: application/octet-stream;name=\"test.pdf\"\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n" + "R0a1231312ad124svsdsal=="; //obviously not a real pdf MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes())); MimePart part = Mime.getMimePart(mm, "1"); Assert.assertEquals("application/octet-stream", Mime.getContentType(part.getContentType())); }