Create SMTP mail services for ASP.NET with C #

zhaozj2021-02-16  64

Create a SMTP mail service for ASP.NET

ASP.NET has a built-in class that sends an email in the System.Web.mail namespace, which is only a packaging of CDOSYS. Developers may want to use an alternative SMTP mail service. In this article, I will show how to create a fully functional SMTP mail service for ASP.NET. (Just a 70-line C # statement) preferred, we create a class that inherits the TCPClient class inheriting the namespace system.net.sockets. The TCPCLIENT class provides a simple method for connecting, transmitting, and receiving data streams. GetStream method is used to create a network stream. The method of reading and writing a network stream is used to send data to the remote host and receive the network stream from the 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 a class that sends an email, and this class has several properties to set some information about the sent mail.

Public string mailserver = "127.0.0.1"; public string from = "; public string to ="; public string body = "; public string subject ="

And a method of sending an email. This method will create a connection with the server name and its port. The instruction can be sent to the 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 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 way to send 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 completed. . Developers can also establish erroneous receipts and release resources (using Close () methods in TCPCLIENT classes). Similarly, these properties can be extended to include more information in the message.

Finally, we can write some code that sends mail in the ASPX file. In this example, "LocalHost" is a mail server. You can change this according to the situation, or you can see my article about how to set up your email.

<% @ Import namespace = "mycomponents"%>