How to create an ASP. NET-based SMTP mail service

Source: Internet
Author: User
Tags getstream

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 );
}
}
}

Related Article

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.