Use C #. NET to implement the email client program,

Source: Internet
Author: User
Tags string back mail exchange

Use C #. NET to implement the email client program,

Use C #. NET to implement the email client program

Zhou Huaqing Dai yaohui (Department of computer and communication, Donghua Institute of Technology, Fuzhou 344000, China)

【Abstract】 using C #, a new object-oriented and type-safe programming language introduced in Visual Studio. NET, develop an email client program on the. NET platform. This article describes how SMTP (Simple Mail Transfer Protocol) and POP3 (Post Office Protocol) work through socket programming, then it explains how to develop the mail sending program of the e-mail client based on SMTP protocol and the mail receiving program of the e-mail client based on POP3 protocol.

Key words: Socket Simple Mail transmission protocol Post Office Protocol

Socket programming in 1 C #

Socket is the cornerstone of communication and the basic operation unit supporting TCP/IP network communication. The socket can be seen as the two-way communication endpoint of processes between different hosts. It forms a programming interface between a single host and the entire network. The socket exists in the communication domain. The communication domain is an abstract concept introduced to process General threads through socket communication. Sockets usually exchange data with sockets in the same domain (Data Exchange may also cross the boundaries of the domain, but some interpreter must be executed at this time ). Various processes use the same domain to communicate with each other using Internet Protocol clusters.

For Socket programming in C #, use the Socket class ,.. NET Framework Socket class is included in the System. net. A very important class in the Sockets namespace, which provides a lot of methods for implementing network programming. You can use Socket to develop windows network applications, which follow roughly the same steps in most cases.

Before using it, you must first create an instance of the Socket object, which can be achieved through the Socket class constructor:

Public Socket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType );

The addressFamily parameter specifies the addressing scheme used by the Socket; The socketType parameter specifies the Socket type; the protocolType parameter specifies the protocol used by the Socket.

The following example creates a Socket that can be used for communication over TCP/IP-based networks (such as the Internet.

Socket s = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );

To use UDP instead of TCP, you need to change the protocol type, as shown in the following example:

Socket s = newSocket (AddressFamily. InterNetwork, SocketType. Dgram, ProtocolType. Udp );

Once a Socket is created on the client, you can Connect to the specified server through the Connect method and Send data to the remote server through the Send/SendTo method, then, you can use the Receive/ReceiveFrom method to Receive data from the server. on the server side, you need to use the Bind method to Bind the specified interface to associate the Socket with a local endpoint, the Listen method is used to Listen for requests on this interface. When listening for a connection to the user end, the Accept is called to complete the connection operation, and a new Socket is created to process incoming connection requests. After using the Socket, remember to use the Shutdown method to disable the Socket and use the Close method to Close the Socket.

2. SMTP and POP3 protocols

Two protocols are required for sending/receiving emails over the Internet: SMTP (Simple Mail Transfer Protocol) and POP3 (Post Office Protocol ). The following describes how to send and receive emails through SMTP and POP3 protocols.

1, describes how to send an email from the sender using the host (Client1) to the mailbox one@server1.com from the mailbox two@server2.com (where the mail system with the domain name server1.com is installed on Server1, the email system with the domain name server2.com is installed on Server2. The recipient receives the email through the host (Client2. Different network protocols must be used in each phase of mail Transmission.

(1) The sender uses the Mail Client software to write an email to the recipient at Client 1. First, the Mail is sent from Client 1 to the server server1. This step uses the SMTP protocol when client 1 is sent to the Server.

 

Figure 1 Email Delivery Process

(2) mail can be directly sent from the sender's mail server Server1 to the recipient's mailbox two@server2.com server Server2, may also need to go through a third-party mail server Server3 for the transfer and then delivered to the recipient, this process is called Relay. In this step, the SMTP protocol is used for Mail transmission between the Mail Server. After a mail is sent to the recipient's server Server2, Server2 is responsible for delivering the mail to the recipient's mailbox two@server2.com and saving it in the disk array of server2. The mail transfer process ends.

(3) when the recipient accesses his mailbox two@server2.com on Server2, the Mail Client software Client2 is used to download the Mail in Server2 mailbox to the local hard disk to read. This step downloads emails from the Server to the Client using the POP3 protocol.

In fact, the development of the e-mail client uses Socket programming for dialog communication and completes mail Transmission according to the SMTP protocol and POP3 protocol specifications.

3. Implementation of the mail sending module

SMTP is a widely used upper-layer protocol in the TCP/IP protocol stack. SMTP defines how mail is transmitted between two users, and uses spooling to allow mail to be sent from a local application to an SMTP application. The SMTP application stores the email in a device or memory. Once the email arrives at the SMTP application, the email is placed in a queue, and a server checks whether the email arrives, then try to deliver the arrived mail. If the recipient of the email does not exist, the server will try again. In the end, if the email cannot be delivered, the email will be discarded or returned to the sender of the email. This concept is called end-to-end delivery, and it stores the mail in the queue until the mail is delivered. We can find the SMTP discussion from the two RFC. RFC822 describes the structure of the message, including the envelope. RFC821 specifies the protocol for controlling mail exchange between two machines.

The following module uses the TcpClient class to develop an SMTP client to send emails.

3.1 send SMTP commands

Generally, the SMTP server recognizes the UTF8 code. All sending commands are UTF-8 encoded. Each Command ends with a line break. Run the following code to send commands.

Private void WriteToNetStream (ref NetworkStream NetStream. string Command)

{String stringToSend = Command + "\ r \ n ";

Byte [] arrayToSend = System. Text. Encoding. UTF8.GetBytes

(StringToSend. ToCharArray ());

NetStream. Write (arrayToSend, 0, arrayToSend. Length );}

3.2 accept the promise code

The SMTP server sends a response to each of them. The following code allows you to accept the promise code.

Private string ReadFromNetStream (ref NetworkStream NetStream)

{Byte [] bb = new byte [1, 512];

NetStream. Read (bb, 0, bb. Length );

String read = System. Text. Encoding. UTF8.GetString (bb );}

3.3 check the response code

The sending client must check the response code to determine whether the server has executed the command. If the server does not execute the command, resend the command or take other measures. The following code checks whether the server correctly executes the command.

Private string checkForError (string strMessage, string check)

{If (strMessage. Indexof (check) =-1)

{Return "err ";}

Else {return "correct ";}}

The first parameter of the preceding method is the server response information, and the second parameter is the authorization code to be checked. If the message returned by the server does not contain the string of the second parameter, "err" is returned, indicating that the server did not correctly execute the command.

3.4 send email

The client sends the DATA command. After the server responds to the 354 request, the Mail content can be sent. The email is ended with <CRLF>. <CRLF>. The following code implements the mail sending function.

Private void sendMail (ref NetworkStream NetStream, string message)

{Byte [] attayToSend = System. Text. Encoding. UTF8.GetBytes

(Message. ToCharArray ());

NetStream. Write (arrayToSend, 0, arrayToSend. Length );}

4. Implementation of the mail receiving module

POP allows the local email UA (User Agent) to connect to the SERVER and return the email to the User's local system. The User can also read and respond to messages on the local machine. POP3UA connects to the server through TCP/IP (port 110 is usually used ). After the user name and password are authenticated, the UA can retrieve or delete the email using the POP3 command. POP3 is only the receiving protocol. POP3UA uses SMTP to send emails to the server.

Use the POP3 Protocol to develop an email receiving program and use USER, PASS, STAT, LIST, RETR, DELE, and QUIT commands. Start the development process.

4.1 send an authorization code

The sending command code also uses ASCII code. Use the following method to send an authorization code to the server:

Private void WriteToNetStream (ref NetworkStream NetStream. string Command)

{String stringToSend = Command + "\ r \ n ";

Byte [] arrayToSend = System. Text. Encoding. ASCII. GetBytes

(StringToSend. ToCharArray ());

NetStream. Write (arrayToSend, 0, arrayToSend. Length );}

4.2 receive Server Response

Generally, you can use both the ASCII code and the UTF8 code to receive the server response. Here, the ASCII code is used. The following methods are used to receive responses from the server.

Private string ReadFromNetStream (ref NetworkStream NetStream)

{Byte [] bb = new byte [1, 1024];

NetStream. Read (bb, 0, bb. Length );

String read = System. Text. Encoding. ASCII. GetString (bb );

Return read ;}

4.3 accept emails

The UTF8 code is used to receive the email here. When <CRKF>. <CRLF> is encountered, Data Reading ends. Run the following code to receive emails:

Private void ReadMail (ref NetworkStream NetStream, int number)

{Int k = 0;

Bool check = false;

Byte [] bb = new byte [2, 6400];

While (! Check)

{K = NetStream. Read (bb, 0, bb. Length );

String read = System. Text. Encoding. UTF8.GetString (bb, 0, k );

Int x = read. IndexOf ("\ r \ n. \ r \ n ");

If (x! =-1)

{Check = true ;}

RichTextBox2.AppendText (read );

WriteToNetStream (ref

NetStream, "DELE" + number. ToString ());

String back = ReadFromNetStream (ref NetStream );

RichTextBox1.AppendText ("DELE" + number. ToString ()

+ "Command response:" + back + "\ r \ n ");}}

5 conclusion

The mail sending module and the mail receiving module are combined to form an email client program. In practical application, it is improved based on the above. If we further combine the database technology, this can develop an easy-to-use and reliable client program for email.

(By date:; email: zhouhuaqing@peoplemail.com.cn)

 

Programming e-mail client with c #. net

Abstract: Develop a program for e-mail client on the flat. net with C #. C # is a new language of visual studio. net, is modern, object-oriented and is safe in type. realize the network corresponspondepending Socket programming. expatiate the principle of SMTP (Simple Mail Transfer Protocol) and POP3 (Post office Protocol ), then explain developing a program to send e-mail according to the SMTP agreement development E-mail customer and carry to receive the procedure developing a program to receive e-mail according to the POP3 in detail.

Key Words: Socket; Simple Mail Transfer Protocol; Post Office Protocol

Contribution

[1] Xie xiiren. Computer Network. 2nd. Beijing: Tsinghua University Press, 1999

[2] Charles Petzold. Windows programming. Version 5th. Beijing: Peking University Press, 1999

[3] Anthony Jones and Jim Ohlund. Network Programming for Windows. 2nd. Beijing: Tsinghua University Press, 2002


C language for addition, subtraction, multiplication, division

?? What's the problem ??
Add a + B
Minus a-B
Multiply by a * B
Except a/B
Remainder a % B
Question ??
Main ()
{Int a, B, c;
Scanf ("% d", & a, & B );
C = a + B; printf ("% d \ n", c );
C = a-B; printf ("% d \ n", c );
C = a * B; printf ("% d \ n", c );
C = a/B; printf ("% d \ n", c );
C = a % B; printf ("% d \ n", c );
}

How to judge prime numbers using C language

A prime number is a number that cannot be divisible by any integer except 1 and itself. For example, 17 is a prime number because it cannot be 2 ~ Any integer in 16. Therefore, to determine whether an integer m is a prime number, you only need to set m to 2 ~ M-1 no integer can be divisible, m is a prime number.
In addition, the judgment method can be simplified. M does not need to be 2 ~ Remove each integer between the numbers expressed as 1 and 2 ~ √ Remove each integer between m. If m cannot be 2 ~ √ Any integer across m, m must be a prime number. For example, to determine whether 17 is a prime number, you only need to enable 17 to be 2 ~ Since each integer between 4 cannot be divisible, 17 is a prime number. (Cause: If m can be 2 ~ If any integer is divisible between expressed on and off, one of the two factors must be less than or equal to √ m, and the other one must be greater than or equal to √ m. For example, 16 can be divided by 2, 4, 8, 16 = 2*8, 2 less than 4, 8 greater than 4, 16 = 4*4, 4 = √ 16, so only 2 ~ There is no factor between 4)
# Include <stdio. h>
# Include <math. h>
Void main ()
{
Int m, I, k;
Printf ("enter an integer :");
Scanf ("% d", & m );
K = (int) sqrt (m );
For (I = 2; I <= k; I ++)
If (m % I = 0)
Break;
If (I> k)
Printf ("% d is a prime number. \ N ", m );
Else
Printf ("% d is not a prime number. \ N ", m );
}

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.