Abstract This article describes how to enhance the functionality of NMSMTP controls that send emails in BCB, and implement mail senders with identity authentication. Key words ESMTP, MIME, identity authentication Introduction In order to more effectively suppress the flooding of spam, the mail transceiver system of most websites uses the identity authentication function of the ESMTP service. That is, when the user sends an email, it is necessary to verify the user's identity. If the account or password is incorrect, the mail server will refuse mail. Borland C Builder 6 has a rich control for developers, which of course also include mail send control NMSMTP, which is easy to use, but the unique disadvantage is that the authentication function is not supported by mail. Through the analysis of the email sending protocol, the author designed a mail sender with an identity authentication function on the basis of the control. ESMTP Protocol Analysis In order to implement identity authentication, some of the contents of the ESMTP protocol is currently authentication. Let's take a look at this certification process, with the author in Netease's email address (where c represents the client, s represents the mail server): (1) C: Auth login (2) s: 334 DXNLCM5HBWU6 (3) C: D3lx2pux3nkx2nu (4) S: 334 UGFZC3DVCMQ6 (5) C: Password (6) S: 235 Authentication Successful detailed description: (1) The client sends a authentication instruction to the server. (2) The server returns the Base64 encoding string, and 334 means success. After the encoding string decodes, "UserName:", which requires the client to send the username. (3) The client sends the user name string encoded by the base64, here is "WYQ_JN_SD_CN". (4) The server returns the Base64 encoding string, and 334 means success. After encoding a string decoding, it is "Password:", which shows that the client will send the user password. (5) The client sends the Base64 encoded password, which is slightly. (6) The server returns a normal string, 235 means success, indicating that the authentication success can send an email. MIME BASE64 encoding explains a normal computer-encoded one byte is 8bit, 0 - ff is 256 different 8bit combinations. This base64 coding we have to introduce now is 6bit per byte, a total of 26 = 64 combinations. Each combination corresponds to a character, these characters are "AbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrStuvwxyz01234567 89 /." This means that every 3 normal codes can be converted to 4 Base64 encoding, then if the ordinary code required is not 3 integer multiple? Base64 specifies that the number of bits is insufficient, and then a few characters will be made a few '=' numbers. Design Ideas We can use NMSMTP controls to connect to a mail server. By calling the Connect method, listen to the onConnect event; we can add the authentication function in the OnConnect event. Here is some basic network communication functions that are mainly inherited from PowerSOCK from PowerSMTP, including Read, DataAvailable, SendBuffer, etc. to implement an identity authentication process. If the identity authentication is successful, you can continue to send mail; otherwise, prompt the error message, disconnect the network connection. The program implementation uses the BCB design as shown in Figure 1.
Figure 1 Program main interface
1. Call the connection function in the onclick event of the login button.
Void __fastcall tform1 :: logon1click (TOBJECT * Sender) {addlog ("Login" Edit1-> Text "..."); NMSMTP1-> Host = Edit1-> text; // host address NMSMTP1-> Port = 25; // host port, default is 25 nmsmtp1-> userid = edit4-> text; // User name nmsmtp1-> connection (); // Connect host} 2, process the onConnect event
Void __fastcall tform1 :: nmsmtp1connect (TOBJECT * Sender) {AddLog ("Connection Server Success."); Ansistring Data = "", RDATA = ""; BOOL B_OK; if (CheckBox1-> Checked) {data = "auth login / R / N "; // Login request command nmsmtp1-> sendbuffer (data.c_str (), data.length ()); // command issues RDATA = WaitforrePly (5); // Waiting for reception return data, 5 seconds must Returns B_OK = false; if (RData.Length ()> = 3) {// 334 means the server requires the user name IF (RData.trimleft (). Substring (0, 3) == "334") {AddLog "I'm verifying identity ..."); b_ok = true;}} f (! "Failure, exiting ..."); NMSMTP1-> disconnect (); return } Rdata = ""; DATA = Encode (edit4-> text) "/ r / n"; // username Convert to Base64 encoding. NMSMTP1-> sendbuffer (data.c_str (), data.length ()); // Send Username RDATA = WaitforrePly (5); B_OK = False; if (RData.Length ()> = 3) {// 334 Means The server requires input password if (RData.trimleft (). Substring (0, 3) == "334") {addlog ("Positive Verification Password ..."); b_ok = true;}} if (! B_ok) {addlog ("Login failed, exiting ..."); NMSMTP1-> disconnect (); return;} rdata = "; data = encode (edit5-> text) " / r / n "; // password conversion to base64 encoding. NMSMTP1-> sendbuffer (data.c_str (), data.length ()); // Send password rData = waitforreply (5); b_ok = false; if (RData.Length ()> = 3) {ified (RData.trimleft () .SUBSTRING (0, 3) == "235") {addlog ("Login Success ..."); b_ok = true;}} f (! B_ok) {addlog ("Login failed, is exiting ... "); nmsmtp1-> disconnect (); return;}} sendmail-> enabled = true; // Allow mail disconnect-> enabled = true; // Allow disconnect Logon1-> enabled = False; // Do not allow registration again} 3, MIME BASE64 encoding conversion