MailKit.Net.Smtp.SmtpClient.ReplayConnect C# (CSharp) Method

ReplayConnect() private method

private ReplayConnect ( string hostName, Stream replayStream, CancellationToken cancellationToken = default(CancellationToken) ) : void
hostName string
replayStream Stream
cancellationToken System.Threading.CancellationToken
return void
		internal void ReplayConnect (string hostName, Stream replayStream, CancellationToken cancellationToken = default (CancellationToken))
		{
			CheckDisposed ();

			if (hostName == null)
				throw new ArgumentNullException (nameof (hostName));

			if (replayStream == null)
				throw new ArgumentNullException (nameof (replayStream));

			Stream = new SmtpStream (replayStream, null, ProtocolLogger);
			capabilities = SmtpCapabilities.None;
			AuthenticationMechanisms.Clear ();
			host = hostName;
			secure = false;
			MaxSize = 0;

			try {
				// read the greeting
				var response = Stream.ReadResponse (cancellationToken);

				if (response.StatusCode != SmtpStatusCode.ServiceReady)
					throw new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response);

				// Send EHLO and get a list of supported extensions
				Ehlo (cancellationToken);

				connected = true;
			} catch {
				Stream.Dispose ();
				Stream = null;
				throw;
			}

			OnConnected ();
		}

Usage Example

Example #1
0
        public void TestBasicFunctionality()
        {
            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", "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 (CreateSimpleMessage (), 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");
            }
        }
All Usage Examples Of MailKit.Net.Smtp.SmtpClient::ReplayConnect