Exemplo n.º 1
0
        public void Deliver_Async()
        {
            // SmtpClient uses BackgroundWorker and listens for the RunWorkerCompleted
            // to mark an async task as completed. The problem is that BackgroundWorker uses
            // System.ComponentModel.AsyncOperationManager to get the synchronization
            // context, and in monotouch that returns by default a synchronization
            // context for the main thread. Since tests are also run on the main thread,
            // we'll block the main thread while waiting for the async send to complete,
            // while the async completion is waiting for the main thread to process it.
            // So instead use a SynchronizationContext that uses the threadpool instead
            // of the main thread.
            var existing_context = global::System.ComponentModel.AsyncOperationManager.SynchronizationContext;

            global::System.ComponentModel.AsyncOperationManager.SynchronizationContext = new ThreadPoolSynchronizationContext();
            try {
                var server = new SmtpServer();
                var client = new SmtpClient("localhost", server.EndPoint.Port);
                var msg    = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo\r\n");

                Thread t = new Thread(server.Run);
                t.Start();
                var task = client.SendMailAsync(msg);
                t.Join();

                Assert.AreEqual("<*****@*****.**>", server.mail_from);
                Assert.AreEqual("<*****@*****.**>", server.rcpt_to);

                Assert.IsTrue(task.Wait(1000));
                Assert.IsTrue(task.IsCompleted, "task");
            } finally {
                global::System.ComponentModel.AsyncOperationManager.SynchronizationContext = existing_context;
            }
        }
Exemplo n.º 2
0
        static void Main()
        {
            var server = new SmtpServer();
            var client = new SmtpClient("localhost", server.EndPoint.Port);
            var msg    = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo");

            DoTest(server, client, msg);

            msg.Sender = new MailAddress("*****@*****.**");

            DoTest(server, client, msg);
        }
Exemplo n.º 3
0
		public void Deliver ()
		{
			var server = new SmtpServer ();
			var client = new SmtpClient ("localhost", server.EndPoint.Port);
			var msg = new MailMessage ("*****@*****.**", "*****@*****.**", "hello", "howdydoo\r\n");

			Thread t = new Thread (server.Run);
			t.Start ();
			client.Send (msg);
			t.Join ();

			Assert.AreEqual ("<*****@*****.**>", server.mail_from);
			Assert.AreEqual ("<*****@*****.**>", server.rcpt_to);
		}
Exemplo n.º 4
0
        static void DoTest(SmtpServer s, SmtpClient c, MailMessage m)
        {
            Thread t = new Thread(s.Run);

            t.Start();
            c.Send(m);
            t.Join();

            Console.WriteLine("Message From: {0}", m.From);
            Console.WriteLine("Message Sender: {0}", m.Sender);
            Console.WriteLine("Mail From: {0}", s.mail_from);
            Console.WriteLine("Rcpt To: {0}", s.rcpt_to);
            Console.WriteLine("-------------------------------------");
            Console.Write(s.data);
            Console.WriteLine("-------------------------------------");
        }
Exemplo n.º 5
0
        public void Deliver_Async()
        {
            var server = new SmtpServer();
            var client = new SmtpClient("localhost", server.EndPoint.Port);
            var msg    = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo\r\n");

            Thread t = new Thread(server.Run);

            t.Start();
            var task = client.SendMailAsync(msg);

            t.Join();

            Assert.AreEqual("<*****@*****.**>", server.mail_from);
            Assert.AreEqual("<*****@*****.**>", server.rcpt_to);

            Assert.IsTrue(task.Wait(1000));
            Assert.IsTrue(task.IsCompleted, "task");
        }
Exemplo n.º 6
0
		public void Deliver_Async ()
		{
			// SmtpClient uses BackgroundWorker and listens for the RunWorkerCompleted
			// to mark an async task as completed. The problem is that BackgroundWorker uses
			// System.ComponentModel.AsyncOperationManager to get the synchronization
			// context, and in monotouch that returns by default a synchronization
			// context for the main thread. Since tests are also run on the main thread,
			// we'll block the main thread while waiting for the async send to complete,
			// while the async completion is waiting for the main thread to process it.
			// So instead use a SynchronizationContext that uses the threadpool instead
			// of the main thread.
			var existing_context = global::System.ComponentModel.AsyncOperationManager.SynchronizationContext;
			global::System.ComponentModel.AsyncOperationManager.SynchronizationContext = new ThreadPoolSynchronizationContext ();
			try {
				var server = new SmtpServer ();
				var client = new SmtpClient ("localhost", server.EndPoint.Port);
				var msg = new MailMessage ("*****@*****.**", "*****@*****.**", "hello", "howdydoo\r\n");

				Thread t = new Thread (server.Run);
				t.Start ();
				var task = client.SendMailAsync (msg);
				t.Join ();

				Assert.AreEqual ("<*****@*****.**>", server.mail_from);
				Assert.AreEqual ("<*****@*****.**>", server.rcpt_to);

				Assert.IsTrue (task.Wait (1000));
				Assert.IsTrue (task.IsCompleted, "task");
			} finally {
				global::System.ComponentModel.AsyncOperationManager.SynchronizationContext = existing_context;
			}
		}
Exemplo n.º 7
0
		public void Deliver_Envelope ()
		{
			var server = new SmtpServer ();
			var client = new SmtpClient ("localhost", server.EndPoint.Port);
			var msg = new MailMessage ("*****@*****.**", "*****@*****.**", "hello", "howdydoo\r\n");

			msg.Sender = new MailAddress ("*****@*****.**");

			Thread t = new Thread (server.Run);
			t.Start ();
			client.Send (msg);
			t.Join ();

			Assert.AreEqual ("<*****@*****.**>", server.mail_from);
			Assert.AreEqual ("<*****@*****.**>", server.rcpt_to);
		}
Exemplo n.º 8
0
		public void Deliver_Async ()
		{
			var server = new SmtpServer ();
			var client = new SmtpClient ("localhost", server.EndPoint.Port);
			var msg = new MailMessage ("*****@*****.**", "*****@*****.**", "hello", "howdydoo\r\n");

			Thread t = new Thread (server.Run);
			t.Start ();
			var task = client.SendMailAsync (msg);
			t.Join ();

			Assert.AreEqual ("<*****@*****.**>", server.mail_from);
			Assert.AreEqual ("<*****@*****.**>", server.rcpt_to);

			task.Wait (10);

			Assert.IsTrue (task.IsCompleted, "task");
		}
Exemplo n.º 9
0
		static void Main ()
		{
			var server = new SmtpServer ();
			var client = new SmtpClient ("localhost", server.EndPoint.Port);
			var msg = new MailMessage ("*****@*****.**", "*****@*****.**", "hello", "howdydoo");

			DoTest (server, client, msg);

			msg.Sender = new MailAddress ("*****@*****.**");

			DoTest (server, client, msg);
		}
Exemplo n.º 10
0
		static void DoTest (SmtpServer s, SmtpClient c, MailMessage m)
		{
			Thread t = new Thread (s.Run);
			t.Start ();
			c.Send (m);
			t.Join ();

			Console.WriteLine ("Message From: {0}", m.From);
			Console.WriteLine ("Message Sender: {0}", m.Sender);
			Console.WriteLine ("Mail From: {0}", s.mail_from);
			Console.WriteLine ("Rcpt To: {0}", s.rcpt_to);
			Console.WriteLine ("-------------------------------------");
			Console.Write (s.data);
			Console.WriteLine ("-------------------------------------");
		}