c语言写的发送邮件

突然对用c实现发送邮件有了兴趣,抽时间写了个小程序,亲测可以实现用新浪邮箱发送邮件,若用qq邮箱发送邮件的话密码哪里需要填的是qq邮箱的授权码,在设置–>账户下面查看。

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include <assert.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>

static int ZJF_Mail_int2char_FUN(int a)
{
    if(a <= 25)
    {
        return 'A'+(a-0);
    }
    else if((a > 25)&&(a <= 51))
    {
        return 'a'+(a-26);
    }
    else if((a > 51)&&(a < 61))
    {
        return '0'+(a-52);
    }
    else if(a == '=')
    {
        return a;
    }
    else if(a == 62)
    {
        return '+';
    }
    else if(a == 63)
    {
        return '/';
    }
    return 0;
}

static int ZJF_Mail_LittleToBig_FUN(char *str1,int len)
{
    int i    = 0;
    int temp = 0;
    for(i = 0; i < len; i++)
    {
        temp |= (str1[i] << (len-i-1)*8);        
    }
    //printf("%#x \n",temp); 
    return temp;
}

static int ZJF_Mail_Base64Encod_FUN(char * data,char *output,int len,int *oputlen)
{

    int i,j,k;
    char strtemp[4] = { 0 };  /*每次拷贝三个字节出来,存储*/
    int temp = 0;
    char * pbuffer = (char * )malloc(len/3*4+4);

    for(i = 0,j = 0; i < len/3; i++)
    {            
        memcpy(strtemp,data+3*i,3);    
        temp = ZJF_Mail_LittleToBig_FUN(strtemp,3);

        pbuffer[j++] = (temp>>18)&0x3f;   //0x3f  111111
        pbuffer[j++] = (temp>>12)&0x3f;  
        pbuffer[j++] = (temp>>6)&0x3f;   
        pbuffer[j++] = (temp>>0)&0x3f;   

        if(j == 75)
        {
            pbuffer[j++] = '\n';
        }
    }

    if(len%3 == 1)
    {
        memcpy(strtemp,data+3*i,1);
        temp = (int)strtemp[0];     
        pbuffer[j++] = temp>>2;
        pbuffer[j++] = (temp&0x03)<<4;
        pbuffer[j++] = '=';
        pbuffer[j++] = '=';
    }
    else if(len%3 == 2)
    {
        memcpy(strtemp,data+3*i,2);
        temp = ZJF_Mail_LittleToBig_FUN(strtemp,2);
        pbuffer[j++] = temp>>10;
        pbuffer[j++] = (temp>>4)&0x3f;
        pbuffer[j++] = (temp<<2)&0x3f;
        pbuffer[j++] = '=';
    }
    pbuffer[j++] = '\0';

    for(i = 0;i < j-1;i++)
    {
        //printf("%c ",ZJF_Mail_int2char_FUN(pbuffer[i]));
        output[i]= ZJF_Mail_int2char_FUN(pbuffer[i]);
    }
    printf("\n");
    *oputlen = j-1; 
    //printf("data len = %d \n",j-1);
    if(pbuffer != NULL)
    {
        free(pbuffer);
        pbuffer = NULL;
    }

    return 0;

}


#define IP_ADDR      ("smtp.sina.com")
static ZJF_Mail_GetPeerAddrInfo_FUN(struct sockaddr_in *PeerAddr)
{
    struct hostent *Addr;
    char ip[32] = { 0 };

    Addr = gethostbyname(IP_ADDR);  
    if(NULL == Addr)
    {
        perror("err:");
        printf("get ip info err !!! \n");
        return -1;
    }

    memset(PeerAddr, 0, sizeof(struct sockaddr_in));  
    PeerAddr->sin_family = AF_INET;
    PeerAddr->sin_port   = htons(25);

    inet_ntop(Addr->h_addrtype, Addr->h_addr_list[0], ip, sizeof(ip));
    PeerAddr->sin_addr.s_addr = inet_addr(ip);
    printf("ip:%s len: %d \n",ip,strlen(ip));

    return 0;
}


static int ZJF_Mail_OpenSocket_FUN(struct sockaddr *addr)
{
    int sockfd = 0;
    sockfd=socket(PF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
    {
        printf("socket failed !!! \n");
        return -1;
    }
    if(connect(sockfd, addr, sizeof(struct sockaddr)) < 0)
    {
        printf("connect failed !!! \n");
        return -1;
    }
    return sockfd;
}

static int ZJF_Send_FUN()
{

}



/*HELO */
static int ZJF_Mail_HELO_FUN(int socket)
{
    char buf[320] = { 0 };
    int ret       = 0;

    sprintf(buf,"%s","EHLO ZJF \r\n");
    send(socket, buf, strlen(buf), 0);
    ret = recv(socket, buf, 320, 0);
    printf("注册返回消息:len = %d  rcv:%s \n",ret,buf);
    if(ret <= 0)
    {        
        return -1;
    }

    return 0;
}


/*AUTH LOGIN 身份认证*/
static int ZJF_Mail_AuthLogin_FUN(int socket)
{
    char buf[320] = { 0 };
    int ret       = 0;

    sprintf(buf,"%s","AUTH LOGIN\r\n");
    send(socket, buf, strlen(buf), 0);
    ret = recv(socket, buf, 320, 0);
    if(ret <= 0)
    {       
        return -1;
    }
    printf("身份认证返回消息:len = %d rcv:%s \n",ret,buf);
    return 0;
}


/*发送账号*/
static int ZJF_Mail_User_FUN(int socket)
{
    char buf[320]    = { 0 };
    char outbuf[120] = { 0 };
    int ret          = 0;
    int len          = 0;

    sprintf(buf,"%s","zjf535214685");
    ZJF_Mail_Base64Encod_FUN(buf,outbuf,strlen(buf),&len);
    sprintf(buf,"%s\r\n",outbuf);
    send(socket, buf, strlen(buf), 0);
    ret = recv(socket, buf, 320, 0);

    if(ret <= 0)
    {       
        return -1;
    }
    printf("发送账号返回消息:len = %d rcv:%s \n",ret,buf);
    return 0;
}


/*发送密码*/
static int ZJF_Mail_Password_FUN(int socket)
{
    char buf[320]    = { 0 };
    char outbuf[120] = { 0 };
    int ret          = 0;
    int len          = 0;

    sprintf(buf,"%s","zjf19911018");
    ZJF_Mail_Base64Encod_FUN(buf,outbuf,strlen(buf),&len);
    sprintf(buf,"%s\r\n",outbuf);
    send(socket, buf, strlen(buf), 0);
    ret = recv(socket, buf, 320, 0);
    if(ret <= 0)
    {       
        return -1;
    }
    printf("发送密码返回消息:len = %d rcv:%s \n",ret,buf);

    return 0;
}


/*发送发件人*/
static int ZJF_Mail_From_FUN(int socket)
{
    char buf[320] = { 0 };
    int ret       = 0;

    sprintf(buf,"%s","MAIL FROM: <zjf535214685@sina.com>\r\n");
    send(socket, buf, strlen(buf), 0);
    ret = recv(socket, buf, 320, 0);
    if(ret <= 0)
    {       
        return -1;
    }
    printf("发送收件人返回消息:len = %d rcv:%s \n",ret,buf);
    return 0;
}



/*发送收件人*/
static int ZJF_Mail_RcptTo_FUN(int socket)
{    
    char buf[320] = { 0 };
    int ret       = 0;

    sprintf(buf,"%s","RCPT TO:<2853109004@qq.com>\r\n");
    send(socket, buf, strlen(buf), 0);
    ret = recv(socket, buf, 320, 0);
    if(ret <= 0)
    {       
        return -1;
    }
    printf("发送收件人返回消息:len = %d rcv:%s \n",ret,buf);
    return 0;
}


/*发送准备开始发送邮件*/
static int ZJF_Mail_DataReady_FUN(int socket)
{
    char buf[320] = { 0 };
    int ret       = 0;

    sprintf(buf,"%s","DATA\r\n");
    send(socket, buf, strlen(buf), 0);
    ret = recv(socket, buf, 320, 0);
    if(ret <= 0)
    {       
        return -1;
    }
    printf("发送准备开始发送消息返回消息:len = %d rcv:%s \n",ret,buf);
    return 0;
}



/*发送邮件*/
static int ZJF_Mail_DataSend_FUN(int socket)
{
    char Email[] = "From: \"zjf\"<zjf535214685@sina.com>\r\n" \
                           "To: \"dasiy\"<2853109004@qq.com>\r\n" \
                           "Subject: Dear pig!\r\n\r\n" \
                           "Dear pig, if you see this email, that I have succeeded!";

    char buf[1200] = { 0 };
    int ret        = 0;

    sprintf(buf,"%s\r\n.\r\n",Email);
    send(socket, buf, strlen(buf), 0);
    ret = recv(socket, buf, 1200, 0);
    if(ret <= 0)
    {       
        return -1;
    }
    printf("发送消息体返回消息:len = %d rcv:%s \n",ret,buf);
    return 0;
}

/*断开连接QUIT*/
static int ZJF_Mail_QUIT_FUN(int socket)
{
    char buf[320] = { 0 };
    int ret       = 0;

    sprintf(buf,"%s","QUIT\r\n");
    send(socket, buf, strlen(buf), 0);
    ret = recv(socket, buf, 320, 0);
    if(ret <= 0)
    {       
        return -1;
    }
    printf("发送结束消息返回消息:len = %d rcv:%s \n",ret,buf);
    return 0;
}

#define MAX_BUFF_SIZE  (2048)
int main()
{
    struct sockaddr_in PeerAddr;
    int socket               = -1;
    char rbuf[MAX_BUFF_SIZE] = { 0 };
    int  ret                 = 0;

    if(ZJF_Mail_GetPeerAddrInfo_FUN(&PeerAddr) < 0)
    {
        return -1;
    }

    for( ; ; )
    {
        socket = ZJF_Mail_OpenSocket_FUN((struct sockaddr *)&PeerAddr);
        if(socket < 0)
        {
            sleep(5);
            continue;
        }

        while(1)
        {
            ret = recv(socket,rbuf,MAX_BUFF_SIZE,0);
            if(ret > 0)
            {
                printf("Revc: %s \n",rbuf);
                break;
            }
            sleep(2);
            continue;
        }

        if(ZJF_Mail_HELO_FUN(socket) < 0)
        {
            continue;
        }
        if(ZJF_Mail_AuthLogin_FUN(socket) < 0)
        {
            continue;
        }
        if(ZJF_Mail_User_FUN(socket) < 0)
        {
            continue;
        }

        if(ZJF_Mail_Password_FUN(socket) < 0)
        {
            continue;
        }
        if(ZJF_Mail_From_FUN(socket) < 0)
        {
            continue;
        }
        if(ZJF_Mail_RcptTo_FUN(socket) < 0)
        {
            continue;
        }

        if(ZJF_Mail_DataReady_FUN(socket) < 0)
        {
            continue;
        }
        if(ZJF_Mail_DataSend_FUN(socket) < 0)
        {
            continue;
        }
        if(ZJF_Mail_QUIT_FUN(socket) < 0)
        {
            continue;
        }       

        close(socket);
        break;
    }

    return 0;
}




  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
主要功能: 1、可以发送带附件的邮件,附件可以是多个,附件大小限制由发送方服务器而定,暂未测试具体为多少MB 2、邮件内容和主题可以是空,但当有附件时,主题取第一个附件的文件名(不含扩展名) 3、密码验证均为base64加密 4、邮件正文和附件的数据传送方式,均为base64 5、自动解析发件箱的SMTP服务器 压缩包文件简介: base.c:包含一些基本的函数,其中有一些在此程序中并未用到,只要使用了其中的base64加密算法 mail.c:包含邮件发送、数据读取、编码转换、smtp服务器连接、ip解析等函数 mailsend.c:包含main的c源文件,mail.exe则是根据mailsend.c、mail.c、base.c编译成的,具体编译方 法可参考makefile libbase.a:make之后生成的静态库 moontalk.cfg:base.c用到的配置文件,可能没用,放在这里进攻阅读参考 mail.cfg:自定义用户的配置文件,可用可不用,用作读代码的参考 mail.exe:邮件发送的执行文件,仅有命令行模式完善了,逐步输入(直接双击)的方式还不完善 b64.exe:base64加密解密的小工具,仅供参考,mail.cfg中用到密码的地方,可以使这个工具得到。 makefile:工程编译链接文件 注意:在本地使用mingw环境开发,遵循ANSI C标准,本地有系统的工程库,但是上传的时候,把这些文件 都放在一起了,可以先参考makefile进行工程调整,如果有任何问题,请发送到邮箱[email protected], 技术交流,不胜感激。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿木小呆呆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值