2 // SmtpClientTest.cs - NUnit Test Cases for System.Net.Mail.SmtpClient
5 // John Luke (john.luke@gmail.com)
12 using System.Net.Mail;
13 using System.Net.Mime;
14 using System.Threading;
16 namespace MonoTests.System.Net.Mail
19 public class SmtpClientTest
25 public void GetReady ()
27 smtp = new SmtpClient ();
28 tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName);
29 if (Directory.Exists (tempFolder))
30 Directory.Delete (tempFolder, true);
31 Directory.CreateDirectory (tempFolder);
35 public void TearDown ()
37 if (Directory.Exists (tempFolder))
38 Directory.Delete (tempFolder, true);
42 public void Credentials_Default ()
44 Assert.IsNull (smtp.Credentials);
48 public void DeliveryMethod ()
50 Assert.AreEqual (SmtpDeliveryMethod.Network, smtp.DeliveryMethod, "#1");
51 smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
52 Assert.AreEqual (SmtpDeliveryMethod.SpecifiedPickupDirectory,
53 smtp.DeliveryMethod, "#2");
54 smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
55 Assert.AreEqual (SmtpDeliveryMethod.PickupDirectoryFromIis,
56 smtp.DeliveryMethod, "#3");
57 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
58 Assert.AreEqual (SmtpDeliveryMethod.Network,
59 smtp.DeliveryMethod, "#4");
63 public void EnableSsl ()
65 Assert.IsFalse (smtp.EnableSsl, "#1");
66 smtp.EnableSsl = true;
67 Assert.IsTrue (smtp.EnableSsl, "#2");
68 smtp.EnableSsl = false;
69 Assert.IsFalse (smtp.EnableSsl, "#3");
75 smtp.Host = "127.0.0.1";
76 Assert.AreEqual ("127.0.0.1", smtp.Host, "#2");
77 smtp.Host = "smtp.ximian.com";
78 Assert.AreEqual ("smtp.ximian.com", smtp.Host, "#3");
82 [Category ("NotWorking")]
83 public void Host_Default ()
85 Assert.IsNull (smtp.Host);
89 public void Host_Value_Null ()
94 } catch (ArgumentNullException ex) {
95 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
96 Assert.IsNull (ex.InnerException, "#3");
97 Assert.IsNotNull (ex.Message, "#4");
98 Assert.AreEqual ("value", ex.ParamName, "#5");
103 public void Host_Value_Empty ()
106 smtp.Host = String.Empty;
108 } catch (ArgumentException ex) {
109 // This property cannot be set to an empty string
110 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
111 Assert.IsNull (ex.InnerException, "#3");
112 Assert.IsNotNull (ex.Message, "#4");
113 Assert.AreEqual ("value", ex.ParamName, "#5");
118 public void PickupDirectoryLocation ()
120 Assert.IsNull (smtp.PickupDirectoryLocation, "#1");
121 smtp.PickupDirectoryLocation = tempFolder;
122 Assert.AreSame (tempFolder, smtp.PickupDirectoryLocation, "#2");
123 smtp.PickupDirectoryLocation = "shouldnotexist";
124 Assert.AreEqual ("shouldnotexist", smtp.PickupDirectoryLocation, "#3");
125 smtp.PickupDirectoryLocation = null;
126 Assert.IsNull (smtp.PickupDirectoryLocation, "#4");
127 smtp.PickupDirectoryLocation = string.Empty;
128 Assert.AreEqual (string.Empty, smtp.PickupDirectoryLocation, "#5");
129 smtp.PickupDirectoryLocation = "\0";
130 Assert.AreEqual ("\0", smtp.PickupDirectoryLocation, "#6");
136 Assert.AreEqual (25, smtp.Port, "#1");
138 Assert.AreEqual (1, smtp.Port, "#2");
139 smtp.Port = int.MaxValue;
140 Assert.AreEqual (int.MaxValue, smtp.Port, "#3");
144 public void Port_Value_Invalid ()
150 } catch (ArgumentOutOfRangeException ex) {
151 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
152 Assert.IsNull (ex.InnerException, "#A3");
153 Assert.IsNotNull (ex.Message, "#A4");
154 Assert.AreEqual ("value", ex.ParamName, "#A5");
161 } catch (ArgumentOutOfRangeException ex) {
162 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
163 Assert.IsNull (ex.InnerException, "#B3");
164 Assert.IsNotNull (ex.Message, "#B4");
165 Assert.AreEqual ("value", ex.ParamName, "#B5");
170 public void Send_Message_Null ()
173 smtp.Send ((MailMessage) null);
175 } catch (ArgumentNullException ex) {
176 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
177 Assert.IsNull (ex.InnerException, "#B3");
178 Assert.IsNotNull (ex.Message, "#B4");
179 Assert.AreEqual ("message", ex.ParamName, "#B5");
184 public void Send_Network_Host_Null ()
187 smtp.Send ("mono@novell.com", "everyone@novell.com",
188 "introduction", "hello");
190 } catch (InvalidOperationException ex) {
191 // The SMTP host was not specified
192 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
193 Assert.IsNull (ex.InnerException, "#3");
194 Assert.IsNotNull (ex.Message, "#4");
199 public void Send_Network_Host_Whitespace ()
201 smtp.Host = " \r\n ";
203 smtp.Send ("mono@novell.com", "everyone@novell.com",
204 "introduction", "hello");
206 } catch (InvalidOperationException ex) {
207 // The SMTP host was not specified
208 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
209 Assert.IsNull (ex.InnerException, "#3");
210 Assert.IsNotNull (ex.Message, "#4");
215 public void Send_SpecifiedPickupDirectory ()
217 smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
218 smtp.PickupDirectoryLocation = tempFolder;
219 smtp.Send ("mono@novell.com", "everyone@novell.com",
220 "introduction", "hello");
222 string [] files = Directory.GetFiles (tempFolder, "*");
223 Assert.AreEqual (1, files.Length, "#1");
224 Assert.AreEqual (".eml", Path.GetExtension (files [0]), "#2");
228 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_DirectoryNotFound ()
230 Directory.Delete (tempFolder);
232 smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
233 smtp.PickupDirectoryLocation = tempFolder;
235 smtp.Send ("mono@novell.com", "everyone@novell.com",
236 "introduction", "hello");
238 } catch (SmtpException ex) {
239 // Failure sending email
240 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
241 Assert.IsNotNull (ex.InnerException, "#3");
242 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.InnerException.GetType (), "#4");
243 Assert.IsNotNull (ex.Message, "#5");
244 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#6");
246 // Could not find a part of the path '...'
247 DirectoryNotFoundException inner = (DirectoryNotFoundException) ex.InnerException;
248 Assert.IsNull (inner.InnerException, "#7");
249 Assert.IsNotNull (inner.Message, "#8");
250 Assert.IsTrue (inner.Message.IndexOf (tempFolder) != -1, "#9");
255 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_Empty ()
257 smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
258 smtp.PickupDirectoryLocation = string.Empty;
260 smtp.Send ("mono@novell.com", "everyone@novell.com",
261 "introduction", "hello");
263 } catch (SmtpException ex) {
264 // Only absolute directories are allowed for
266 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
267 Assert.IsNull (ex.InnerException, "#3");
268 Assert.IsNotNull (ex.Message, "#4");
269 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
274 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_IllegalChars ()
276 smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
277 smtp.PickupDirectoryLocation = "\0abc";
279 smtp.Send ("mono@novell.com", "everyone@novell.com",
280 "introduction", "hello");
282 } catch (SmtpException ex) {
283 // Failure sending email
284 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
285 Assert.IsNotNull (ex.InnerException, "#3");
286 Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#4");
287 Assert.IsNotNull (ex.Message, "#5");
288 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#6");
290 // Illegal characters in path
291 ArgumentException inner = (ArgumentException) ex.InnerException;
292 Assert.IsNull (inner.InnerException, "#7");
293 Assert.IsNotNull (inner.Message, "#8");
294 Assert.IsNull (inner.ParamName, "#9");
299 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_NotAbsolute ()
301 smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
302 smtp.PickupDirectoryLocation = "relative";
304 smtp.Send ("mono@novell.com", "everyone@novell.com",
305 "introduction", "hello");
307 } catch (SmtpException ex) {
308 // Only absolute directories are allowed for
310 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
311 Assert.IsNull (ex.InnerException, "#3");
312 Assert.IsNotNull (ex.Message, "#4");
313 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
318 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_Null ()
320 smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
322 smtp.Send ("mono@novell.com", "everyone@novell.com",
323 "introduction", "hello");
325 } catch (SmtpException ex) {
326 // Only absolute directories are allowed for
328 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
329 Assert.IsNull (ex.InnerException, "#3");
330 Assert.IsNotNull (ex.Message, "#4");
331 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
336 public void Timeout ()
338 Assert.AreEqual (100000, smtp.Timeout, "#1");
340 Assert.AreEqual (50, smtp.Timeout, "#2");
342 Assert.AreEqual (0, smtp.Timeout, "#3");
346 [ExpectedException (typeof (ArgumentOutOfRangeException))]
347 public void Timeout_Value_Negative ()
353 public void UseDefaultCredentials_Default ()
355 Assert.IsFalse (smtp.UseDefaultCredentials);
359 public void Deliver ()
361 var server = new SmtpServer ();
362 var client = new SmtpClient ("localhost", server.EndPoint.Port);
363 var msg = new MailMessage ("foo@example.com", "bar@example.com", "hello", "howdydoo\r\n");
365 Thread t = new Thread (server.Run);
370 Assert.AreEqual ("<foo@example.com>", server.mail_from);
371 Assert.AreEqual ("<bar@example.com>", server.rcpt_to);
375 public void Deliver_Envelope ()
377 var server = new SmtpServer ();
378 var client = new SmtpClient ("localhost", server.EndPoint.Port);
379 var msg = new MailMessage ("foo@example.com", "bar@example.com", "hello", "howdydoo\r\n");
381 msg.Sender = new MailAddress ("baz@example.com");
383 Thread t = new Thread (server.Run);
388 Assert.AreEqual ("<baz@example.com>", server.mail_from);
389 Assert.AreEqual ("<bar@example.com>", server.rcpt_to);
393 public void Deliver_Async ()
395 var server = new SmtpServer ();
396 var client = new SmtpClient ("localhost", server.EndPoint.Port);
397 var msg = new MailMessage ("foo@example.com", "bar@example.com", "hello", "howdydoo\r\n");
399 Thread t = new Thread (server.Run);
401 var task = client.SendMailAsync (msg);
404 Assert.AreEqual ("<foo@example.com>", server.mail_from);
405 Assert.AreEqual ("<bar@example.com>", server.rcpt_to);
407 Assert.IsTrue (task.Wait (1000));
408 Assert.IsTrue (task.IsCompleted, "task");