Search in sources :

Example 11 with Transport

use of javax.mail.Transport 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 12 with Transport

use of javax.mail.Transport 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 13 with Transport

use of javax.mail.Transport 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)

Example 14 with Transport

use of javax.mail.Transport in project zm-mailbox by Zimbra.

the class DataSourceManager method test.

/*
     * Tests connecting to a data source.  Do not actually create the
     * data source.
     */
public static void test(DataSource ds) throws ServiceException {
    ZimbraLog.datasource.info("Testing: %s", ds);
    try {
        DataImport di = getInstance().getDataImport(ds, true);
        di.test();
        if (ds.isSmtpEnabled()) {
            Session session = JMSession.getSession(ds);
            Transport smtp = session.getTransport();
            String emailAddress = ds.getEmailAddress();
            if (smtp instanceof SmtpTransport) {
                test((SmtpTransport) smtp, emailAddress);
            } else {
                test((SMTPTransport) smtp, emailAddress);
            }
        }
        ZimbraLog.datasource.info("Test succeeded: %s", ds);
    } catch (ServiceException x) {
        ZimbraLog.datasource.info("Test failed: %s", ds, x);
        throw x;
    } catch (Exception e) {
        ZimbraLog.datasource.info("Test failed: %s", ds, e);
        throw ServiceException.INVALID_REQUEST("Datasource test failed", e);
    }
}
Also used : DataImport(com.zimbra.cs.account.DataSource.DataImport) ServiceException(com.zimbra.common.service.ServiceException) SmtpTransport(com.zimbra.cs.mailclient.smtp.SmtpTransport) Transport(javax.mail.Transport) SmtpTransport(com.zimbra.cs.mailclient.smtp.SmtpTransport) SMTPTransport(com.sun.mail.smtp.SMTPTransport) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session)

Example 15 with Transport

use of javax.mail.Transport in project zm-mailbox by Zimbra.

the class MailSender method sendMessageToHost.

private void sendMessageToHost(String hostname, MimeMessage mm, Address[] rcptAddresses) throws MessagingException {
    mSession.getProperties().setProperty("mail.smtp.host", hostname);
    if (mEnvelopeFrom != null) {
        mSession.getProperties().setProperty("mail.smtp.from", mEnvelopeFrom);
    }
    ZimbraLog.smtp.debug("Sending message %s to SMTP host %s with properties: %s", mm.getMessageID(), hostname, mSession.getProperties());
    Transport transport = mSession.getTransport("smtp");
    try {
        transport.connect();
        transport.sendMessage(mm, rcptAddresses);
    } finally {
        transport.close();
    }
}
Also used : Transport(javax.mail.Transport)

Aggregations

Transport (javax.mail.Transport)41 Session (javax.mail.Session)28 MimeMessage (javax.mail.internet.MimeMessage)24 JMSession (com.zimbra.cs.util.JMSession)18 MessagingException (javax.mail.MessagingException)18 Test (org.junit.Test)16 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)14 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)14 InternetAddress (javax.mail.internet.InternetAddress)11 Properties (java.util.Properties)9 Date (java.util.Date)7 Message (javax.mail.Message)6 NoSuchProviderException (javax.mail.NoSuchProviderException)6 SendFailedException (javax.mail.SendFailedException)6 IOException (java.io.IOException)5 SMTPMessage (com.sun.mail.smtp.SMTPMessage)2 SMTPTransport (com.sun.mail.smtp.SMTPTransport)2 Address (javax.mail.Address)2 Multipart (javax.mail.Multipart)2 AddressException (javax.mail.internet.AddressException)2