Esempio n. 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 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");
            }
        }
Esempio n. 2
0
		public static void DownloadNewMessages (HashSet<string> previouslyDownloadedUids)
		{
			using (var client = new Pop3Client ()) {
				IList<string> uids = null;

				try {
					client.Connect ("pop.gmail.com", 995, SecureSocketOptions.SslOnConnect);
				} catch (Pop3CommandException ex) {
					Console.WriteLine ("Error trying to connect: {0}", ex.Message);
					Console.WriteLine ("\tStatusText: {0}", ex.StatusText);
					return;
				} catch (Pop3ProtocolException ex) {
					Console.WriteLine ("Protocol error while trying to connect: {0}", ex.Message);
					return;
				}

				try {
					client.Authenticate ("username", "password");
				} catch (AuthenticationException ex) {
					Console.WriteLine ("Invalid user name or password.");
					return;
				} catch (Pop3CommandException ex) {
					Console.WriteLine ("Error trying to authenticate: {0}", ex.Message);
					Console.WriteLine ("\tStatusText: {0}", ex.StatusText);
					return;
				} catch (Pop3ProtocolException ex) {
					Console.WriteLine ("Protocol error while trying to authenticate: {0}", ex.Message);
					return;
				}

				// for the sake of this example, let's assume GMail supports the UIDL extension
				if (client.Capabilities.HasFlag (Pop3Capabilities.UIDL)) {
					try {
						uids = client.GetMessageUids ();
					} catch (Pop3CommandException ex) {
						Console.WriteLine ("Error trying to get the list of uids: {0}", ex.Message);
						Console.WriteLine ("\tStatusText: {0}", ex.StatusText);

						// we'll continue on leaving uids set to null...
					} catch (Pop3ProtocolException ex) {
						Console.WriteLine ("Protocol error while trying to authenticate: {0}", ex.Message);

						// Pop3ProtocolExceptions often cause the connection to drop
						if (!client.IsConnected)
							return;
					}
				}

				for (int i = 0; i < client.Count; i++) {
					if (uids != null && previouslyDownloadedUids.Contains (uids[i])) {
						// we must have downloaded this message in a previous session
						continue;
					}

					try {
						// download the message at the specified index
						var message = client.GetMessage (i);

						// write the message to a file
						if (uids != null) {
							message.WriteTo (string.Format ("{0}.msg", uids[i]));

							// keep track of our downloaded message uids so we can skip downloading them next time
							previouslyDownloadedUids.Add (uids[i]);
						} else {
							message.WriteTo (string.Format ("{0}.msg", i));
						}
					} catch (Pop3CommandException ex) {
						Console.WriteLine ("Error downloading message {0}: {1}", i, ex.Message);
						Console.WriteLine ("\tStatusText: {0}", ex.StatusText);
						continue;
					} catch (Pop3ProtocolException ex) {
						Console.WriteLine ("Protocol error while sending message {0}: {1}", i, ex.Message);
						// most likely the connection has been dropped
						if (!client.IsConnected)
							break;
					}
				}

				if (client.IsConnected) {
					// if we do not disconnect cleanly, then the messages won't actually get deleted
					client.Disconnect (true);
				}
			}
		}
Esempio n. 3
0
        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");
            }
        }
Esempio n. 4
0
		public static void DownloadNewMessages (HashSet<string> previouslyDownloadedUids)
		{
			using (var client = new Pop3Client ()) {
				client.Connect ("pop.gmail.com", 995, SecureSocketOptions.SslOnConnect);

				client.Authenticate ("username", "password");

				if (!client.Capabilities.HasFlag (Pop3Capabilities.UIDL))
					throw new Exception ("The POP3 server does not support UIDs!");

				var uids = client.GetMessageUids ();

				for (int i = 0; i < client.Count; i++) {
					// check that we haven't already downloaded this message
					// in a previous session
					if (previouslyDownloadedUids.Contains (uids[i]))
						continue;

					var message = client.GetMessage (i);

					// write the message to a file
					message.WriteTo (string.Format ("{0}.msg", uids[i]));

					// add the message uid to our list of downloaded uids
					previouslyDownloadedUids.Add (uids[i]);
				}

				client.Disconnect (true);
			}
		}