You may be accustomed to using the JMail component in an ASP program to send and receive mail, asp.net a built-in class that sends emails in the System.Web.Mail namespace, but this is only an illusion of CDOSYS. Developers can use an alternative to its SMTP mail service. In this article, I'll show you how to create a full-featured SMTP mail service for asp.net.
First, we create a class that inherits the TcpClient class of the namespace System.Net.Sockets. The TcpClient class provides a simple way to connect, send, and receive data streams from the network. The GetStream method is used to create a network stream (NetworkStream). The Read and write network stream (NetworkStream) method is used to send data to a remote host and receive network streams from a remote host.
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 create the class that sends out the mail, which has several properties to set up some information about the message being sent.
public string mailserver = "127.0.0.1";
public string from = "";
public string to = "";
public string BODY = "";
public string Subject = "";
And a way to send mail. This method will use the server name and its port to create a connection. Instructions can be sent to a remote host.
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 T" + to + "\ r \ n");
String strheaders = "";
Strheaders + = "From:" + from + "\ r \ n";
Strheaders + = "T" + 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 of sending an instruction to the local server.
private void Sendcommandtoserver (string cmd)
{
Tcp. Send (CMD);
Response=tcp. Read ();
System.Web.HttpContext.Current.Trace.Warn ("Response", Response);
}
Now, this class is basically done. Developers can also create error receipts and release resources (using the close () method in the TcpClient class). Again, these properties can be extended to include more information in the message.
Finally, we can write some code to send the message in the ASPX file. In this example, "localhost" is a mail server. You can change this depending on the situation, or you can read my article about how to set up your local server for sending mail.
<%@ 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>
Technical Exchange is endless