Send emails using SMTP

Source: Internet
Author: User

I,
Assuming that the sender jmst66 (jmst66@163.com) is sent to the neuwjyou@sina.com mailbox, the commands returned by the following hosts indicate the commands returned when the command succeeds. The sending process is as follows:

1. Connect to the host smtp.163.com
Host return: 220 163.com anti-spam GT for coremail System (163com [20050206])

2. First session
Send command: EHLO mailto: jmst66@163.com % 20R % 20n (Note: After the command is sent, there is a line feed identifier "/R/N ")
Returned data:
250-mail
250-pipelining
250-auth login Plain
250-auth = login Plain
250 8 bitmime

3. User Logon
Send command: auth login/R/n
Returned data: 334 dxnlcm5hbwu6 (Note: This is the base64 encoding of username)
Sending account: am1zddy2 (base64 encoding of jmst66)

Returned data: 334 ugfzc3dvcmq6 (Note: This is the base64 encoding of password)
Sending password: xxxxxxx (base64 encoding of the password of jmst66)

Returned data: 235 authentication successful (logon successful)

4: email sending instructions
Send command: mail from: <mailto: jmst66@163.com> % 20R % 20n
Returned data: 250 mail OK

5. Description of the target email address
Send command: rcpt to: <mailto: neuwjyou@sina.com> % 20R % 20n
Returned data: 250 mail OK

6. send an email request
Send command: Data/R/n
Return command: 354 end data with <CR> <LF>. <CR> <LF>

7. Send the mail header and body
Send data: the complete data is directly ended with "<CR> <LF>. <CR> <LF>"
Returned data: 250 mail OK queued as smtp1, wkjrc7d7f6mfoodga_i7ma =. 52140s2 1189584903

8. End sending data
Send command: Quit/R/n
Receive data: 221 bye

9. Disconnect

II,
The specific program implementation is briefly described as follows:
1. Connect to the host smtp.163.com

Sockaddr_in serveraddr;
Socket sock;
Hostent * Host;
Host = gethostbyname (serverhostname. c_str ());
If (host = NULL ){
Return-1;
}

Sock = socket (af_inet, sock_stream, 0 );
Serveraddr. sin_addr = * (struct in_addr *) Host-> h_addr;
Serveraddr. sin_family = af_inet;
Serveraddr. sin_port = htons (SERVERPORT );

If (connect (sock, (const sockaddr *) & serveraddr, sizeof (sockaddr) = socket_error)
{
Closesocket (sock );
Return-1;
}

// Add a value to determine whether the data returned by the host indicates success
Deal_return_code (getresponsecode (sock, 220, STR ));

2. First session
Char buffer [buffer_size];
Sprintf (buffer, "ehlo % S/R/N", from); // from is Char data. Storage sending Address
Send (sock, buffer, strlen (buffer), 0 );

3. User Logon
Sprintf (buffer, "auth login/R/N ");
Send (sock, buffer, strlen (buffer), 0 );

// User Name
// User name is coded by base64.
Encodingbase64 (username. c_str (), buffer); // encode the user account with base64
Strcat (buffer, "/R/N ");
Send (sock, buffer, strlen (buffer), 0 );

// Password coded by base64
Encodingbase64 (Pass. c_str (), buffer); // encode the password with base64
Strcat (buffer, "/R/N ");
Send (sock, buffer, strlen (buffer), 0 );

4: email sending instructions
Sprintf (buffer, "mail from: <% S>/R/N", from. c_str ());
Send (sock, buffer, strlen (buffer), 0 );

5. Description of the target email address
Sprintf (buffer, "rcpt to: <% S>/R/N", to. c_str ());
Send (sock, buffer, strlen (buffer), 0 );
Deal_return_code (getresponsecode (sock, 250, STR ));

6. send an email request
Sprintf (buffer, "Data/R/N ");
Send (sock, buffer, strlen (buffer), 0 );
Deal_return_code (getresponsecode (sock, 354, STR ));

7. Send the mail header and body. The subject part of the sent mail should be formatted before being sent. The format program is not described here.
// Data head
Send (sock, mailhead, strlen (mailhead), 0 );

// Data body
Send (sock, mailbody, strlen (mailbody), 0 );

8. End sending data
Strcpy (buffer, "Quit/R/N ");
Send (sock, buffer, strlen (buffer), 0 );
Deal_return_code (getresponsecode (sock, 221, STR ));

9. Disconnect
Closesocket (sock );

}

Mail header, composed
Date:
From:
To:
Subject:
Message-ID:
X-mailer:
Mime-version:

The following is the encoded data of an email:

Date: Wed, 12 Sep 2007 16:13:56 + 0800
From: <jmst66@163.com>
To: <zzzheng@hit.edu.cn>
Subject: =? Gb2312? B? Sk1tvcc52npat6kx7c7e1ck78b2xtfey6q ==? =
Message-ID: <1189584836@163.com>
X-mailer: =? Gb2312? B? 19s8urxetwfpbgvy? =
Mime-type: 1.0
Content-Type: multipart/alternative;
???????? Boundary = "=== majorsplittag ==="
This is a multi-part message in MIME format.

-- === Majorsplittag ===
Content-Type: text/plain;
???????? Charset = "gb2312"
Content-transfer-encoding: base64

-- === Majorsplittag ===
Content-Type: text/html;
???????? Charset = "gb2312"
Content-transfer-encoding: base64

Pgh0bww + Signature
Bytes
Zd4nc1_kpgjvzhk + dqo8zm9udcbzaxplpsirmsi + dqo8ccbhbglnbj0ibgvmdci + 1/C + tlxe1/fv36os
Bytes/s
Qsrvvk/e + ttaobzktvnuobe3orhtwtvoxlxez + c52ndfz6kjrmbk1tcw/mcou/g9scfpv/ajrm7e1ckx
U9l908o1xltoyv26zbg70v3tw7xev6/o78p7s8ajrlvy1d + 78deowpvj6sfrtctwpmrpoaph68t6vavl
+ Beise3oxnxctctm4ss/o6ziq7k/1/fv36ost6kx7bxeicde6qosvu2jqmbao6mjrznkilyw0 + u45bz +
Z + c52lxeu/g9sdakyum78txf16ja + 9 akyumjrmmow + i687eiy83wwtx1pkpnu1q2nkaxnjmuq29tpc91
Prvy1d + 9q8bkulttobz + tkvv5tbbiko6idaynkotodm5nzkyndihoya8l3a + dqo8ccbhbglnbj0ibgvm
DCI + 1/fv37xeynnt/tkyysfo0sphxtq/r7xeynnt/qost8ezo7jq0lve + rbuztldx7mk1/e1xmxkus + J
Rnkyz6pn + 8t6us3e + rxezazkwsphxnzsu8jnvmjn + bxy1qez1s7sw8ejosa8l3a + dqo8ccbhbglnbj0i
Bgvmdci + 16pe + qo6ilmk1/fls8d7o6zn8srcyofs4qohidxiciavpg0kicc0y9bcidwvcd4ncjxwifs
Awdupsjszwz0ij6 + tmdxidwvcd4ncjxwiggfsawdupsjszwz0ij5ktvnusec8rbk/idwvcd4nc1_vzm9u
Bytes
Ls0tls0tls0gpgjyic8 + dqogiepvdxjuywwgb2ygtwf0zxjpywxzicbty2llbmnliczhbxa7ifrly2hu
Bytes
Bytes
Bytes
Ci5hyy5jbii + sk1tvebpbxiuywmuy248l2e + lca8ysbocmvmpsjtywlsdg86sk1tvdy2qde2my5jb20i
Pkpnu1q2nkaxnjmuy29tpc9hpia8l3a + dqo8ccbhbglnbj0ibgvmdci + v2viia8ysbocmvmpsjodhrw
Oi8vd3d3lkpnu1qut1jhij5xv1cusk1tvc5pukc8l2e + idxiciavpg0kicatls0tls0tls0tls0tls0tls0t
Bytes
-- === Majorsplittag ===

Base64 encoding is actually to convert three 8-bit bytes into four 6-bit bytes ,(? 3*8? =? 4*6? =? 24 ?)? What are the four six bytes? In fact, it is still 8 bits, but the upper two bits are set to 0 .? When a byte is valid for only 6 characters, its value is 0? To? Is the power 6 of 2 reduced by 1? That is, 63, that is, the value space of each encoded base64 to be converted is (0 ~ 63 )?.?
????
???? In fact, 0 ~ There are many invisible characters in the ASCII code between 63, so we should make another ing. The ing table is?
????
???? 'A '?~? 'Z '??? ASCII (0 ?~? 25 )?
????
???? 'A '?~? 'Z '??? ASCII (26 ?~? 51 )?
????
???? '0 '?~? '9 '??? ASCII (52 ?~? 61 )?
????
???? '+ '??? ASCII (62 )?
????
???? '/'??? ASCII (63 )?
????
???? In this way, three 8-bit bytes can be converted to four visible characters .?
????
???? The specific byte splitting method is as follows? :-))?
????
???? Aaaaaabb? Ccccdddd? Eeffffff ??? // Abcdef is actually 1 or 0. Use abcdef instead to make it clear.
????
???? ~~~~~~~~?~~~~~~~~?~~~~~~~~?
????
???? Bytes? 1? Bytes? 2? Bytes? 3?
????
???? |?
???? //?
????
???? 00 aaaaaa? 00 bbcccc? 00 ddddee? 00 ffffff?
????
???? Note: The above three bytes are original. The following four bytes are base64 encoded, and the first two are both 0 .?
????
???? In this case, the number of bytes in the original text should be a multiple of 3. When this condition cannot be met, the full zero byte is used?
????
???? Complete. The base64 encoding is replaced by the = sign during conversion. That is why some base64 encoding ends with one or two equal signs?
????
???? Because F (origin) represents the number of bytes in the original text, F (remain) represents?
????
???? Table remainder, then?
????
???? F (remain )? =? F (origin )? MoD? 3? Yes .?
????
???? So the possible values of F (remain) are 0, 1, 2 .?
????
???? If you set? N? =? [F (origin )? -? F (remain)]? /? 3?
????
???? When F (remain )? =? 0? Is converted to 4 * n Bytes of base64 encoding .?
????
???? When F (remain )? =? 1? Because a Source byte can be split into two base64 encoded bytes?
????
???? So that base64 encoding is a multiple of 4, so we should add two equal signs .?
????
???? When F (remain )? =? 2? Because two original bytes can be split into three base64 encoded bytes, the same is true ,?
????
???? An equal sign should be added.

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.