コード例 #1
0
        public void With_AuthenticateException(Type authenticateException)
        {
            var sender = GetMailMergeSender();
            var msg    = GetMimeMessage();
            var config = new SmtpClientConfig {
                MessageOutput = MessageOutput.SmtpServer, NetworkCredential = new Credential()
            };

            sender.Config.SmtpClientConfig[0] = config;

            Exception exception;

            if (authenticateException == typeof(AuthenticationException))
            {
                exception = new AuthenticationException();
            }
            else if (authenticateException == typeof(SmtpCommandException))
            {
                exception = new SmtpCommandException(SmtpErrorCode.UnexpectedStatusCode, SmtpStatusCode.CommandNotImplemented, "unitTest");
            }
            else if (authenticateException == typeof(SmtpProtocolException))
            {
                exception = new SmtpProtocolException();
            }
            else if (authenticateException == typeof(IOException))
            {
                exception = new IOException();
            }
            else if (authenticateException == typeof(Exception))
            {
                exception = new Exception();
            }
            else
            {
                throw new ArgumentOutOfRangeException();
            }

            var smtpClient = new FakeSmtpClient {
                AuthenticateException = exception
            };

            Assert.Throws(authenticateException, () => sender.SendMimeMessage(smtpClient, msg, config));
            Assert.ThrowsAsync(authenticateException, async() => await sender.SendMimeMessageAsync(smtpClient, msg, config));
        }
コード例 #2
0
        static void TestSmtpCommandException(SmtpCommandException expected)
        {
            using (var stream = new MemoryStream()) {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, expected);
                stream.Position = 0;

                var ex = (SmtpCommandException)formatter.Deserialize(stream);
                Assert.AreEqual(expected.ErrorCode, ex.ErrorCode, "Unexpected ErrorCode.");
                Assert.AreEqual(expected.StatusCode, ex.StatusCode, "Unexpected StatusCode.");

                if (expected.Mailbox != null)
                {
                    Assert.IsTrue(expected.Mailbox.Equals(ex.Mailbox), "Unexpected Mailbox.");
                }
                else
                {
                    Assert.IsNull(ex.Mailbox, "Expected Mailbox to be null.");
                }
            }
        }