First, we create a class that inherits the TcpClient class of the namespace System. Net. Sockets. The TcpClient class provides a simple method for connecting, sending, and receiving network data streams. The GetStream method is used to create a network stream (NetworkStream ). NetworkStream is used to send data to and receive network streams from remote hosts.
Copy codeThe Code is as follows:
Public class ClientConnection: TcpClient
{
Private NetworkStream _ NetworkStream = null;
Private StreamReader _ StreamReader = null;
Private StreamWriter _ StreamWriter = null;
Public void Initialise ()
{
_ NetworkStream = this. GetStream ();
_ StreamReader = new StreamReader (_ NetworkStream,
System. Text. Encoding. Default, false, this. ReceiveBufferSize );
_ StreamWriter = new StreamWriter (_ NetworkStream,
System. Text. Encoding. Default, this. SendBufferSize );
}
Public void Send (string s)
{
_ StreamWriter. WriteLine (s );
_ StreamWriter. Flush ();
}
Public string Read ()
{
Return _ StreamReader. ReadLine ();
}
}
Next, we will create a class for sending emails. This class has several attributes to set some information about the sent emails.
Copy codeThe Code is as follows:
Public string MailServer = "127.0.0.1 ";
Public string From = "";
Public string To = "";
Public string Body = "";
Public string Subject = "";
And a method for sending emails. This method creates a connection with the server name and port. Commands can be sent to remote hosts.
Copy codeThe Code is as follows:
Public void Send ()
{
Tcp = new ClientConnection ();
Tcp. Connect (MailServer, 25 );
Tcp. Initialise ();
SendCommandToServer ("HELO" + System. Net. Dns. GetHostName ());
SendCommandToServer ("mail from:" + From + "\ r \ n ");
SendCommandToServer ("rcpt to:" + To + "\ r \ n ");
String StrHeaders = "";
StrHeaders + = "From:" + From + "\ r \ n ";
StrHeaders + = "To:" + To + "\ r \ n ";
StrHeaders + = "Subject:" + Subject + "\ r \ n ";
StrHeaders + = "Content-type: text/plain; charset = \" ISO-8859-1 \ "+" \ r \ n ";
SendCommandToServer ("DATA \ r \ n" + StrHeaders );
SendCommandToServer (Body + "\ r \ n. \ r \ n ");
SendCommandToServer ("QUIT \ r \ n ");
}
There is also a private method to send a command to the local server.
Copy codeThe Code is as follows:
Private void SendCommandToServer (string cmd)
{
Tcp. Send (cmd );
Response = tcp. Read ();
System. Web. HttpContext. Current. Trace. Warn ("Response", Response );
}
Now, this class is basically complete. Developers can also create error receipt and release resources (using the Close () method in the TcpClient class ). Similarly, these attributes can be extended to include more information in the mail.
Finally, we can write some email sending code in the aspx file. In this example, "localhost" is the mail server. You can change this as needed, or view my article on how to set up your local server for sending emails.
Copy codeThe Code is as follows:
<% @ Import Namespace = "MyComponents" %>
<Script runat = "server">
Protected void Page_Load (Object Src, EventArgs E)
{
If (IsPostBack)
{
Mail Mailer = new Mail ();
Mailer. From = From. Text;
Mailer. To = To. Text;
Mailer. Subject = Subject. Text;
Mailer. Body = Body. Text;
Mailer. MailServer = "localhost ";
Mailer. Send ();
}
}
</Script>
The following is the complete code.
Copy codeThe Code is as follows:
Using System. IO;
Using System. Net. Sockets;
Namespace MyComponents
{
Public class ClientConnection: TcpClient
{
Private NetworkStream _ NetworkStream = null;
Private StreamReader _ StreamReader = null;
Private StreamWriter _ StreamWriter = null;
Public void Initialise ()
{
_ NetworkStream = this. GetStream ();
_ StreamReader = new StreamReader (_ NetworkStream,
System. Text. Encoding. Default, false, this. ReceiveBufferSize );
_ StreamWriter = new StreamWriter (_ NetworkStream,
System. Text. Encoding. Default, this. SendBufferSize );
}
Public void Send (string s)
{
_ StreamWriter. WriteLine (s );
_ StreamWriter. Flush ();
}
Public string Read ()
{
Return _ StreamReader. ReadLine ();
}
}
Public class Mail
{
Public string MailServer = "127.0.0.1 ";
Public string From = "";
Public string To = "";
Public string Body = "";
Public string Subject = "";
Private ClientConnection tcp = null;
Private string Response = "";
Public void Send ()
{
Tcp = new ClientConnection ();
Tcp. Connect (MailServer, 25 );
Tcp. Initialise ();
SendCommandToServer ("HELO" + System. Net. Dns. GetHostName ());
SendCommandToServer ("mail from:" + From + "\ r \ n ");
SendCommandToServer ("rcpt to:" + To + "\ r \ n ");
String StrHeaders = "";
StrHeaders + = "From:" + From + "\ r \ n ";
StrHeaders + = "To:" + To + "\ r \ n ";
StrHeaders + = "Subject:" + Subject + "\ r \ n ";
StrHeaders + = "Content-type: text/plain; charset = \" ISO-8859-1 \ "+" \ r \ n ";
SendCommandToServer ("DATA \ r \ n" + StrHeaders );
SendCommandToServer (Body + "\ r \ n. \ r \ n ");
SendCommandToServer ("QUIT \ r \ n ");
}
Private void SendCommandToServer (string cmd)
{
Tcp. Send (cmd );
Response = tcp. Read ();
System. Web. HttpContext. Current. Trace. Warn ("Response", Response );
}
}
}