DIY writing email software

  
                              


In the face of Outlook Express, Foxmail, Eudora, Feiyang, personal letter and so on, you are also eager to try, you want to write one? You can achieve this by following the steps below.

First, try to send a manual letter

In the mail status window of Foxmail, a row of constantly changing characters is often displayed: for example, MAIL FROM:, RCPT TO, DATA, etc. Working status. But what is its internal mechanism? Let's "simulate" the process of email software sending.

Run TELNET.EXE in Windows 95/98 first. Turn on Partial Echo in its Terminal/Preferences menu. Open the "Connection" menu and select the "Remote System" menu item to open a dialog box. Fill in the address of your mailing server in the "Host Name" field, fill in the "Port" column, and start the connection.

After the connection, the following characters will appear:

220 XXX Sendmail ...

220 is the return code of the SMTP server, indicating that it is ready, ready to send Letter, SENDMAIL is the SENDMAIL process responsible for sending messages in the UNIX system, and XXX is the name of the server.

First, you must indicate your identity to the server so that the system can automatically compose the log. The format of the command is:

HELO your host name

When finished, the server returns 250, indicating acceptance. You can now tell the server your outgoing address so that the other party can reply or send a failed back.

MAIL FROM: Your email address

Note that the colon cannot be omitted. If the system approves, it also returns 250. At this point, you can tell the SMTP server who your letter is to be sent to. Please enter:

RCPT TO: Address of the recipient

If this command is used multiple times, it means that the letter is sent to multiple recipients at the same time. After receiving the returned 250 code, you can start transferring data. Enter the DATA command and press Enter. After you see the return code 354, you can start entering the contents of the email. At the end of the content, you will be given a separate period in a new line, and then press Enter to tell the server that your letter has been lost. At this time, you can receive the 250 code returned by the server, indicating OK.

Finally, type the QUIT command to terminate this TELNET session. After the server returns to 221, it will actively disconnect.

Second, write a small program yourself

Our goal is to design a VB program to let it complete this series of signaling actions. Therefore, you must understand how to use the Winsock communication control in Visual Basic.

Open VB5, create a new "Standard EXE" project, and add the Microsoft Winsock Control 5.0 control in the "Project/Parts" menu. The LocalPort property of the Winsock control is used to set the network port used by the local host, which is typically 25. The RemoteHost and RemotePort properties can be set to which port of the network server to communicate with. Here we set the RemotePort to 25 to connect to the SMTP server. The Protocol attribute is set to 0-sckTCPProtocol, which means that the TCP protocol is used.

There are several ways Winsock is what we need. The Connect method is used to set up the above four properties and start to connect to the remote host. When Winsock receives the data, it generates a DataArrival event. In this event you can use the GetData method to get the received data. Alternatively, you can send data using the SendData method.

With this information in hand, we can design a simple sending program. The controls used in the program are as follows:

Just after the connection and after each command is executed, the server's return code is checked, so the part that sends the message must be placed in the Winsock1_DataArrival event. The code to implement this feature is as follows:

Private Sub cmdSend_Click()

Winsock1.RemoteHost = txtServer.Text

Winsock1.Connect

End Sub

----------------------Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim Data As String

Winsock1.GetData Data

Select Case Val(Data)' Process Return Code

Case 220' If it is just connected, send the helo command and set the flag to helo to identify

Tag ="helo"

Winsock1.SendData"helo"&txtServer.Text&vbCrLf

Case 250 'If you return 250 (OK)

Select Case Tag ' Determine the flag set when the command is executed to distinguish which command has just been executed

Case"helo"

Winsock1.SendData"mail from:"&txtFrom.Text&vbCrLf

Tag = "mail"

Case"'s mail" 'If you just executed the MAIL command, execute RCPT command

Winsock1.SendData"rcpt to:"&txtTo.Text&vbCrLf

Tag = "rcpt"

Case"rcpt" 'If you just executed the RCPT command, execute DATA command

Winsock1.SendData"data"&vbCrLf

Tag = "data"

Case"data" 'If you just executed the DATA command, execute the QUIT command

Winsock1.SendData"quit"&vbCrLf

Tag = "quit"

End Select

Case 251

Winsock1.SendData"data" &vbCrLf

Tag = "data"

Case 354

Winsock1.SendData"From:"&txtFrom.Text&vbCrLf&"To:"&txtTo.Text&vbCrLf&"Subject:"&txtSubject.Text&vbCrLf& "Date:" &Now()

Winsock1.SendData"MIME-Version: 1.0"&vbCrLf&"Content-Type: text/plain;charset=""gb2312"""&vbCrLf&"Content-Transfer-Encodin g: 8bit"&vbCrLf&vbCrLf

Winsock1.SendData txtContent.Text

Winsock1.SendData vbCrLf&"."&vbCrLf

Case 221' is the return code of the QUIT command, it is cleared Command execution flag, ready to disconnect

Tag = ""

Case Else 'If it is another return code, it will prompt an error

Winsock1.SendData"quit"&vbCrLf< Br>

Tag = "quit"

MsgBox"Error"&Data

End Select

End Sub

Third, mail header and encoding < Br>

When you receive an email that you just sent yourself, you may find that this letter has neither "From", "To", nor "Subject" and "Date". This is because the function of encoding the header information and the body of the mail has not been added to the program. A typical mail header looks like this:

From:"cxx"

To:

Subject: =?gb2312?B?taW797TLtKbK5MjrytW8/sjL?=

Date: Mon, 20 Dec 1999 16:46:58+0800

MIME-Version: 1.0

Content-Type: text/plain;

charset=”gb2312 "

Content-Transfer-Encoding: quoted-printable

The encoding method specified in this header is quoted-printable. In fact, most servers in China support 8-bit encoding, so there is no need to Then encode. The modified program section is as follows:

Sub Winsock1_DataArrival()

......

Case 354

Winsock1.SendData"From:" &txtFrom.Text&vbCrLf&"To:"&txtTo.Text&vbCrLf&"Subject:"&txtSubject.Text&vbCrLf&"Date:"&Now()

Winsock1.SendData"MIME-Version: 1.0"&vbCrLf&"Content-Type: text/plain; Charset=""gb2312"""&vbCrLf&"Content-Transfer-Encoding: 8bit"&vbCrLf&vbCrLf

Winsock1.SendData txtContent.Text

Winsock1.SendData vbCrLf&"."&vbCrLf

......

End Sub

So far, a simple email software has been written successfully. Interested readers can add or subtract features as needed to meet their needs.

Copyright © Windows knowledge All Rights Reserved