How to create a specific method for asp.net SMTP mail service _ Practical Tips

Source: Internet
Author: User
Tags getstream httpcontext readline

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.

Copy Code code 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 create the class that sends out the mail, which has several properties to set up some information about the message being sent.

Copy Code code as follows:

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.

Copy Code code 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 of sending an instruction to the local server.

Copy Code code 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 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.

Copy Code code 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>

Here is the complete code.

Copy Code code 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.