public void TestMailDeliveryAsync() { SmtpServer server = new SmtpServer(); SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port); MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo"); Thread t = new Thread(server.Run); t.Start(); Task task = client.SendMailAsync(msg); t.Join(); server.Stop(); Assert.Equal("<*****@*****.**>", server.MailFrom); Assert.Equal("<*****@*****.**>", server.MailTo); Assert.Equal("hello", server.Subject); Assert.Equal("howdydoo", server.Body); Assert.True(task.Wait(1000)); }
public async Task TestCredentialsCopyInAsyncContext() { SmtpServer server = new SmtpServer(); SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port); MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo"); string clientDomain = IPGlobalProperties.GetIPGlobalProperties().HostName.Trim().ToLower(); CredentialCache cache = new CredentialCache(); cache.Add("localhost", server.EndPoint.Port, "NTLM", CredentialCache.DefaultNetworkCredentials); client.Credentials = cache; try { Thread t = new Thread(server.Run); t.Start(); await client.SendMailAsync(msg); t.Join(); Assert.Equal("<*****@*****.**>", server.MailFrom); Assert.Equal("<*****@*****.**>", server.MailTo); Assert.Equal("hello", server.Subject); Assert.Equal("howdydoo", server.Body); Assert.Equal(clientDomain, server.ClientDomain); } finally { server.Stop(); } }
public async Task TestMailDeliveryAsync() { SmtpServer server = new SmtpServer(); SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port); MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo"); string clientDomain = IPGlobalProperties.GetIPGlobalProperties().HostName.Trim().ToLower(); try { Thread t = new Thread(server.Run); t.Start(); await client.SendMailAsync(msg); t.Join(); Assert.Equal("<*****@*****.**>", server.MailFrom); Assert.Equal("<*****@*****.**>", server.MailTo); Assert.Equal("hello", server.Subject); Assert.Equal("howdydoo", server.Body); Assert.Equal(clientDomain, server.ClientDomain); } finally { server.Stop(); } }
public void TestMailDelivery() { SmtpServer server = new SmtpServer(); SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port); client.Credentials = new NetworkCredential("user", "password"); MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo"); try { Thread t = new Thread(server.Run); t.Start(); client.Send(msg); t.Join(); Assert.Equal("<*****@*****.**>", server.MailFrom); Assert.Equal("<*****@*****.**>", server.MailTo); Assert.Equal("hello", server.Subject); Assert.Equal("howdydoo", server.Body); } finally { server.Stop(); } }
[InlineData(true, true, true)] // Received subjectBase64. If subjectText is received, the test fails, and the results are inconsistent with those of synchronous methods. public void SendMail_DeliveryFormat_SubjectEncoded(bool useAsyncSend, bool useSevenBit, bool useSmtpUTF8) { // If the server support `SMTPUTF8` and use `SmtpDeliveryFormat.International`, the server should received this subject. const string subjectText = "Test \u6d4b\u8bd5 Contain \u5305\u542b UTF8"; // If the server does not support `SMTPUTF8` or use `SmtpDeliveryFormat.SevenBit`, the server should received this subject. const string subjectBase64 = "=?utf-8?B?VGVzdCDmtYvor5UgQ29udGFpbiDljIXlkKsgVVRGOA==?="; SmtpServer server = new SmtpServer(); // Setting up Server Support for `SMTPUTF8`. server.SupportSmtpUTF8 = useSmtpUTF8; SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port); if (useSevenBit) { // Subject will be encoded by Base64. client.DeliveryFormat = SmtpDeliveryFormat.SevenBit; } else { // If the server supports `SMTPUTF8`, subject will not be encoded. Otherwise, subject will be encoded by Base64. client.DeliveryFormat = SmtpDeliveryFormat.International; } MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**", subjectText, "hello \u9ad8\u575a\u679c"); msg.HeadersEncoding = msg.BodyEncoding = msg.SubjectEncoding = System.Text.Encoding.UTF8; try { Thread t = new Thread(server.Run); t.Start(); if (useAsyncSend) { client.SendMailAsync(msg).Wait(); } else { client.Send(msg); } if (useSevenBit || !useSmtpUTF8) { Assert.Equal(subjectBase64, server.Subject); } else { Assert.Equal(subjectText, server.Subject); } } finally { server.Stop(); } }
public async Task TestMailDeliveryAsync() { SmtpServer server = new SmtpServer(); SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port); MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo"); try { Thread t = new Thread(server.Run); t.Start(); await client.SendMailAsync(msg); t.Join(); Assert.Equal("<*****@*****.**>", server.MailFrom); Assert.Equal("<*****@*****.**>", server.MailTo); Assert.Equal("hello", server.Subject); Assert.Equal("howdydoo", server.Body); } finally { server.Stop(); } }
public void TestMailDelivery() { SmtpServer server = new SmtpServer(); SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port); client.Credentials = new NetworkCredential("user", "password"); MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo"); Thread t = new Thread(server.Run); t.Start(); client.Send(msg); t.Join(); server.Stop(); Assert.Equal("<*****@*****.**>", server.MailFrom); Assert.Equal("<*****@*****.**>", server.MailTo); Assert.Equal("hello", server.Subject); Assert.Equal("howdydoo", server.Body); }
public async void TestCredentialsCopyInAsyncContext() { SmtpServer server = new SmtpServer(); SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port); MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**", "hello", "howdydoo"); CredentialCache cache = new CredentialCache(); cache.Add("localhost", server.EndPoint.Port, "NTLM", CredentialCache.DefaultNetworkCredentials); client.Credentials = cache; try { Thread t = new Thread(server.Run); t.Start(); await client.SendMailAsync(msg); t.Join(); Assert.Equal("<*****@*****.**>", server.MailFrom); Assert.Equal("<*****@*****.**>", server.MailTo); Assert.Equal("hello", server.Subject); Assert.Equal("howdydoo", server.Body); } finally { server.Stop(); } }