使用SOCKET发送邮件【源码+程序+使用说明】

来源:互联网 发布:mysql insert into id 编辑:程序博客网 时间:2024/06/01 16:06


主程序:

// smtp.cpp : 定义控制台应用程序的入口点。// #include "stdafx.h"#include <WinSock2.h>#include <stdlib.h>#include <iostream>#include <cstring>#include "ZBase64.h"#pragma comment(lib,"ws2_32.lib")  using namespace std;BOOL SendDataQuit();/************************************************************************//* 《配置信息》/************************************************************************/#define SMTP_SERVER "smtp.qq.com"#define SMTP_PORT                25   /************************************************************************//*《资源回收宏》/************************************************************************/#define RELEASE_SOCKET(s) {if(INVALID_SOCKET!=s){closesocket(s);s=INVALID_SOCKET;}}#define RELEASE_HANDLE(h) {if(NULL!=h){CloseHandle(h);h=NULL;}}   /************************************************************************//*《全局变量》/************************************************************************/SOCKET socket_client;        //        客户端套接字hostent *host;                                        //        SMTP服务器地址信息in_addr in_addr_string;        //        SMTP服务器IP地址sockaddr_in addr;                        //        客户端用来配置与SMTP服务端的连接信息(端口+IP)char buff[1024];                                //        用户接收服务端返回数据的缓冲区char m_name[512];                        //        登陆成功后记录用户邮箱账号        int nErr;                                                        //        记录错误  /************************************************************************//*        《初始化系统资源》/************************************************************************/BOOL InitSystem(){        memset(m_name, 0, sizeof(m_name));        //        初始化网络接口        WSADATA wsadata;        WORD version;        version = MAKEWORD(2, 0);        WSAStartup(version, &wsadata);        //        创建套接字        socket_client = socket(AF_INET, SOCK_STREAM, NULL);        if (INVALID_SOCKET == socket_client)        {                printf("Client->Create Socket Error.\n");                return FALSE;        }        //        获取服务器地址信息        host = gethostbyname(SMTP_SERVER);        memmove(&in_addr_string, host->h_addr_list[0], 4);        //        开始连接SMTP服务器        memset(&addr, 0, sizeof(sockaddr_in));        addr.sin_family = AF_INET;        addr.sin_port = htons(SMTP_PORT);        addr.sin_addr = in_addr_string;        if (SOCKET_ERROR == connect(socket_client, (sockaddr*)&addr, sizeof(sockaddr)))        {                nErr = WSAGetLastError();                printf("Client->Connect Error,Code:%d\n", nErr);                RELEASE_SOCKET(socket_client);                return FALSE;        }        //        接收来自服务器的信息,确认连接成功        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                nErr = WSAGetLastError();                printf("Client->Connect Error,Code:%d\n", nErr);                RELEASE_SOCKET(socket_client);                return FALSE;        }        printf("Server->%s.\n", buff);        return TRUE;}   /************************************************************************//*        《回收系统资源》/************************************************************************/void UnInitSystem(){        SendDataQuit();        //        释放网络接口        WSACleanup();}   /************************************************************************//* 《发送请求》/************************************************************************///        发送HELO信息BOOL SendDataHELO(){        if (socket_client == 0 || socket_client == INVALID_SOCKET)        {                printf("Client->SendData Error,Socket Error.\n");                return FALSE;        }        char* lpszHELO = "HELO smtp\r\n";        if (SOCKET_ERROR == send(socket_client,lpszHELO,strlen(lpszHELO),NULL))        {                printf("Client->Send HELO Error.\n");                return FALSE;        }        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                printf("Client->Recv HELO Error.\n");                return FALSE;        }        printf("Server->%s.\n", buff);        return TRUE;}//        发送登陆信息BOOL SendDataLogin(char* lpszUserName,char* lpszPassword){        string lpszUserNameBuf;        string lpszPasswordBuf;        if (socket_client == 0 || socket_client == INVALID_SOCKET)        {                printf("Client->SendData Error,Socket Error.\n");                return FALSE;        }        char *lpszLogin = "auth login\r\n";        if (SOCKET_ERROR == send(socket_client, lpszLogin,strlen(lpszLogin), NULL))        {                printf("Client->Send Login Error.\n");                return FALSE;        }        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                printf("Client->Recv Login Error.\n");                return FALSE;        }        printf("Server->%s.\n", buff);        //        发送用户名        ZBase64 zBase;        lpszUserNameBuf = zBase.Encode((unsigned char*)lpszUserName, strlen(lpszUserName));        lpszUserNameBuf += "\r\n";        if (SOCKET_ERROR == send(socket_client, lpszUserNameBuf.c_str(), lpszUserNameBuf.length(), NULL))        {                printf("Client->Send UserName Error.\n");                return FALSE;        }        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                printf("Client->Recv UserName Error.\n");                return FALSE;        }        printf("Server->%s.\n", buff);        //        发送用户密码        lpszPasswordBuf = zBase.Encode((unsigned char*)lpszPassword, strlen(lpszPassword));        lpszPasswordBuf += "\r\n";        if (SOCKET_ERROR == send(socket_client, lpszPasswordBuf.c_str(), lpszPasswordBuf.length(), NULL))        {                printf("Client->Send Password Error.\n");                return FALSE;        }        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                printf("Client->Recv Password Error.\n");                return FALSE;        }        lstrcpy(m_name, lpszUserName);        printf("Server->%s.\n", buff);        return TRUE;}  //        发送邮件BOOL SendDataMail(char* lpToName, char* lpSubject,char* lpContent){        char lpszSendBuf[512];        char* lpszDATA = "DATA\r\n";        if (socket_client == 0 || socket_client == INVALID_SOCKET)        {                printf("Client->SendDataMail Error,Socket Error.\n");                return FALSE;        }        wsprintf(lpszSendBuf, "MAIL FROM:<%s>\r\n", m_name);        if (SOCKET_ERROR == send(socket_client, lpszSendBuf, strlen(lpszSendBuf), NULL))        {                printf("Client->Send MAIL FROM Error.\n");                return FALSE;        }        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                printf("Client->Recv MAIL FROM Error.\n");                return FALSE;        }        printf("Server->%s.\n", buff);        wsprintf(lpszSendBuf, "RCPT TO:<%s>\r\n", lpToName);        if (SOCKET_ERROR == send(socket_client, lpszSendBuf, strlen(lpszSendBuf), NULL))        {                printf("Client->Send RCPT TO Error.\n");                return FALSE;        }        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                printf("Client->Recv RCPT TO Error.\n");                return FALSE;        }        printf("Server->%s.\n", buff);        if (SOCKET_ERROR == send(socket_client, lpszDATA, strlen(lpszDATA), NULL))        {                printf("Client->Send DATA Error.\n");                return FALSE;        }        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                printf("Client->Recv DATA Error.\n");                return FALSE;        }        printf("Server->%s.\n", buff);        //        发送邮件        wsprintf(lpszSendBuf, "From:%s\r\nSubject:%s\r\n\r\n%s\r\n\r\n\r\n.\r\n", m_name, lpSubject, lpContent);        if (SOCKET_ERROR == send(socket_client, lpszSendBuf, strlen(lpszSendBuf), NULL))        {                printf("Client->Send MAIL Error.\n");                return FALSE;        }        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                printf("Client->Recv MAIL Error.\n");                return FALSE;        }        printf("Server->%s.\n", buff);        return TRUE;} BOOL SendDataQuit(){        char* lpszQUIT = "QUIT\r\n";        if (socket_client == 0 || socket_client == INVALID_SOCKET)        {                printf("Client->SendDataMail Error,Socket Error.\n");                return FALSE;        }        if (SOCKET_ERROR == send(socket_client, lpszQUIT, strlen(lpszQUIT), NULL))        {                printf("Client->Send QUIT Error.\n");                return FALSE;        }        memset(buff, 0, sizeof(buff));        nErr = recv(socket_client, buff, sizeof(buff), NULL);        if (nErr == 0 || nErr == SOCKET_ERROR)        {                printf("Client->Recv QUIT Error.\n");                return FALSE;        }        printf("Server->%s.\n", buff);} /************************************************************************//*/************************************************************************/int _tmain(int argc, _TCHAR* argv[]){        char lpszUserName[1024];        char lpszPassword[1024];        char lpszName[1024];        char lpszToName[2014];        char lpszSubject[1024];        char lpszContext[1024];        system("color a");        InitSystem();        SendDataHELO();        cout << "UserID:";        cin >> lpszUserName;        cout << "Password:";        cin >> lpszPassword;        if (SendDataLogin(lpszUserName, lpszPassword))        {                cout << "Client->Login Success.\n";        }        while (true)        {                cout << "ToName:";                cin >> lpszToName;                cout << "Subject:";                cin >> lpszSubject;                cout << "Context:";                cin >> lpszContext;                SendDataMail(lpszToName, lpszSubject, lpszContext);        }        UnInitSystem();        return 0;}

以下为BASE64编码转换程序

ZBase64.h

#include <string>using namespace std; class ZBase64{public:    /*编码    DataByte        [in]输入的数据长度,以字节为单位    */    string Encode(unsigned char* Data,int DataByte);    /*解码    DataByte        [in]输入的数据长度,以字节为单位    OutByte        [out]输出的数据长度,以字节为单位,请不要通过返回值计算        输出数据的长度    */    string Decode(const char* Data,int DataByte,int& OutByte);};

ZBase64.cpp

#include "stdAfx.h"#include "ZBase64.h" string ZBase64::Encode(unsigned char* Data,int DataByte){    //编码表    const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";    //返回值    string strEncode;    unsigned char Tmp[4]={0};    int LineLength=0;    for(int i=0;i<(int)(DataByte / 3);i++)    {        Tmp[1] = *Data++;        Tmp[2] = *Data++;        Tmp[3] = *Data++;        strEncode+= EncodeTable[Tmp[1] >> 2];        strEncode+= EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];        strEncode+= EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];        strEncode+= EncodeTable[Tmp[3] & 0x3F];        if(LineLength+=4,LineLength==76) {strEncode+="\r\n";LineLength=0;}    }    //对剩余数据进行编码    int Mod=DataByte % 3;    if(Mod==1)    {        Tmp[1] = *Data++;        strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];        strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4)];        strEncode+= "==";    }    else if(Mod==2)    {        Tmp[1] = *Data++;        Tmp[2] = *Data++;        strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];        strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];        strEncode+= EncodeTable[((Tmp[2] & 0x0F) << 2)];        strEncode+= "=";    }         return strEncode;} string ZBase64::Decode(const char* Data,int DataByte,int& OutByte){    //解码表    const char DecodeTable[] =    {        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,        62, // '+'        0, 0, 0,        63, // '/'        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'        0, 0, 0, 0, 0, 0, 0,        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,        13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'        0, 0, 0, 0, 0, 0,        26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,        39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'    };    //返回值    string strDecode;    int nValue;    int i= 0;    while (i < DataByte)    {        if (*Data != '\r' && *Data!='\n')        {            nValue = DecodeTable[*Data++] << 18;            nValue += DecodeTable[*Data++] << 12;            strDecode+=(nValue & 0x00FF0000) >> 16;            OutByte++;            if (*Data != '=')            {                nValue += DecodeTable[*Data++] << 6;                strDecode+=(nValue & 0x0000FF00) >> 8;                OutByte++;                if (*Data != '=')                {                    nValue += DecodeTable[*Data++];                    strDecode+=nValue & 0x000000FF;                    OutByte++;                }            }            i += 4;        }        else// 回车换行,跳过        {            Data++;            i++;        }     }    return strDecode;}

下载地址:http://www.ghostasm.com/forum.php?mod=viewthread&tid=1261&extra=page%3D1

0 0