Use BCB to develop an email program with the identity authentication function

Source: Internet
Author: User
   AbstractThis article introduces how to enhance the nmsmtp Control Function of sending emails in BCB and implement the mail sending program with the identity authentication function.

  KeywordsESMTP, mime, identity authentication

  Introduction

To effectively suppress the spread of spam, the identity authentication function of ESMTP is currently used in the mail sending and receiving systems of most websites. That is, when a user sends an email, the user's identity must be verified. If the account or password is incorrect, the email server rejects sending the email. Borland C ++ Builder 6 has a variety of controls for developers, including the mail sending control nmsmtp, which is convenient to use, however, the only drawback is that it does not support identity authentication when sending emails. Based on the analysis of the mail sending protocol, the author designs the mail sending program with the identity authentication function on the basis of the use of controls.

  ESMTP protocol analysis

To implement the identity authentication function, some content is added to the ESMTP protocol, which is identity authentication. Next, let's take a look at this authentication process, taking the author's mailbox on Netease as an example (C indicates the client, s indicates the mail server ):

(1) c: AUTH LOGIN

(2) S: 334 dxnlcm5hbwu6

(3) c: d3lxx2pux3nkx2nu

(4) S: 334 ugfzc3dvcmq6

(5) C: Skip the password

(6) S: 235 authentication successful

Detailed description:

(1) the client sends authentication commands to the server.

(2) The server returns a base64 encoded string. 334 indicates success. The encoded string is decoded as "username:", indicating that the user name must be sent from the client.

(3) The username string sent by the client in base64 encoding. "wyq_jn_sd_cn" is used here ".

(4) The server returns a base64 encoded string. 334 indicates success. The decoded string is "password:", indicating that the client needs to send the user password.

(5) the client sends a base64 encoded password string, which is omitted here.

(6) The server returns a normal string. The value 235 indicates that authentication is successful and an email can be sent.

  Mime base64 encoding explanation

Generally, an 8-bit byte is encoded by a computer, and 0--ff is a 256 different 8-bit combination. The base64 encoding we want to introduce now is 6 bits per byte, with a total of 26 = 64 combinations. Each combination corresponds to one character, which is "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz01234567 89 + /." This means that each three common encodings can be converted to four base64 encodings. What if the common encoding to be converted is not an integer multiple of three? Base64 requires that the number of bytes with less than a few digits is supplemented by 0, and then the number of = is supplemented by a few characters.

  Design Concept

We can use the nmsmtp control to connect to the mail server. Call the connect method and then listen to the onconnect event. In the onconnect event, we can add the identity authentication function. Here, we mainly use some basic network communication functions inherited by nmsmtp from powersock, including read, dataavailable, and sendbuffer to implement identity authentication. If the identity authentication is successful, you can continue sending the email; otherwise, an error message is prompted to disconnect the network.

  Program Implementation

Use BCB to design the form shown in 1.


Figure 1 Main Program Interface

1. Call the connection function in The onclick event of the logon button.

Void _ fastcall tform1: logon1click (tobject * sender)

{

Addlog ("logging on" + edit1-> text + "......");

Nmsmtp1-> host = edit1-> text; // host address

Nmsmtp1-> Port = 25; // host port. The default value is 25.

Nmsmtp1-> userid = edit4-> text; // User Name

Nmsmtp1-> connect (); // connect to the host

}

2. Handling onconnect events

Void _ fastcall tform1: nmsmtp1connect (tobject * sender)

{

Addlog ("the server is connected successfully. ");

Ansistring DATA = "", RDATA = "";

Bool B _ OK;

If (checkbox1-> checked ){

Data = "auth login/R/N"; // Logon Request command

Nmsmtp1-> sendbuffer (data. c_str (), Data. Length (); // Command issued

RDATA = waitforreply (5); // wait for receiving the returned data, which must be returned within 5 seconds

B _ OK = false;

If (RDATA. Length ()> = 3 ){

// 334 means that the server requires the user name

If (RDATA. trimleft (). substring (334) = "){

Addlog ("verifying identity ......");

B _ OK = true;

}

}

If (! B _ OK ){

Addlog ("Logon Failed, exiting ......");

Nmsmtp1-> disconnect ();

Return;

}

RDATA = "";

Data = encode (edit4-> text) + "/R/N"; // convert the username to base64 encoding.

Nmsmtp1-> sendbuffer (data. c_str (), Data. Length (); // user name for sending

RDATA = waitforreply (5 );

B _ OK = false;

If (RDATA. Length ()> = 3 ){

// 334 indicates that the server requires a password

If (RDATA. trimleft (). substring (334) = "){

Addlog ("verifying password ......");

B _ OK = true;

}

}

If (! B _ OK ){

Addlog ("Logon Failed, exiting ......");

Nmsmtp1-> disconnect ();

Return;

}

RDATA = "";

Data = encode (edit5-> text) + "/R/N"; // convert the password to base64 encoding.

Nmsmtp1-> sendbuffer (data. c_str (), Data. Length (); // send a password

RDATA = waitforreply (5 );

B _ OK = false;

If (RDATA. Length ()> = 3 ){

If (RDATA. trimleft (). substring (235) = "){

Addlog ("Login successful ......");

B _ OK = true;

}

}

If (! B _ OK ){

Addlog ("Logon Failed, exiting ......");

Nmsmtp1-> disconnect ();

Return;

}

}

Sendmail-> enabled = true; // you can send emails.

Disconnect-> enabled = true; // disconnect allowed

Logon1-> enabled = false; // you cannot log on again.

}

3. Mime base64 encoding and conversion

Ansistring tform1: encode (ansistring S)

{

Int m_len; // String Length

Int I; // cyclic variable

Int m_tmp; // Temporary Variable

Ansistring m_64code; // stores base64 encoded strings

Char * M_s; // temporary storage parameter string
 
// Base64 sequence table

Char m_64 [] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /";

M_len = S. Length (); // obtain the string length.

M_s = S. c_str ();

M_64code = ""; // The return string is null.

// Process characters less than 3

For (I = 0; I <m_len-m_len % 3; I + = 3 ){

M_tmp = M_s [I]/4;

M_64code + = m_64 [m_tmp];

M_tmp = M_s [I] % 4*16 + M_s [I + 1]/16;

M_64code + = m_64 [m_tmp];

M_tmp = M_s [I + 1] % 16*4 + M_s [I + 2]/64;

M_64code + = m_64 [m_tmp];

M_tmp = M_s [I + 2] % 64;

M_64code + = m_64 [m_tmp];

}

// If the length of the string is divided by 3 to 2, the number of digits that are insufficient is supplemented by 0, and the end is supplemented by "="

If (m_len % 3 = 2 ){
 
M_tmp = M_s [m_len-2]/4;

M_64code + = m_64 [m_tmp];

M_tmp = M_s [m_len-2] % 4*16 + M_s [m_len-1]/16;

M_64code + = m_64 [m_tmp];

M_tmp = M_s [m_len-1] % 16*4;

M_64code + = m_64 [m_tmp];

M_64code + = ';

}

// If the length of the string is divided by 3 and the remainder is 1, the number of digits below is supplemented by 0, and the end is supplemented by two "="

If (m_len % 3 = 1 ){

M_tmp = M_s [m_len-1]/4;

M_64code + = m_64 [m_tmp];

M_tmp = M_s [m_len-1] % 4*16;

M_64code + = m_64 [m_tmp];

M_64code + = "= ";

}

Return m_64code;

}

  Conclusion

In Windows 2000, this program is compiled and debugged Using Borland C ++ Builder 6.0. By doing experiments using Netease And Sina Mail respectively, identity authentication and email sending functions can be successfully completed.

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.