One of the socket programming languages: sending emails using smtp protocol

Source: Internet
Author: User
Tags getstream ftp protocol

The purpose of this article is to serve as a record for me to learn socket programming. I have no technical content. The content of this article is basically written by referring to the online materials, first of all, I would like to thank my colleagues who have studied and written these technical articles. If you already know these technologies, I hope you can share them with me. I started to play.

In all previous projects, when the mail sending function is required, my first thought is to use the jmail component. Because of this, I have never studied how the smtp protocol works. In recent work, the mail function has just to be sent again, and third-party components cannot be installed on the server, just as you have started to become interested in socket programming. I want to use socket for implementation.

If you want to do it, first use google search, you can find a lot of information about the smtp protocol, recommend a good site for everyone-China protocol network (http://www.cnpaf.net /), there are various protocols and RFC documents.

. SMTP basic command set:

Command description
------------------------------
HELO identifies a user to the server
The sender can cheat and lie, but generally the server can detect it.

MAIL initialization Email transmission
Mail from:
RCPT identifies a single email recipient, often behind the MAIL command
Multiple rcpt:
After one or more RCPT commands, DATA indicates that all Email recipients have been identified, and DATA transmission is initiated to end.
VRFY is used to verify whether the specified user or email address exists. This command is often disabled by the server for security reasons.
EXPN verifies whether the specified mailbox list exists and expands the mailbox list.
HELP query commands supported by the server
No NOOP operation, the server should respond OK
QUIT end session
RSET resets the session and the current transmission is canceled.

If IIS is installed on your machine, you can enter
Telnet localhost 25
Helo localhost
Mail from: jimyhsu@163.com
Rcpt to: jimyhsu@163.com
...
To test smtp sending.

After learning about the smtp protocol, we started to use socket. The socket is used as an API of the TCP/IP protocol, so there are too many things to do. In fact, socket5 now supports TCP/IP and UDP protocols. What we need here is
System. Net. Sockets. TcpClient and System. Net. Sockets. NetworkStream. You can learn more about the members of System. Net. Sockets through msdn.

The Code is as follows:

// -- Create a connection to port 25 of the server
TcpClient SmtpServ = new TcpClient (smtp, 25 );
ListBox1.Items. Clear ();

// -- Display the initial server information
NetworkStream NetStrm = SmtpServ. GetStream ();
StreamReader RdStrm = new StreamReader (SmtpServ. GetStream ());
ListBox1.Items. Add (RdStrm. ReadLine ());

SendData = "HELO localhost" + CRLF;
SzData = System. Text. Encoding. ASCII. GetBytes (sendData. ToCharArray ());
NetStrm. Write (szData, 0, szData. Length );
ListBox1.Items. Add (RdStrm. ReadLine ());

// -- Flag the sender
SendData = "mail from:" + "<" + from + ">" + CRLF;
SzData = System. Text. Encoding. ASCII. GetBytes (sendData. ToCharArray ());
NetStrm. Write (szData, 0, szData. Length );
ListBox1.Items. Add (RdStrm. ReadLine ());

// -- Mark the recipient
SendData = "rcpt to:" + "<" + to + ">" + CRLF;
SzData = System. Text. Encoding. ASCII. GetBytes (sendData. ToCharArray ());
NetStrm. Write (szData, 0, szData. Length );
ListBox1.Items. Add (RdStrm. ReadLine ());

// -- Prepare the sent content
SendData = "DATA" + CRLF;
SzData = System. Text. Encoding. ASCII. GetBytes (sendData. ToCharArray ());
NetStrm. Write (szData, 0, szData. Length );
ListBox1.Items. Add (RdStrm. ReadLine ());

// -- Send topic
SendData = "SUBJECT:" + subject + CRLF;
// -- Send content
SendData = sendData + content + CRLF;
// -- 'End sending
SendData = sendData + "." + CRLF;
SzData = System. Text. Encoding. ASCII. GetBytes (sendData. ToCharArray ());
NetStrm. Write (szData, 0, szData. Length );
ListBox1.Items. Add (RdStrm. ReadLine ());

// -- Exit
SendData = "QUIT" + CRLF;
SzData = System. Text. Encoding. ASCII. GetBytes (sendData. ToCharArray ());
NetStrm. Write (szData, 0, szData. Length );
ListBox1.Items. Add (RdStrm. ReadLine ());

// -- Close the connection
NetStrm. Close ();
RdStrm. Close ();
ListBox1.Items. Add ("Connection closed ");
ListBox1.Items. Add ("sent successfully ");

The next section will discuss socket programming for the ftp protocol.

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.