using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text; using System.IO; using System.Data.SqlClient; using System.Net.Sockets; using System.Net; using System.Diagnostics; namespace mySMTP { /// <summary> /// Summary description for mySocket. /// </summary> public class mySocket:System.Object { public void DoSocketGet(string server, bool tlTrace) { System.String[] SendBuffer = new System.String[5] ;; System.String[] ResponseCode = {"220","250","250","354","250"}; string gcMIME = "Test MIME Data to ##cEmail## address"; SendBuffer[0] = "HELO epd.hq.edithroman.com\r\n" ; SendBuffer[1] = "MAIL FROM: SocketTest\r\n" ; SendBuffer[3] = "DATA\r\n"; //Sets up variables and a string to write to the server Encoding ASCII = Encoding.ASCII; String strRetPage = null; // IPAddress and IPEndPoint represent the endpoint that will // receive the request. // Get the first IPAddress in the list using DNS. IPAddress hostadd = Dns.Resolve(server).AddressList[0]; IPEndPoint EPhost = new IPEndPoint(hostadd, 25); //Creates the Socket for sending data over TCP. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Connects to the host using IPEndPoint. s.Connect(EPhost); if (!s.Connected) { MessageBox.Show("Unable to connect to host"); } SqlConnection loConnection = new SqlConnection("myConnectionString"); string lcSQL = "SELECT TOP 100 cEmail FROM myTable"; loConnection.Open(); SqlCommand loCmd = new SqlCommand(lcSQL, loConnection); SqlDataReader oDataReader = loCmd.ExecuteReader(CommandBehavior.CloseConnection); string lcEmail; int ii = 0, jj = 0; //Sends the GET text to the host. while (oDataReader.Read()) { jj++; lcEmail = oDataReader["cEmail"].ToString().Trim(); gcMIME = gcMIME.Replace("##cEmail##", lcEmail); SendBuffer[2] = "RCPT TO: " + lcEmail + "\r\n"; SendBuffer[4] = gcMIME + "\r\n.\r\n" ; for (ii=0;ii<5;ii++) { string Get = SendBuffer[ii]; Byte[] ByteGet = ASCII.GetBytes(Get); Byte[] RecvBytes = new Byte[1024]; // Send Data s.Send(ByteGet, ByteGet.Length, SocketFlags.None); // Receives the page, looping until all bytes are received Int32 bytes = s.Receive(RecvBytes, 1024, 0); strRetPage = ASCII.GetString(RecvBytes, 0, bytes); // Error Checking if (ii==0) { if (strRetPage.Substring(0, 3) != "250" && strRetPage.Substring(0, 3) != "220") MessageBox.Show("Error 1"); } else { if (strRetPage.Substring(0, 3) != ResponseCode[ii]) { MessageBox.Show("Error 2"); } } } } //Disconnect this.Disconnect(s); oDataReader.Close(); } public void Disconnect(Socket ts) { Byte[] ByteGet1 = Encoding.ASCII.GetBytes("QUIT\r\n"); Byte[] RecvBytes1 = new Byte[1024]; ts.Send(ByteGet1, ByteGet1.Length, SocketFlags.None); //Receives the page Int32 bytes1 = ts.Receive(RecvBytes1, 1024, 0); Encoding.ASCII.GetString(RecvBytes1, 0, bytes1); ts.Shutdown(SocketShutdown.Both); ts.Close(); return; } } }