Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SMTP Control using System.Net.Sockets
Message
General information
Forum:
ASP.NET
Category:
Other
Title:
SMTP Control using System.Net.Sockets
Miscellaneous
Thread ID:
00832838
Message ID:
00832838
Views:
40
I have written a wrapper class around System.Net.Sockets to connect to smtp server and send out emails.

Everything works fine when I execute it once. But if I try to execute it again, it hangs. I have to close the application, rebuild the project and start again to make it work. It seems to me that the socket is holding on to the some resources eventhough I have used Socket.ShutDown(SocketShutdown.Both) and Socket.Close() at the end. I spent a lot of time but couldn't figure out the issue.

Here is the code: DoSocketGet() is the public method being called to send out emails.
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;
		}
	}
}
- Jayesh
Next
Reply
Map
View

Click here to load this message in the networking platform