VC实现邮件发送

typedef struct mail_tag{
char mail_addr[255];
char mail_from[255];
char mail_to[255];
char username[20];
char password[20];
char subject[255];
char content[1024];
} MAIL;

int sendmail (MAIL *pmail)
{
    char *pmail_addr=pmail->mail_addr;
    char *pmail_from=pmail->mail_from;
    char *pmail_to=pmail->mail_to;
    char *pusername=pmail->username;
    char *ppassword=pmail->password;    
    char pdata[1048]={0};
    strcpy(pdata,"from:");
    strcat(pdata,pmail->mail_from);
    strcat(pdata,"/r/n");
    strcat(pdata,"to:");
    strcat(pdata,pmail->mail_to);
    strcat(pdata,"/r/n");
    strcat(pdata,"subject:");
    strcat(pdata,pmail->subject);
    strcat(pdata,"/r/n/r/n");
    strcat(pdata,pmail->content);
    strcat(pdata,"/r/n");
    //构造邮件数据
    Base64 bs;
    WSADATA data;
    unsigned short version;
    version=0x0002; //version 2.0版本
    ::WSAStartup(version,&data);
    sockaddr_in mail_addr={0};
    hostent *phost=::gethostbyname(pmail_addr);
    mail_addr.sin_addr.S_un.S_addr=*(unsigned long *)phost->h_addr_list[0];
    mail_addr.sin_family=AF_INET;
    mail_addr.sin_port=htons(25);
    SOCKET s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    int ret=connect(s,(sockaddr*)&mail_addr,sizeof(mail_addr));
    if(ret<0)
    {
        quit(s);
        return 1;
    }
    char buffer[255]={0};
    recv(s,buffer,255,0);
    memset(buffer,0,255);
    //开始登陆smtp服务器
    ret=send(s,"HELO LLIKZ/r/n",12,0);
    //发送HELO
    if(ret<=0)
    {
        quit(s);
        return 1;
    }
    ret=recv(s,buffer,255,0);
    if(ret<=0)
    {
        quit(s);
        return 1;
    }
    memset(buffer,0,255);
    //发送身份认证请求
    send( s,"AUTH LOGIN/r/n",12,0);
    recv(s,buffer,255,0);
    if(buffer[0]!='3'&&buffer[1]!='3')
    {
        quit(s);
        return 1;
    }
    memset(buffer,0,255);
    strcpy(buffer,bs.base64_encode( pusername).c_str());
    strcat( buffer,"/r/n");
    //发送BASE64编码的用户名
    send(s,buffer,strlen(buffer),0);
    memset(buffer,0,255);
    recv(s,buffer,255,0);
    if(buffer[0]!='3')
    {
        quit(s);
        return 1;
    }
    memset(buffer,0,255);
    //发送BASE64编码的密码
    strcpy(buffer,bs.base64_encode(ppassword).c_str());
    strcat(buffer,"/r/n");
    send(s,buffer,strlen(buffer),0);
    memset(buffer,0,255);
    recv(s,buffer,255,0);
    if(buffer[0]!='2'&&buffer[1]!='3')
    {
        quit(s);
        return 1;
    }
    //认证完毕,开始发送邮件数据
    memset(buffer,0,255);
    strcpy(buffer,"MAIL FROM:<");
    strcat(buffer,pmail_from);
    strcat(buffer,">");
    strcat(buffer,"/r/n");
    //发送mail from命令
    send(s,buffer,strlen(buffer),0);
    memset(buffer,0,255);
    recv(s,buffer,255,0);
    if(buffer[0]!='2'&&buffer[1]!='5')
    {
        quit(s);
        return 1;
    }
    memset(buffer,0,255);
    strcpy(buffer,"RCPT TO:<");
    strcat(buffer,pmail_to);
    strcat(buffer,">");
    strcat(buffer,"/r/n");
    //发送rcpt to指令
    send(s,buffer,strlen(buffer),0);
    memset(buffer,0,255);
    recv(s,buffer,255,0);
    if(buffer[0]!='2'&&buffer[1]!='5')
    {
        quit(s);
        return 1;
    }
    memset(buffer,0,255);
    send(s,"DATA/r/n",6,0);
    recv(s,buffer,255,0);
    if(buffer[0]!='3'&&buffer[1]!='5')
    {
        quit(s);
        return 1;
    }
    strcpy(buffer,pdata);
    strcat(buffer,"/r/n./r/n");
    //开始发送邮件数据
    send(s,buffer,strlen(buffer),0);
    memset(buffer,0,255);
    recv(s,buffer,255,0);
    if(buffer[0]!='2')
    {
        quit(s);
        return 1;
    }
    memset(buffer,0,255);
    strcpy(buffer,"QUIT/r/n");
    //退出登陆
    send(s,buffer,strlen(buffer),0);
    closesocket(s);
    ::WSACleanup();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值