编程环境:WinXP SP2,Visual Studio 2005 Pro

参考资料:《HowTo.SMTP》,《SendMail》(NextFly写的),《MSDN 2005》

好了,废话就不说了。下面是源代码:

// 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;
}

其中需要注意的是寄件人地址与邮件内容里的邮件地址的关系:

寄件人的地址必须和你用的连接帐号一致。帐号和密码是使用Base64编码的(关于什么是Base64编码请到http://www.ynutx.net/blog/user1/Myth/archives/2006/1682.html下面看看,它的C实现代码在下面)。。。。

而在邮件内容里的地址只是一个告诉邮件服务器这封信是谁发的。可以和你所使用的发送帐号不一致!也就是说你可以伪造别人的地址进行邮件发送。。。。

哎,不要干什么干事哟^_^!

好了,下面就是Base64编码与解码的实现代码了:

#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;
}

呵呵,终于完了。有兴趣的可以试试哦,发不了的话加我QQ:845765,或者发邮件给我:DL88250@gmail.com 大家一起研究学习一下!