Search in sources :

Example 1 with LmtpClient

use of com.zimbra.common.lmtp.LmtpClient in project zm-mailbox by Zimbra.

the class ZimbraLmtpBackend method deliverMessageToRemoteMailboxes.

private void deliverMessageToRemoteMailboxes(Blob blob, byte[] data, LmtpEnvelope env) {
    Multimap<String, LmtpAddress> serverToRecipientsMap = env.getRemoteServerToRecipientsMap();
    for (String server : serverToRecipientsMap.keySet()) {
        LmtpClient lmtpClient = null;
        InputStream in = null;
        Collection<LmtpAddress> serverRecipients = serverToRecipientsMap.get(server);
        try {
            Server serverObj = Provisioning.getInstance().getServerByName(server);
            lmtpClient = new LmtpClient(server, new Integer(serverObj.getAttr(Provisioning.A_zimbraLmtpBindPort)));
            in = data == null ? blob.getInputStream() : new ByteArrayInputStream(data);
            boolean success = lmtpClient.sendMessage(in, getRecipientsEmailAddress(serverRecipients), env.getSender().getEmailAddress(), blob.getFile().getName(), blob.getRawSize());
            if (success) {
                setDeliveryStatuses(serverRecipients, LmtpReply.DELIVERY_OK);
            } else {
                ZimbraLog.lmtp.warn("Unsuccessful remote mail delivery - LMTP response: %s", lmtpClient.getResponse());
                setDeliveryStatuses(serverRecipients, LmtpReply.TEMPORARY_FAILURE);
            }
        } catch (LmtpProtocolException e) {
            ZimbraLog.lmtp.warn("Unsuccessful remote mail delivery - LMTP response: %s", e.getMessage());
            setDeliveryStatuses(serverRecipients, LmtpReply.TEMPORARY_FAILURE);
        } catch (Exception e) {
            ZimbraLog.lmtp.warn("Exception delivering remote mail", e);
            setDeliveryStatuses(serverRecipients, LmtpReply.TEMPORARY_FAILURE);
        } finally {
            ByteUtil.closeStream(in);
            if (lmtpClient != null) {
                lmtpClient.close();
            }
        }
    }
}
Also used : LmtpClient(com.zimbra.common.lmtp.LmtpClient) Server(com.zimbra.cs.account.Server) LmtpProtocolException(com.zimbra.common.lmtp.LmtpProtocolException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Rfc822ValidationInputStream(com.zimbra.common.mime.Rfc822ValidationInputStream) CopyInputStream(com.zimbra.common.util.CopyInputStream) BlobInputStream(com.zimbra.cs.store.BlobInputStream) InputStream(java.io.InputStream) MessagingException(javax.mail.MessagingException) LmtpProtocolException(com.zimbra.common.lmtp.LmtpProtocolException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Example 2 with LmtpClient

use of com.zimbra.common.lmtp.LmtpClient in project zm-mailbox by Zimbra.

the class TestUtil method addMessageLmtp.

public static boolean addMessageLmtp(String[] recipients, String sender, String message) throws Exception {
    String[] recipWithDomain = new String[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
        recipWithDomain[i] = addDomainIfNecessary(recipients[i]);
    }
    Provisioning prov = Provisioning.getInstance();
    LmtpClient lmtp = new LmtpClient("localhost", prov.getLocalServer().getIntAttr(Provisioning.A_zimbraLmtpBindPort, 7025));
    byte[] data = message.getBytes();
    String senderAddress = "";
    if (!StringUtil.isNullOrEmpty(sender)) {
        senderAddress = addDomainIfNecessary(sender);
    }
    boolean success = lmtp.sendMessage(new ByteArrayInputStream(data), recipWithDomain, senderAddress, "TestUtil", (long) data.length);
    lmtp.close();
    return success;
}
Also used : LmtpClient(com.zimbra.common.lmtp.LmtpClient) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) ZMountpoint(com.zimbra.client.ZMountpoint) Provisioning(com.zimbra.cs.account.Provisioning) SoapProvisioning(com.zimbra.cs.account.soap.SoapProvisioning)

Example 3 with LmtpClient

use of com.zimbra.common.lmtp.LmtpClient in project zm-mailbox by Zimbra.

the class TestLmtp method testLhloNotSendByClient.

@Test
public void testLhloNotSendByClient() throws Exception {
    String[] commands = new String[] { NOOP, RSET, VRFY + " " + USER_NAME, "MAIL FROM:<" + TestUtil.addDomainIfNecessary(USER_NAME) + ">" };
    LmtpClient lmtpClient;
    Provisioning prov = Provisioning.getInstance();
    boolean replyOk;
    boolean lhloRequired = prov.getLocalServer().getBooleanAttr(Provisioning.A_zimbraLmtpLHLORequired, true);
    for (String command : commands) {
        lmtpClient = new LmtpClient("localhost", Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraLmtpBindPort, 7025));
        Assert.assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
        lmtpClient.sendLine(command);
        replyOk = lmtpClient.replyOk();
        Assert.assertTrue("Response :" + lmtpClient.getResponse() + " for command :" + command, lhloRequired ? !replyOk : replyOk);
        lmtpClient.abruptClose();
    }
}
Also used : LmtpClient(com.zimbra.common.lmtp.LmtpClient) Provisioning(com.zimbra.cs.account.Provisioning) Test(org.junit.Test)

Example 4 with LmtpClient

use of com.zimbra.common.lmtp.LmtpClient in project zm-mailbox by Zimbra.

the class TestLmtp method testErrorWhenNoStartTlsOnSslEnforcedByServer.

@Test
public void testErrorWhenNoStartTlsOnSslEnforcedByServer() throws Exception {
    boolean tlsEnforcedByServer = LC.zimbra_require_interprocess_security.booleanValue();
    if (tlsEnforcedByServer) {
        LmtpClient lmtpClient = new LmtpClient("localhost", Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraLmtpBindPort, 7025));
        Assert.assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
        lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
        Assert.assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
        if (lmtpClient.getResponse().contains("STARTTLS")) {
            lmtpClient.sendLine("MAIL FROM:<" + TestUtil.addDomainIfNecessary(USER_NAME) + ">");
            Assert.assertTrue(lmtpClient.getResponse(), !lmtpClient.replyOk());
        }
        lmtpClient.abruptClose();
    }
}
Also used : LmtpClient(com.zimbra.common.lmtp.LmtpClient) Test(org.junit.Test)

Example 5 with LmtpClient

use of com.zimbra.common.lmtp.LmtpClient in project zm-mailbox by Zimbra.

the class TestLmtp method testStartTLSSuccess.

@Test
public void testStartTLSSuccess() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    LmtpClient lmtpClient = new LmtpClient("localhost", Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraLmtpBindPort, 7025));
    Assert.assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
    lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
    Assert.assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
    if (lmtpClient.getResponse().contains(STARTTLS)) {
        lmtpClient.startTLS();
        lmtpClient.sendLine("LHLO " + LC.zimbra_server_hostname.value());
        Assert.assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
    }
    lmtpClient.sendLine("MAIL FROM:<" + TestUtil.addDomainIfNecessary(USER_NAME) + ">");
    Assert.assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
    lmtpClient.sendLine("RCPT TO:<" + TestUtil.addDomainIfNecessary(USER_NAME) + ">");
    Assert.assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
    lmtpClient.sendLine("DATA");
    Assert.assertTrue(lmtpClient.getResponse(), lmtpClient.replyOk());
    String subject = NAME_PREFIX + " testFinalDotNotSent";
    lmtpClient.sendLine("Subject: " + subject);
    lmtpClient.abruptClose();
    // wait for some time
    Thread.sleep(1000);
    List<ZMessage> msgs = TestUtil.search(mbox, "in:inbox " + subject);
    Assert.assertTrue("msg got delivered via LMTP even though <CRLF>.<CRLF> was not received", msgs.isEmpty());
}
Also used : ZMessage(com.zimbra.client.ZMessage) LmtpClient(com.zimbra.common.lmtp.LmtpClient) ZMailbox(com.zimbra.client.ZMailbox) Test(org.junit.Test)

Aggregations

LmtpClient (com.zimbra.common.lmtp.LmtpClient)9 Test (org.junit.Test)7 ZMailbox (com.zimbra.client.ZMailbox)3 ZMessage (com.zimbra.client.ZMessage)3 Provisioning (com.zimbra.cs.account.Provisioning)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ZGetMessageParams (com.zimbra.client.ZGetMessageParams)1 ZMountpoint (com.zimbra.client.ZMountpoint)1 LmtpProtocolException (com.zimbra.common.lmtp.LmtpProtocolException)1 Rfc822ValidationInputStream (com.zimbra.common.mime.Rfc822ValidationInputStream)1 DeliveryServiceException (com.zimbra.common.service.DeliveryServiceException)1 ServiceException (com.zimbra.common.service.ServiceException)1 CopyInputStream (com.zimbra.common.util.CopyInputStream)1 Server (com.zimbra.cs.account.Server)1 SoapProvisioning (com.zimbra.cs.account.soap.SoapProvisioning)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 BlobInputStream (com.zimbra.cs.store.BlobInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MessagingException (javax.mail.MessagingException)1