コード例 #1
0
        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 sizes = client.GetMessageSizes(CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Count: {0}", ex);
                }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException 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");
            }
        }
コード例 #2
0
 public void TestGetMessageSizes()
 {
     Connect();
     Assert.Throws(typeof(InvalidUseException), delegate { Client.GetMessageSizes(); });
 }
コード例 #3
0
        static private void function_pop3()
        {
            // Используем using чтобы соединение автоматически закрывалось
            using (OpenPop.Pop3.Pop3Client client = new Pop3Client())
            {
                Console.Write("Введите имя сервера: ");
                string name_server = /*"pop.mail.ru";/*/ Console.ReadLine();

                Console.Write("Введите порт (110 или 995): ");
                var  port   = /*995;/*/ Convert.ToInt32(Console.ReadLine());
                bool usessl = false;

                if (port == 995)
                {
                    usessl = true;
                }
                else
                if (port == 110)
                {
                    usessl = false;
                }
                else
                {
                    Environment.Exit(0);
                }

                Console.Write("Введите почту: ");
                string login = /*"*****@*****.**";/*/ Console.ReadLine();

                Console.Write("Введите пароль: ");
                string password = /*"student2017";*/ Console.ReadLine();


                // Подключение к серверу
                client.Connect(name_server, port, usessl);

                // Аутентификация (проверка логина и пароля)
                client.Authenticate(login, password, AuthenticationMethod.UsernameAndPassword);


                if (client.Connected)
                {
                    var flag = true;
                    for (; flag;)
                    {
                        Console.WriteLine("\nВыберите действие:");
                        Console.WriteLine("1 - выход");                             // Работает
                        Console.WriteLine("2 - проверить текущее состояние почты"); // Работает
                        Console.WriteLine("3 - вывод списка сообщений");            // Работает
                        Console.WriteLine("4 - вывод всех сообщений");              // Работает
                        Console.WriteLine("5 - прочитать сообщение номер");         // Работает
                        Console.WriteLine("6 - удалить сообщение номер");           // Работает
                        var choice = Convert.ToInt32(Console.ReadLine());

                        switch (choice)
                        {
                        case 1:
                            flag = false;
                            client.Disconnect();
                            break;

                        case 2:
                            var count = client.GetMessageCount();
                            var mem   = client.GetMessageSizes();
                            int sum   = 0;
                            foreach (var mem1 in mem)
                            {
                                sum += mem1;
                            }
                            Console.WriteLine("Всего {0} сообщений с общим размером {1} байт", count, sum);
                            break;

                        case 3:
                            int            messageCount = client.GetMessageCount();
                            List <Message> allMessages  = new List <Message>(messageCount);

                            int s = 1;
                            for (int i = messageCount; i > 0; i--)
                            {
                                Message message = client.GetMessage(i);
                                allMessages.Add(message);
                                Console.WriteLine("{0}        {1} {2} {3}", i, message.Headers.Subject, message.Headers.From, message.Headers.DateSent);
                                s++;
                            }
                            break;

                        case 4:
                            get_all_message(client);
                            break;

                        case 5:
                            Console.WriteLine("Номер сообщения.");
                            var ch    = Convert.ToInt32(Console.ReadLine());
                            var bytes = client.GetMessage(ch);

                            string subject = bytes.Headers.Subject + '\n';     //заголовок

                            var    help_val = bytes.FindFirstPlainTextVersion();
                            string text_mes = "";
                            if (help_val != null)
                            {
                                text_mes = help_val.GetBodyAsText() + '\n';
                            }
                            string date = bytes.Headers.Date.ToString() + '\n';     //Дата/Время
                            string from = bytes.Headers.From.ToString() + '\n';     //от кого

                            string text = subject + text_mes + date + from;

                            Console.WriteLine(text);
                            break;

                        case 6:
                            Console.WriteLine("Номер сообщения.");
                            var ch1 = Convert.ToInt32(Console.ReadLine());
                            client.DeleteMessage(ch1);
                            break;

                        default:
                            Console.WriteLine("Ошибка выбора!");
                            break;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("ошибка аутентификации! Повторите снова!");
                }
            }
        }
コード例 #4
0
 public void TestGetMessageSizesDoesNotThrow()
 {
     Authenticate("+OK\r\n1 2\r\n.");             // LIST command accepted. // Message 1 has size 2 // . ends answer
     Assert.DoesNotThrow(delegate { Client.GetMessageSizes(); });
 }