示例#1
0
        public void TestEightBitMime()
        {
            var commands = new List <SmtpReplayCommand> ();

            commands.Add(new SmtpReplayCommand("", "comcast-greeting.txt"));
            commands.Add(new SmtpReplayCommand("EHLO [127.0.0.1]\r\n", "comcast-ehlo.txt"));
            commands.Add(new SmtpReplayCommand("AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "comcast-auth-plain.txt"));
            commands.Add(new SmtpReplayCommand("EHLO [127.0.0.1]\r\n", "comcast-ehlo.txt"));
            commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**> BODY=8BITMIME\r\n", "comcast-mail-from.txt"));
            commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt"));
            commands.Add(new SmtpReplayCommand("DATA\r\n", "comcast-data.txt"));
            commands.Add(new SmtpReplayCommand(".\r\n", "comcast-data-done.txt"));
            commands.Add(new SmtpReplayCommand("QUIT\r\n", "comcast-quit.txt"));

            using (var client = new SmtpClient()) {
                try {
                    client.ReplayConnect("localhost", new SmtpReplayStream(commands), 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.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Authentication), "Failed to detect AUTH extension");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("LOGIN"), "Failed to detect the LOGIN auth mechanism");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("PLAIN"), "Failed to detect the PLAIN auth mechanism");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EightBitMime), "Failed to detect 8BITMIME extension");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EnhancedStatusCodes), "Failed to detect ENHANCEDSTATUSCODES extension");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Size), "Failed to detect SIZE extension");
                Assert.AreEqual(36700160, client.MaxSize, "Failed to parse SIZE correctly");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.StartTLS), "Failed to detect STARTTLS extension");

                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);
                }

                try {
                    client.Send(CreateEightBitMessage(), CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Send: {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");
            }
        }
示例#2
0
        public async void TestUnauthorizedAccessException()
        {
            var commands = new List <SmtpReplayCommand> ();

            commands.Add(new SmtpReplayCommand("", "comcast-greeting.txt"));
            commands.Add(new SmtpReplayCommand("EHLO [127.0.0.1]\r\n", "comcast-ehlo.txt"));
            commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "auth-required.txt"));
            commands.Add(new SmtpReplayCommand("QUIT\r\n", "comcast-quit.txt"));

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

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

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Authentication), "Failed to detect AUTH extension");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("LOGIN"), "Failed to detect the LOGIN auth mechanism");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("PLAIN"), "Failed to detect the PLAIN auth mechanism");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EightBitMime), "Failed to detect 8BITMIME extension");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EnhancedStatusCodes), "Failed to detect ENHANCEDSTATUSCODES extension");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Size), "Failed to detect SIZE extension");
                Assert.AreEqual(36700160, client.MaxSize, "Failed to parse SIZE correctly");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.StartTLS), "Failed to detect STARTTLS extension");

                try {
                    await client.SendAsync(CreateSimpleMessage());

                    Assert.Fail("Expected an ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // this is the expected exception
                } catch (Exception ex) {
                    Assert.Fail("Did not expect this exception in Send: {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "Expected the client to still be connected");

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

                Assert.IsFalse(client.IsConnected, "Failed to disconnect");
            }
        }
示例#3
0
        public void TestBinaryMime()
        {
            var    message = CreateBinaryMessage();
            string bdat;

            using (var memory = new MemoryStream()) {
                var  options = FormatOptions.Default.Clone();
                long size;

                options.NewLineFormat = NewLineFormat.Dos;

                using (var measure = new MeasuringStream()) {
                    message.WriteTo(options, measure);
                    size = measure.Length;
                }

                var bytes = Encoding.ASCII.GetBytes(string.Format("BDAT {0} LAST\r\n", size));
                memory.Write(bytes, 0, bytes.Length);
                message.WriteTo(options, memory);

                bytes = memory.GetBuffer();

                bdat = Encoding.UTF8.GetString(bytes, 0, (int)memory.Length);
            }

            var commands = new List <SmtpReplayCommand> ();

            commands.Add(new SmtpReplayCommand("", "comcast-greeting.txt"));
            commands.Add(new SmtpReplayCommand("EHLO [127.0.0.1]\r\n", "comcast-ehlo.txt"));
            commands.Add(new SmtpReplayCommand("AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "comcast-auth-plain.txt"));
            commands.Add(new SmtpReplayCommand("EHLO [127.0.0.1]\r\n", "comcast-ehlo+binarymime.txt"));
            commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**> BODY=BINARYMIME\r\n", "comcast-mail-from.txt"));
            commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt"));
            commands.Add(new SmtpReplayCommand(bdat, "comcast-data-done.txt"));
            commands.Add(new SmtpReplayCommand("QUIT\r\n", "comcast-quit.txt"));

            using (var client = new SmtpClient()) {
                try {
                    client.ReplayConnect("localhost", new SmtpReplayStream(commands), 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.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Authentication), "Failed to detect AUTH extension");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("LOGIN"), "Failed to detect the LOGIN auth mechanism");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("PLAIN"), "Failed to detect the PLAIN auth mechanism");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EightBitMime), "Failed to detect 8BITMIME extension");
                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EnhancedStatusCodes), "Failed to detect ENHANCEDSTATUSCODES extension");
                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Size), "Failed to detect SIZE extension");
                Assert.AreEqual(36700160, client.MaxSize, "Failed to parse SIZE correctly");
                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.StartTLS), "Failed to detect STARTTLS extension");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.StartTLS), "Failed to detect STARTTLS extension");

                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.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.BinaryMime), "Failed to detect BINARYMIME extension");
                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Chunking), "Failed to detect CHUNKING extension");

                try {
                    client.Send(message, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Send: {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");
            }
        }
示例#4
0
        public async void TestRcptToMailboxUnavailable()
        {
            var commands = new List <SmtpReplayCommand> ();

            commands.Add(new SmtpReplayCommand("", "comcast-greeting.txt"));
            commands.Add(new SmtpReplayCommand("EHLO [127.0.0.1]\r\n", "comcast-ehlo.txt"));
            commands.Add(new SmtpReplayCommand("AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "comcast-auth-plain.txt"));
            commands.Add(new SmtpReplayCommand("EHLO [127.0.0.1]\r\n", "comcast-ehlo.txt"));
            commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "comcast-mail-from.txt"));
            commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "mailbox-unavailable.txt"));
            commands.Add(new SmtpReplayCommand("RSET\r\n", "comcast-rset.txt"));
            commands.Add(new SmtpReplayCommand("QUIT\r\n", "comcast-quit.txt"));

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

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

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Authentication), "Failed to detect AUTH extension");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("LOGIN"), "Failed to detect the LOGIN auth mechanism");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("PLAIN"), "Failed to detect the PLAIN auth mechanism");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EightBitMime), "Failed to detect 8BITMIME extension");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EnhancedStatusCodes), "Failed to detect ENHANCEDSTATUSCODES extension");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Size), "Failed to detect SIZE extension");
                Assert.AreEqual(36700160, client.MaxSize, "Failed to parse SIZE correctly");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.StartTLS), "Failed to detect STARTTLS extension");

                try {
                    await client.AuthenticateAsync("username", "password");
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Authenticate: {0}", ex);
                }

                try {
                    await client.SendAsync(CreateSimpleMessage());

                    Assert.Fail("Expected an SmtpException");
                } catch (SmtpCommandException sex) {
                    Assert.AreEqual(sex.ErrorCode, SmtpErrorCode.RecipientNotAccepted, "Unexpected SmtpErrorCode");
                } catch (Exception ex) {
                    Assert.Fail("Did not expect this exception in Send: {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "Expected the client to still be connected");

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

                Assert.IsFalse(client.IsConnected, "Failed to disconnect");
            }
        }
示例#5
0
        public async void TestBasicFunctionality()
        {
            var commands = new List <SmtpReplayCommand> ();

            commands.Add(new SmtpReplayCommand("", "comcast-greeting.txt"));
            commands.Add(new SmtpReplayCommand("EHLO unit-tests.mimekit.org\r\n", "comcast-ehlo.txt"));
            commands.Add(new SmtpReplayCommand("AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "comcast-auth-plain.txt"));
            commands.Add(new SmtpReplayCommand("EHLO unit-tests.mimekit.org\r\n", "comcast-ehlo.txt"));
            commands.Add(new SmtpReplayCommand("VRFY Smith\r\n", "rfc0821-vrfy.txt"));
            commands.Add(new SmtpReplayCommand("EXPN Example-People\r\n", "rfc0821-expn.txt"));
            commands.Add(new SmtpReplayCommand("NOOP\r\n", "comcast-noop.txt"));
            commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "comcast-mail-from.txt"));
            commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt"));
            commands.Add(new SmtpReplayCommand("DATA\r\n", "comcast-data.txt"));
            commands.Add(new SmtpReplayCommand(".\r\n", "comcast-data-done.txt"));
            commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "comcast-mail-from.txt"));
            commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt"));
            commands.Add(new SmtpReplayCommand("DATA\r\n", "comcast-data.txt"));
            commands.Add(new SmtpReplayCommand(".\r\n", "comcast-data-done.txt"));
            commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "comcast-mail-from.txt"));
            commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt"));
            commands.Add(new SmtpReplayCommand("DATA\r\n", "comcast-data.txt"));
            commands.Add(new SmtpReplayCommand(".\r\n", "comcast-data-done.txt"));
            commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "comcast-mail-from.txt"));
            commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt"));
            commands.Add(new SmtpReplayCommand("DATA\r\n", "comcast-data.txt"));
            commands.Add(new SmtpReplayCommand(".\r\n", "comcast-data-done.txt"));
            commands.Add(new SmtpReplayCommand("QUIT\r\n", "comcast-quit.txt"));

            using (var client = new SmtpClient()) {
                client.LocalDomain = "unit-tests.mimekit.org";

                try {
                    client.ReplayConnect("localhost", new SmtpReplayStream(commands));
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Connect: {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "Client failed to connect.");
                Assert.IsFalse(client.IsSecure, "IsSecure should be false.");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Authentication), "Failed to detect AUTH extension");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("LOGIN"), "Failed to detect the LOGIN auth mechanism");
                Assert.IsTrue(client.AuthenticationMechanisms.Contains("PLAIN"), "Failed to detect the PLAIN auth mechanism");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EightBitMime), "Failed to detect 8BITMIME extension");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EnhancedStatusCodes), "Failed to detect ENHANCEDSTATUSCODES extension");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Size), "Failed to detect SIZE extension");
                Assert.AreEqual(36700160, client.MaxSize, "Failed to parse SIZE correctly");

                Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.StartTLS), "Failed to detect STARTTLS extension");

                Assert.Throws <ArgumentException> (() => client.Capabilities |= SmtpCapabilities.UTF8);

                Assert.AreEqual(100000, client.Timeout, "Timeout");
                client.Timeout *= 2;

                try {
                    await client.AuthenticateAsync("username", "password");
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Authenticate: {0}", ex);
                }

                try {
                    await client.VerifyAsync("Smith");
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Verify: {0}", ex);
                }

                try {
                    await client.ExpandAsync("Example-People");
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Expand: {0}", ex);
                }

                try {
                    await client.NoOpAsync();
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in NoOp: {0}", ex);
                }

                var message = CreateSimpleMessage();
                var options = FormatOptions.Default;

                try {
                    await client.SendAsync(message);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Send: {0}", ex);
                }

                try {
                    await client.SendAsync(message, message.From.Mailboxes.FirstOrDefault(), message.To.Mailboxes);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Send: {0}", ex);
                }

                try {
                    await client.SendAsync(options, message);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Send: {0}", ex);
                }

                try {
                    await client.SendAsync(options, message, message.From.Mailboxes.FirstOrDefault(), message.To.Mailboxes);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Send: {0}", ex);
                }

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

                Assert.IsFalse(client.IsConnected, "Failed to disconnect");
            }
        }