// SendMail.cpp : 定义控制台应用程序的入口点。
//
#i nclude "stdafx.h"
#i nclude <stdio.h>
#i nclude <stdlib.h>
#i nclude <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
const char *MailData = "From: \"88250\"<dl88250@126.com>\r\n"
"Subject: This is only a test mail! ^^\r\n.\r\n";
int main(int argc, char *argv[])
{
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
struct hostent *pHostent = NULL;
SOCKET server = INVALID_SOCKET;
struct sockaddr_in service;
int retConnect = 0;
char Buffer[1024] = {0};
if(WSAStartup(wVersionRequested, &wsaData) != 0){
printf("Error at WSAStartup()\n");
goto WSACleanup;
}
server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create a Socket
if(server == INVALID_SOCKET){
printf("Error at socket(): %ld\n", WSAGetLastError());
goto WSACleanup;
}
pHostent = gethostbyname("smtp.126.com"); //Get the Mail Server Name
if(pHostent == NULL){
printf("The Host Name is Invalid...\n");
goto WSACleanup;
}
service.sin_family = AF_INET;
memcpy(&service.sin_addr.s_addr, pHostent->h_addr_list[0], pHostent->h_length);
service.sin_port = htons(25);
//Connect to the remote Mail Server
retConnect = connect(server, (struct sockaddr*)&service, sizeof(service));
if(retConnect == SOCKET_ERROR){
printf("Failed to connect.\n");
goto WSACleanup;
}
printf("Connect to %s....\n", inet_ntoa(service.sin_addr));
//Receive Data From the Mail Server
ZeroMemory(Buffer, sizeof(Buffer));
retConnect = recv(server, Buffer, sizeof(Buffer), 0);
if(retConnect == SOCKET_ERROR){
printf("Failed to connect.\n");
goto WSACleanup;
}else{
printf("%s\n", Buffer);
}
//Send "HELO Server....\r\n" to the Mail Server
retConnect = send(server, "HELO Server....\r\n", strlen("HELO Server....\r\n"), 0);
if(retConnect == SOCKET_ERROR){
printf("Send HELO to the Mail Failure.\n");
goto WSACleanup;
}else{
printf("HELO Server....\n");
}
//Receive Data From the Mail Server
ZeroMemory(Buffer, sizeof(Buffer));
retConnect = recv(server, Buffer, sizeof(Buffer), 0);
if(retConnect == SOCKET_ERROR){
printf("Failed to connect.\n");
goto WSACleanup;
}else{
printf("%s\n", Buffer);
}
//Send "AUTH LOGIN\r\n" to the Mail Server
retConnect = send(server, "AUTH LOGIN\r\n", strlen("AUTH LOGIN\r\n"), 0);
if(retConnect == SOCKET_ERROR){
printf("Send \"AUTH LOGIN\" to Mail Failure.\n");
goto WSACleanup;
}else{
printf("AUTH LOGIN\n");
}
//Receive Data From the Mail Server
ZeroMemory(Buffer, sizeof(Buffer));
retConnect = recv(server, Buffer, sizeof(Buffer), 0);
if(retConnect == SOCKET_ERROR){
printf("Receive Data From Mail Server Failure.\n");
goto WSACleanup;
}else{
printf("%s\n", Buffer);
}
//Send UserName to the Mail Server. The UserName is Encoded by Base64.
retConnect = send(server, "bGJleW9uZDRrb21h==\r\n", strlen("bGJleW9uZDRrb21h==\r\n"), 0);
if(retConnect == SOCKET_ERROR){
printf("Send UserName to the Mail Failure.\n");
goto WSACleanup;
}else{
printf("UserName\n");
}
//Receive Data From the Mail Server
ZeroMemory(Buffer, sizeof(Buffer));
retConnect = recv(server, Buffer, sizeof(Buffer), 0);
if(retConnect == SOCKET_ERROR){
printf("Receive Data From the Mail Server Failure.\n");
goto WSACleanup;
}else{
printf("%s\n", Buffer);
}
//Send Password to the Mail Server The Password is Encoded by Base64.
retConnect = send(server, "bGJleW9uZDRrb21h=\r\n", strlen("bGJleW9uZDRrb21h=\r\n"), 0);
if(retConnect == SOCKET_ERROR){
printf("Send Password to Mail Failure.\n");
goto WSACleanup;
}else{
printf("Password\n");
}
//Receive Data From the Mail Server
ZeroMemory(Buffer, sizeof(Buffer));
retConnect = recv(server, Buffer, sizeof(Buffer), 0);
if(retConnect == SOCKET_ERROR){
printf("Receive Data From the Mail Server Failure.\n");
goto WSACleanup;
}else{
printf("%s\n", Buffer);
}
//Send "Mail From: " File to the Mail Server, sender's Mail Address
retConnect = send(server, "MAIL FROM: <lbeyond4koma@126.com>\r\n", strlen("MAIL FROM: <lbeyond4koma@126.com>\r\n"), 0);
if(retConnect == SOCKET_ERROR){
printf("Send \"Mail From: \" to Mail Failure.\n");
goto WSACleanup;
}else{
printf("MAIL FROM: <lbeyond4koma@126.com>\n");
}
//Receive Data From the Mail Server
ZeroMemory(Buffer, sizeof(Buffer));
retConnect = recv(server, Buffer, sizeof(Buffer), 0);
if(retConnect == SOCKET_ERROR){
printf("Receive Data From Mail Server Failure.\n");
goto WSACleanup;
}else{
Buffer[retConnect] = ' ';
printf("%s\n", Buffer);
}
//Send "RCPT TO: " File to the Mail Server, receiver 's Mail Address
retConnect = send(server, "RCPT TO: <dl88250@gmail.com>\r\n", strlen("RCPT TO: <dl88250@gmail.com>\r\n"), 0);
if(retConnect == SOCKET_ERROR){
printf("Send \"RCPT TO: \" to Mail Failure.\n");
goto WSACleanup;
}else{
printf("RCPT TO: <dl88250@gmail.com>\n");
}
//Receive Data From the Mail Server
ZeroMemory(Buffer, sizeof(Buffer));
retConnect = recv(server, Buffer, sizeof(Buffer), 0);
if(retConnect == SOCKET_ERROR){
printf("Receive Data From the Mail Server Failure.\n");
goto WSACleanup;
}else{
printf("%s\n", Buffer);
}
//Send "Data" Fiele to the Mail Server, start to Send mail
retConnect = send(server, "Data\r\n", strlen("Data\r\n"), 0);
if(retConnect == SOCKET_ERROR){
printf("Send \"Data\" Field to Mail Failure...\n");
goto WSACleanup;
}else{
printf("Data\n");
}
//Receive Data From the Mail Server
ZeroMemory(Buffer, sizeof(Buffer));
retConnect = recv(server, Buffer, sizeof(Buffer), 0);
if(retConnect == SOCKET_ERROR){
printf("Receive Data From Mail Server Failure...\n");
goto WSACleanup;
}else{
printf("%s\n", Buffer);
}
//Send Mail data to the the Mail Server
retConnect = send(server, MailData, strlen(MailData), 0);
if(retConnect == SOCKET_ERROR){
printf("Send Context Of Mail to Mail Failure...\n");
goto WSACleanup;
}else{
printf("%s\n", MailData);
}
//Receive Data From the Mail Server
ZeroMemory(Buffer, sizeof(Buffer));
retConnect = recv(server, Buffer, sizeof(Buffer), 0);
if(retConnect == SOCKET_ERROR){
printf("Receive Data From Mail Server Failure...\n");
goto WSACleanup;
}else{
printf("%s\n", Buffer);
}
//Send "QUIT" Context to the Mail Server
retConnect = send(server, "QUIT\r\n", strlen("QUIT\r\n"), 0);
if(retConnect == SOCKET_ERROR){
printf("Send \"Quit\" to Mail Failure...\n");
goto WSACleanup;
}else{
printf("Quit\n");
}
printf("Send Mail Successful!\n");
WSACleanup:{
if(server != INVALID_SOCKET){
closesocket(server);
}
WSACleanup();
}
system("pause");
return 0;
}
#i nclude <stdio.h>
#i nclude <string.h>
char *ch64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned char *encode(unsigned char *src, int srclen)
{
int n, buflen, i, j;
int pading = 0;
unsigned char *buf;
static unsigned char *dst;
buf = src;
buflen = n = srclen;
if(n % 3 != 0){ /* pad with '=' by using a temp buffer */
pading = 1;
buflen = n + 3 - n % 3;
buf = malloc(buflen + 1);
memset(buf, 0, buflen + 1);
memcpy(buf, src, n);
for(i=0;i<3-n%3;i++){
buf[n+i] = '=';
}
}
dst = malloc(buflen * 4 / 3 + 1);
memset(dst, 0, buflen * 4 / 3 + 1);
for(i = 0, j = 0; i < buflen; i += 3, j += 4){
dst[j] = (buf[i] & 0xFC) >> 2;
dst[j+1] = ((buf[i] & 0x03) << 4) + ((buf[i+1] & 0xF0) >> 4);
dst[j+2] = ((buf[i+1] & 0x0F) << 2) + ((buf[i+2] & 0xC0) >> 6);
dst[j+3] = buf[i+2] & 0x3F;
}
for(i = 0; i < buflen * 4 / 3; i++){ /* map 6 bit value to base64 ASCII character */
dst[i] = ch64[dst[i]];
}
if(pading){
free(buf);
}
return dst;
}
unsigned char *decode(unsigned char *src)
{
int n, i, j;
unsigned char *p;
static unsigned char *dst;
n = strlen(src);
for(i=0;i<n;i++){ /* map base64 ASCII character to 6 bit value */
p = strchr(ch64, src[i]);
if(!p){
break;
}
src[i] = p - ch64;
}
dst = malloc(n * 3 / 4 + 1);
memset(dst, 0, n * 3 / 4 + 1);
for(i = 0, j = 0; i < n; i += 4, j += 3){
dst[j] = (src[i] << 2) + ((src[i+1] & 0x30) >> 4);
dst[j+1] = ((src[i+1] & 0x0F) << 4) + ((src[i+2] & 0x3C) >> 2);
dst[j+2] = ((src[i+2] & 0x03) << 6) + src[i+3];
}
return dst;
}
int main()
{
char *src = "lbeyond4koma";
unsigned char *dst1;
unsigned char *dst2;
unsigned int i;
dst1 = encode(src, strlen(src)); /* the second parameter must accord with the first one */
printf("%s\n", dst1);
dst2 = decode(dst1);
for(i = 0; i < _msize(dst2); i++){
printf("%c",dst2[i]);
}
free(dst1);
free(dst2);
return 0;
}