/*
         * returns message count from pop3 server
         */
        internal static int GetMessageCount(Core.Node ip, Core.Node dp)
        {
            string host = Expressions.GetExpressionValue<string>(ip.GetValue("host", ""), dp, ip, false);
            int port = Expressions.GetExpressionValue<int>(ip.GetValue("port", "-1"), dp, ip, false);
            bool implicitSsl = Expressions.GetExpressionValue<bool>(ip.GetValue("ssl", "false"), dp, ip, false);
            string username = Expressions.GetExpressionValue<string>(ip.GetValue("username", ""), dp, ip, false);
            string password = Expressions.GetExpressionValue<string>(ip.GetValue("password", ""), dp, ip, false);

            using (Pop3Client client = new Pop3Client())
            {
                client.Connect(host, port, implicitSsl);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                if (!string.IsNullOrEmpty(username))
                    client.Authenticate(username, password);
                int retVal = client.GetMessageCount();
                client.Disconnect(true);
                return retVal;
            }
        }
        public void TestAuthenticationExceptions()
        {
            var commands = new List<Pop3ReplayCommand> ();
            commands.Add (new Pop3ReplayCommand ("", "comcast.greeting.txt"));
            commands.Add (new Pop3ReplayCommand ("CAPA\r\n", "comcast.capa1.txt"));
            commands.Add (new Pop3ReplayCommand ("USER username\r\n", "comcast.ok.txt"));
            commands.Add (new Pop3ReplayCommand ("PASS password\r\n", "comcast.err.txt"));
            commands.Add (new Pop3ReplayCommand ("QUIT\r\n", "comcast.quit.txt"));

            using (var client = new Pop3Client ()) {
                try {
                    client.ReplayConnect ("localhost", new Pop3ReplayStream (commands, false), CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Connect: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "Client failed to connect.");

                Assert.AreEqual (ComcastCapa1, client.Capabilities);
                Assert.AreEqual (0, client.AuthenticationMechanisms.Count);
                Assert.AreEqual (31, client.ExpirePolicy);

                try {
                    var credentials = new NetworkCredential ("username", "password");
                    client.Authenticate (credentials, CancellationToken.None);
                    Assert.Fail ("Expected AuthenticationException");
                } catch (AuthenticationException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "AuthenticationException should not cause a disconnect.");

                try {
                    var count = client.GetMessageCount (CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Count: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var sizes = client.GetMessageSizes (CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageSizes: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var size = client.GetMessageSize ("uid", CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageSize(uid): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var size = client.GetMessageSize (0, CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageSize(int): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var uids = client.GetMessageUids (CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageUids: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var uid = client.GetMessageUid (0, CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageUid: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var message = client.GetMessage ("uid", CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessage(uid): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    var message = client.GetMessage (0, CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessage(int): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    client.DeleteMessage ("uid", CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in DeleteMessage(uid): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    client.DeleteMessage (0, CancellationToken.None);
                    Assert.Fail ("Expected UnauthorizedAccessException");
                } catch (UnauthorizedAccessException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in DeleteMessage(int): {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "UnauthorizedAccessException should not cause a disconnect.");

                try {
                    client.Disconnect (true, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Disconnect: {0}", ex);
                }

                Assert.IsFalse (client.IsConnected, "Failed to disconnect");
            }
        }
        public void TestGMailPop3Client()
        {
            var commands = new List<Pop3ReplayCommand> ();
            commands.Add (new Pop3ReplayCommand ("", "gmail.greeting.txt"));
            commands.Add (new Pop3ReplayCommand ("CAPA\r\n", "gmail.capa1.txt"));
            commands.Add (new Pop3ReplayCommand ("AUTH PLAIN\r\n", "gmail.plus.txt"));
            commands.Add (new Pop3ReplayCommand ("AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "gmail.auth.txt"));
            commands.Add (new Pop3ReplayCommand ("CAPA\r\n", "gmail.capa2.txt"));
            commands.Add (new Pop3ReplayCommand ("STAT\r\n", "gmail.stat.txt"));
            commands.Add (new Pop3ReplayCommand ("RETR 1\r\n", "gmail.retr1.txt"));
            commands.Add (new Pop3ReplayCommand ("QUIT\r\n", "gmail.quit.txt"));

            using (var client = new Pop3Client ()) {
                try {
                    client.ReplayConnect ("localhost", new Pop3ReplayStream (commands, false), CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Connect: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "Client failed to connect.");

                Assert.AreEqual (GMailCapa1, client.Capabilities);
                Assert.AreEqual (2, client.AuthenticationMechanisms.Count);
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("XOAUTH2"), "Expected SASL XOAUTH2 auth mechanism");
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism");

                // Note: remove the XOAUTH2 auth mechanism to force PLAIN auth
                client.AuthenticationMechanisms.Remove ("XOAUTH2");

                try {
                    var credentials = new NetworkCredential ("username", "password");
                    client.Authenticate (credentials, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex);
                }

                Assert.AreEqual (GMailCapa2, client.Capabilities);
                Assert.AreEqual (0, client.AuthenticationMechanisms.Count);

                try {
                    var count = client.GetMessageCount (CancellationToken.None);
                    Assert.AreEqual (1, count, "Expected 1 message");
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageCount: {0}", ex);
                }

            //				try {
            //					var uids = client.GetMessageUids (CancellationToken.None);
            //					Assert.AreEqual (7, uids.Length, "Expected 7 uids");
            //				} catch (Exception ex) {
            //					Assert.Fail ("Did not expect an exception in GetMessageUids: {0}", ex);
            //				}

                try {
                    var message = client.GetMessage (0, CancellationToken.None);

                    using (var jpeg = new MemoryStream ()) {
                        var attachment = message.Attachments.FirstOrDefault ();

                        attachment.ContentObject.DecodeTo (jpeg);
                        jpeg.Position = 0;

                        using (var md5 = new MD5CryptoServiceProvider ()) {
                            var md5sum = HexEncode (md5.ComputeHash (jpeg));

                            Assert.AreEqual ("5b1b8b2c9300c9cd01099f44e1155e2b", md5sum, "MD5 checksums do not match.");
                        }
                    }
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessage: {0}", ex);
                }

                try {
                    client.Disconnect (true, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Disconnect: {0}", ex);
                }

                Assert.IsFalse (client.IsConnected, "Failed to disconnect");
            }
        }
        public void TestExchangePop3Client()
        {
            var commands = new List<Pop3ReplayCommand> ();
            commands.Add (new Pop3ReplayCommand ("", "exchange.greeting.txt"));
            commands.Add (new Pop3ReplayCommand ("CAPA\r\n", "exchange.capa.txt"));
            commands.Add (new Pop3ReplayCommand ("AUTH PLAIN\r\n", "exchange.plus.txt"));
            commands.Add (new Pop3ReplayCommand ("AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "exchange.auth.txt"));
            commands.Add (new Pop3ReplayCommand ("CAPA\r\n", "exchange.capa.txt"));
            commands.Add (new Pop3ReplayCommand ("STAT\r\n", "exchange.stat.txt"));
            commands.Add (new Pop3ReplayCommand ("UIDL\r\n", "exchange.uidl.txt"));
            commands.Add (new Pop3ReplayCommand ("RETR 1\r\n", "exchange.retr1.txt"));
            commands.Add (new Pop3ReplayCommand ("QUIT\r\n", "exchange.quit.txt"));

            using (var client = new Pop3Client ()) {
                try {
                    client.ReplayConnect ("localhost", new Pop3ReplayStream (commands, false), CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Connect: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "Client failed to connect.");

                Assert.AreEqual (ExchangeCapa, client.Capabilities);
                Assert.AreEqual (3, client.AuthenticationMechanisms.Count);
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("GSSAPI"), "Expected SASL GSSAPI auth mechanism");
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("NTLM"), "Expected SASL NTLM auth mechanism");
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism");

                // Note: remove these auth mechanisms to force PLAIN auth
                client.AuthenticationMechanisms.Remove ("GSSAPI");
                client.AuthenticationMechanisms.Remove ("NTLM");

                try {
                    var credentials = new NetworkCredential ("username", "password");
                    client.Authenticate (credentials, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex);
                }

                Assert.AreEqual (ExchangeCapa, client.Capabilities);
                Assert.AreEqual (3, client.AuthenticationMechanisms.Count);
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("GSSAPI"), "Expected SASL GSSAPI auth mechanism");
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("NTLM"), "Expected SASL NTLM auth mechanism");
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism");

                try {
                    var count = client.GetMessageCount (CancellationToken.None);
                    Assert.AreEqual (7, count, "Expected 7 messages");
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageCount: {0}", ex);
                }

                try {
                    var uids = client.GetMessageUids (CancellationToken.None);
                    Assert.AreEqual (7, uids.Count, "Expected 7 uids");
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessageUids: {0}", ex);
                }

                try {
                    var message = client.GetMessage (0, CancellationToken.None);
                    // TODO: assert that the message is byte-identical to what we expect
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessage: {0}", ex);
                }

                try {
                    client.Disconnect (true, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Disconnect: {0}", ex);
                }

                Assert.IsFalse (client.IsConnected, "Failed to disconnect");
            }
        }
        public void TestBasicPop3ClientUnixLineEndings()
        {
            var commands = new List<Pop3ReplayCommand> ();
            commands.Add (new Pop3ReplayCommand ("", "comcast.greeting.txt"));
            commands.Add (new Pop3ReplayCommand ("CAPA\r\n", "comcast.capa1.txt"));
            commands.Add (new Pop3ReplayCommand ("USER username\r\n", "comcast.ok.txt"));
            commands.Add (new Pop3ReplayCommand ("PASS password\r\n", "comcast.ok.txt"));
            commands.Add (new Pop3ReplayCommand ("CAPA\r\n", "comcast.capa2.txt"));
            commands.Add (new Pop3ReplayCommand ("STAT\r\n", "comcast.stat1.txt"));
            commands.Add (new Pop3ReplayCommand ("RETR 1\r\n", "comcast.retr1.txt"));
            commands.Add (new Pop3ReplayCommand ("QUIT\r\n", "comcast.quit.txt"));

            using (var client = new Pop3Client ()) {
                try {
                    client.ReplayConnect ("localhost", new Pop3ReplayStream (commands, true), CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Connect: {0}", ex);
                }

                Assert.IsTrue (client.IsConnected, "Client failed to connect.");

                Assert.AreEqual (ComcastCapa1, client.Capabilities);
                Assert.AreEqual (0, client.AuthenticationMechanisms.Count);
                Assert.AreEqual (31, client.ExpirePolicy);

                try {
                    var credentials = new NetworkCredential ("username", "password");
                    client.Authenticate (credentials, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Authenticate: {0}", ex);
                }

                Assert.AreEqual (ComcastCapa2, client.Capabilities);
                Assert.AreEqual ("ZimbraInc", client.Implementation);
                Assert.AreEqual (2, client.AuthenticationMechanisms.Count);
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("PLAIN"), "Expected SASL PLAIN auth mechanism");
                Assert.IsTrue (client.AuthenticationMechanisms.Contains ("X-ZIMBRA"), "Expected SASL X-ZIMBRA auth mechanism");
                Assert.AreEqual (-1, client.ExpirePolicy);

                try {
                    var count = client.GetMessageCount (CancellationToken.None);
                    Assert.AreEqual (1, count, "Expected 1 message");
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Count: {0}", ex);
                }

                try {
                    var message = client.GetMessage (0, CancellationToken.None);
                    // TODO: assert that the message is byte-identical to what we expect
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in GetMessage: {0}", ex);
                }

                try {
                    client.Disconnect (true, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail ("Did not expect an exception in Disconnect: {0}", ex);
                }

                Assert.IsFalse (client.IsConnected, "Failed to disconnect");
            }
        }
        /*
         * retrieves messages from pop3 server
         */
        private static void RetrieveMessagesFromServer(
            Node pars,
            Node dp,
            Node ip,
            string basePath,
            string attachmentDirectory,
            string linkedAttachmentDirectory,
            Node exeNode,
            Pop3Client client)
        {
            int serverCount = client.GetMessageCount();
            int count = Expressions.GetExpressionValue<int>(ip.GetValue("count", serverCount.ToString()), dp, ip, false);
            count = Math.Min(serverCount, count);

            bool delete = Expressions.GetExpressionValue<bool>(ip.GetValue("delete", "false"), dp, ip, false);

            for (int idxMsg = 0; idxMsg < count; idxMsg++)
            {
                MimeMessage msg = client.GetMessage(idxMsg);
                HandleMessage(
                    pars,
                    ip,
                    basePath,
                    attachmentDirectory,
                    linkedAttachmentDirectory,
                    exeNode,
                    idxMsg,
                    msg);

                if (delete)
                    client.DeleteMessage(idxMsg);
            }
        }
Exemple #7
0
        /*
         * retrieves messages from pop3 server
         */
        private static void RetrieveMessagesFromServer(
            Node pars, 
            Node dp,
            Node ip, 
            string basePath, 
            string attachmentDirectory, 
            string linkedAttachmentDirectory, 
            Node exeNode, 
            Pop3Client client)
        {
            int serverCount = client.GetMessageCount();
            int count = Expressions.GetExpressionValue<int>(ip.GetValue("count", serverCount.ToString()), dp, ip, false);
            count = Math.Min(serverCount, count);

            bool delete = Expressions.GetExpressionValue<bool>(ip.GetValue("delete", "false"), dp, ip, false);

            for (int idxMsg = 0; idxMsg < count; idxMsg++)
            {
                // short circuting in case message is already downloaded
                string uuid = null;
                if (client.SupportsUids)
                {
                    uuid = client.GetMessageUid(idxMsg);
                    if (MessageAlreadyDownloaded(uuid))
                        continue;
                }

                MimeMessage msg = client.GetMessage(idxMsg);
                HandleMessage(
                    pars,
                    ip,
                    basePath,
                    attachmentDirectory,
                    linkedAttachmentDirectory,
                    exeNode,
                    idxMsg,
                    msg,
                    uuid);

                if (delete)
                    client.DeleteMessage(idxMsg);
            }
        }