Search in sources :

Example 96 with Session

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

the class SmtpTransportTest method mailSmtpFrom.

@Test(timeout = 3000)
public void mailSmtpFrom() 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().sendLine("221 bye").build().start(PORT);
    Session session = JMSession.getSession();
    session.getProperties().setProperty("mail.smtp.from", "from@zimbra.com");
    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:<from@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 97 with Session

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

the class SmtpTransportTest method dataException.

@Test(timeout = 3000)
public void dataException() 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").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))) {

        @Override
        public void writeTo(OutputStream os, String[] ignoreList) throws MessagingException {
            // exception while encoding
            throw new MessagingException();
        }
    };
    try {
        transport.sendMessage(msg, msg.getAllRecipients());
        Assert.fail();
    } catch (SendFailedException expected) {
    } 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.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) MessagingException(javax.mail.MessagingException) OutputStream(java.io.OutputStream) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 98 with Session

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

the class SmtpTransportTest method rcptToError.

@Test(timeout = 3000)
public void rcptToError() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("550 error").recvLine().build().start(PORT);
    Session session = JMSession.getSession();
    session.getProperties().setProperty("mail.smtp.sendpartial", "true");
    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(0, e.getValidSentAddresses().length);
        Assert.assertEquals(0, e.getValidUnsentAddresses().length);
        Assert.assertEquals(1, 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("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 99 with Session

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

the class SmtpTransportTest method rset.

@Test(timeout = 3000)
public void rset() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").build().start(PORT);
    Session session = JMSession.getSession();
    SmtpTransport transport = (SmtpTransport) session.getTransport("smtp");
    try {
        transport.connect("localhost", PORT, null, null);
        transport.mail("sender@zimbra.com");
        transport.rset();
    } 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("RSET\r\n", server.replay());
    Assert.assertNull(server.replay());
}
Also used : JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) Test(org.junit.Test)

Example 100 with Session

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

the class Notification method notifyIfNecessary.

/**
     * If the recipient's account is set up for email notification, sends a notification
     * message to the user's notification address.
     */
private void notifyIfNecessary(Account account, Message msg, String rcpt) throws MessagingException, ServiceException {
    // Does user have new mail notification turned on
    boolean notify = account.getBooleanAttr(Provisioning.A_zimbraPrefNewMailNotificationEnabled, false);
    if (!notify) {
        return;
    }
    // Validate notification address
    String destination = account.getAttr(Provisioning.A_zimbraPrefNewMailNotificationAddress);
    if (destination == null) {
        nfailed("destination not set", null, rcpt, msg, null);
        return;
    }
    try {
        new JavaMailInternetAddress(destination);
    } catch (AddressException ae) {
        nfailed("invalid destination", destination, rcpt, msg, ae);
        return;
    }
    // filter rules, we assume it's not interesting.
    if (msg.inSpam()) {
        nfailed("in spam", destination, rcpt, msg);
        return;
    }
    try {
        if (msg.inTrash()) {
            nfailed("in trash", destination, rcpt, msg);
            return;
        }
    } catch (ServiceException e) {
        nfailed("call to Message.inTrash() failed", destination, rcpt, msg, e);
        return;
    }
    // If precedence is bulk or junk
    MimeMessage mm = msg.getMimeMessage();
    String[] precedence = mm.getHeader("Precedence");
    if (hasPrecedence(precedence, "bulk")) {
        nfailed("precedence bulk", destination, rcpt, msg);
        return;
    }
    if (hasPrecedence(precedence, "junk")) {
        nfailed("precedence junk", destination, rcpt, msg);
        return;
    }
    // Check for mail loop
    String[] autoSubmittedHeaders = mm.getHeader("Auto-Submitted");
    if (autoSubmittedHeaders != null) {
        for (int i = 0; i < autoSubmittedHeaders.length; i++) {
            String headerValue = autoSubmittedHeaders[i].toLowerCase();
            if (headerValue.indexOf("notification") != -1) {
                nfailed("detected a mail loop", destination, rcpt, msg);
                return;
            }
        }
    }
    // Send the message
    try {
        Session smtpSession = JMSession.getSmtpSession();
        // Assemble message components
        MimeMessage out = assembleNotificationMessage(account, msg, rcpt, destination, smtpSession);
        if (out == null) {
            return;
        }
        String envFrom = "<>";
        try {
            if (!Provisioning.getInstance().getConfig().getBooleanAttr(Provisioning.A_zimbraAutoSubmittedNullReturnPath, true)) {
                envFrom = account.getName();
            }
        } catch (ServiceException se) {
            ZimbraLog.mailbox.warn("error encoutered looking up return path configuration, using null return path instead", se);
        }
        smtpSession.getProperties().setProperty("mail.smtp.from", envFrom);
        Transport.send(out);
        ZimbraLog.mailbox.info("notification sent dest='" + destination + "' rcpt='" + rcpt + "' mid=" + msg.getId());
    } catch (MessagingException me) {
        nfailed("send failed", destination, rcpt, msg, me);
    }
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session)

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