use of javax.mail.Session in project zm-mailbox by Zimbra.
the class ZMimeMultipartTest method encoded.
@Test
public void encoded() throws Exception {
final String boundary = "dfghjkl";
final String preamble = "when in the course of human events...\r\n";
final String plain = "The Rain in Spain.";
final String html = "The <u>Rain</u> in <em>Spain</em>.";
ByteBuilder bbheader = new ByteBuilder();
bbheader.append("From: test@example.com\r\n");
bbheader.append("To: rcpt@example.com\r\n");
bbheader.append("Subject: message subject\r\n");
bbheader.append("Message-ID: <11e1-b0c4-0800200c9a66@example.com>\r\n");
bbheader.append("Content-Transfer-Encoding: base64\r\n");
bbheader.append("Content-Type: multipart/alternative; boundary=").append(boundary).append("\r\n");
bbheader.append("\r\n");
ByteBuilder bbmulti = new ByteBuilder();
bbmulti.append(preamble);
bbmulti.append("--").append(boundary).append("\r\n");
bbmulti.append("Content-Type: text/plain\r\n");
bbmulti.append("\r\n");
bbmulti.append(plain).append("\r\n");
bbmulti.append("--").append(boundary).append("\r\n");
bbmulti.append("Content-Type: text/html\r\n");
bbmulti.append("\r\n");
bbmulti.append(html).append("\r\n");
bbmulti.append("--").append(boundary).append("--\r\n");
// message with CTE header and base64-encoded body
ByteBuilder bb = new ByteBuilder();
bb.append(bbheader);
bb.append(ByteUtil.getContent(new Base64EncoderStream(new ByteArrayInputStream(bbmulti.toByteArray())), -1));
Session s = Session.getDefaultInstance(new Properties());
ZMimeMessage mm = new ZMimeMessage(s, new SharedByteArrayInputStream(bb.toByteArray()));
Object o = mm.getContent();
Assert.assertTrue("content is ZMimeMultipart", o instanceof ZMimeMultipart);
ZMimeMultipart multi = (ZMimeMultipart) o;
Assert.assertEquals("preamble matches", preamble, multi.getPreamble());
Assert.assertEquals("2 subparts", 2, multi.getCount());
Assert.assertEquals("part 1 content match", plain, multi.getBodyPart(0).getContent());
Assert.assertEquals("part 2 content match", html, multi.getBodyPart(1).getContent());
// message with CTE header and nonencoded body
bb = new ByteBuilder();
bb.append(bbheader);
bb.append(bbmulti);
mm = new ZMimeMessage(s, new SharedByteArrayInputStream(bb.toByteArray()));
o = mm.getContent();
Assert.assertTrue("content is ZMimeMultipart", o instanceof ZMimeMultipart);
multi = (ZMimeMultipart) o;
Assert.assertEquals("preamble matches", preamble, multi.getPreamble());
Assert.assertEquals("2 subparts", 2, multi.getCount());
Assert.assertEquals("part 1 content match", plain, multi.getBodyPart(0).getContent());
Assert.assertEquals("part 2 content match", html, multi.getBodyPart(1).getContent());
}
use of javax.mail.Session in project zm-mailbox by Zimbra.
the class SmtpTransportTest method dataError.
@Test(timeout = 3000)
public void dataError() throws Exception {
server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("451 error").recvLine().build().start(PORT);
Session session = JMSession.getSession();
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)));
try {
transport.sendMessage(msg, msg.getAllRecipients());
Assert.fail();
} catch (SendFailedException e) {
Assert.assertEquals(1, e.getValidSentAddresses().length);
Assert.assertEquals(0, e.getValidUnsentAddresses().length);
Assert.assertEquals(0, e.getInvalidAddresses().length);
} finally {
transport.close();
}
server.shutdown(1000);
Assert.assertEquals("EHLO localhost\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());
}
use of javax.mail.Session in project zm-mailbox by Zimbra.
the class SmtpTransportTest method quitNoResponse.
@Test(timeout = 3000)
public void quitNoResponse() throws Exception {
server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("354 OK").swallowUntil("\r\n.\r\n").sendLine("250 OK").recvLine().build().start(PORT);
Session session = JMSession.getSession();
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, msg.getAllRecipients());
transport.close();
server.shutdown(1000);
Assert.assertEquals("EHLO localhost\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());
}
use of javax.mail.Session in project zm-mailbox by Zimbra.
the class SmtpTransportTest method noAuthMechansims.
@Test(timeout = 3000)
public void noAuthMechansims() throws Exception {
server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250-OK").sendLine("250 AUTH NTLM").build().start(PORT);
Session session = JMSession.getSession();
Transport transport = session.getTransport("smtp");
try {
transport.connect("localhost", PORT, "zimbra", "secret");
Assert.fail();
} catch (MessagingException e) {
Assert.assertEquals("No auth mechanism supported: [NTLM]", e.getMessage());
}
server.shutdown(1000);
}
use of javax.mail.Session in project zm-mailbox by Zimbra.
the class SmtpTransportTest method noAuth.
@Test(timeout = 3000)
public void noAuth() throws Exception {
server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").build().start(PORT);
Session session = JMSession.getSession();
Transport transport = session.getTransport("smtp");
try {
transport.connect("localhost", PORT, "zimbra", "secret");
Assert.fail();
} catch (MessagingException e) {
Assert.assertEquals("The server doesn't support SMTP-AUTH.", e.getMessage());
}
server.shutdown(1000);
}
Aggregations