Search in sources :

Example 26 with Session

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());
}
Also used : ByteBuilder(com.zimbra.common.zmime.ZMimeUtility.ByteBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Base64EncoderStream(com.zimbra.common.zmime.ZTransferEncoding.Base64EncoderStream) Properties(java.util.Properties) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 27 with Session

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());
}
Also used : SendFailedException(javax.mail.SendFailedException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 28 with Session

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());
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 29 with Session

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);
}
Also used : MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) Test(org.junit.Test)

Example 30 with Session

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);
}
Also used : MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) Test(org.junit.Test)

Aggregations

Session (javax.mail.Session)110 MimeMessage (javax.mail.internet.MimeMessage)72 Properties (java.util.Properties)66 InternetAddress (javax.mail.internet.InternetAddress)49 MessagingException (javax.mail.MessagingException)47 Message (javax.mail.Message)31 Test (org.junit.Test)30 Transport (javax.mail.Transport)28 JMSession (com.zimbra.cs.util.JMSession)20 PasswordAuthentication (javax.mail.PasswordAuthentication)19 Date (java.util.Date)17 MimeBodyPart (javax.mail.internet.MimeBodyPart)16 MimeMultipart (javax.mail.internet.MimeMultipart)16 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)15 IOException (java.io.IOException)15 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)15 Authenticator (javax.mail.Authenticator)12 Multipart (javax.mail.Multipart)11 NoSuchProviderException (javax.mail.NoSuchProviderException)10 BodyPart (javax.mail.BodyPart)9