Create an ASP. NET-based SMTP Mail System

Source: Internet
Author: User

You may get used to using the JMAIL component to send and receive emails in ASP programs. Asp.net has a built-in class for sending emails in the System. Web. Mail namespace, but this is only an illusion of cdosys. Developers can use an alternative smtp mail service. In this article, I will show you how to create a fully functional 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 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.

Reference content 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.

Reference content 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.

Reference content 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 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 to send a command to the local server.

Reference content 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.

Reference content 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>

Never-ending technical exchanges

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.