code_junkie

Can I test SmtpClient before calling client.Send()?

Nov 13th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. SmtpClient client = new SmtpClient("mysmtpserver.com", myportID);
  2. client.Credentials = new System.Net.NetworkCredential("myusername.com", "mypassword");
  3.  
  4. public static class SmtpHelper
  5. {
  6. /// <summary>
  7. /// test the smtp connection by sending a HELO command
  8. /// </summary>
  9. /// <param name="config"></param>
  10. /// <returns></returns>
  11. public static bool TestConnection(Configuration config)
  12. {
  13. MailSettingsSectionGroup mailSettings = config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
  14. if (mailSettings == null)
  15. {
  16. throw new ConfigurationErrorsException("The system.net/mailSettings configuration section group could not be read.");
  17. }
  18. return TestConnection(mailSettings.Smtp.Network.Host, mailSettings.Smtp.Network.Port);
  19. }
  20.  
  21. /// <summary>
  22. /// test the smtp connection by sending a HELO command
  23. /// </summary>
  24. /// <param name="smtpServerAddress"></param>
  25. /// <param name="port"></param>
  26. public static bool TestConnection(string smtpServerAddress, int port)
  27. {
  28. IPHostEntry hostEntry = Dns.GetHostEntry(smtpServerAddress);
  29. IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], port);
  30. using (Socket tcpSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  31. {
  32. //try to connect and test the rsponse for code 220 = success
  33. tcpSocket.Connect(endPoint);
  34. if (!CheckResponse(tcpSocket, 220))
  35. {
  36. return false;
  37. }
  38.  
  39. // send HELO and test the response for code 250 = proper response
  40. SendData(tcpSocket, string.Format("HELO {0}rn", Dns.GetHostName()));
  41. if (!CheckResponse(tcpSocket, 250))
  42. {
  43. return false;
  44. }
  45.  
  46. // if we got here it's that we can connect to the smtp server
  47. return true;
  48. }
  49. }
  50.  
  51. private static void SendData(Socket socket, string data)
  52. {
  53. byte[] dataArray = Encoding.ASCII.GetBytes(data);
  54. socket.Send(dataArray, 0, dataArray.Length, SocketFlags.None);
  55. }
  56.  
  57. private static bool CheckResponse(Socket socket, int expectedCode)
  58. {
  59. while (socket.Available == 0)
  60. {
  61. System.Threading.Thread.Sleep(100);
  62. }
  63. byte[] responseArray = new byte[1024];
  64. socket.Receive(responseArray, 0, socket.Available, SocketFlags.None);
  65. string responseData = Encoding.ASCII.GetString(responseArray);
  66. int responseCode = Convert.ToInt32(responseData.Substring(0, 3));
  67. if (responseCode == expectedCode)
  68. {
  69. return true;
  70. }
  71. return false;
  72. }
  73. }
  74.  
  75. if (!SmtpHelper.TestConnection(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)))
  76. {
  77. throw new ApplicationException("The smtp connection test failed");
  78. }
Add Comment
Please, Sign In to add comment