新闻动态
    技术中心
    技术中心
    当前位置:ag8亚游集团自控 >> 服务支持 >> 技术中心 >> 浏览文章
    C语言发送邮件
    作者:研发部 小薛 日期:2018年11月26日 来源:本站原创 浏览:

    内容导读:    Linux下使用c语言发送邮件 ,需要将服务器上的df -hl的执行结果定时发给用户。    尝试使用sendmail来发邮件,但是后来放弃了,并不是所有的服务器上都安装了sendmail。    于是,就用c写一个吧,还能够指定邮件服务器

        Linux下使用c语言发送邮件 ,需要将服务器上的df -hl的执行结果定时发给用户。
        尝试使用sendmail来发邮件,但是后来放弃了,并不是所有的服务器上都安装了sendmail。
        于是,就用c写一个吧,还能够指定邮件服务器地址和端口,会更灵活些。
        目标是创建程序qmail,然后通过命令qmail my@163.com your.txt将your.txt的内容发送给my@163.com
    #include <stdio.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <stdlib.h>
    #include <string.h>

    intmain(intargc,char*argv[])
    {
    intsockfd =-1;
    inticonn =-2;
    ssize_t retConnect =-2;
    structsockaddr_in servaddr;
    charip[20] ="192.168.0.251";//设置SMTP地址
    charsentmsg[2048] ="";
    charbuf[255] ="";
    charfileContent[1024] ="";
    FILE*file;
    charcin[255] ="";

    intlen =0;
    while(!(argv[len]==NULL))
    {
    len++;
    }

    if(len!=3)
    {
    printf("Usage:qmail yourname@163.com df.log\n");
    exit(0);
    }
    memset(fileContent,'\0',sizeof(fileContent));
    if((file =fopen(argv[2], "r")) ==NULL)
    printf("Not find file");
    else
    {
    while(fgets(cin, sizeof(cin), file) !=NULL)
    {
    strcat(fileContent,cin);
    memset(cin,'\0',sizeof(cin));
    }
    }
    fclose(file);
    printf("%s",fileContent);
    setvbuf(stdout,NULL,_IONBF,0);
    sockfd =socket(AF_INET,SOCK_STREAM,0);//以scoket方式和邮件服务器通讯
    if(sockfd>0)
    {
    printf("socket is open");
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_port=htons(25);
    inet_pton(AF_INET,ip,&servaddr.sin_addr);
    iconn =connect(sockfd,(structsockaddr *)&servaddr,sizeof(servaddr));
    if(iconn==0)
    {
    printf("connect to 192.168.0.251 25 success!");
    retConnect =recv(sockfd,buf,sizeof(buf),0);
    if(retConnect==-1)
    {
    printf("Failed to receive msg from smtp port");
    }
    else
    printf("\nServer:%s\n",buf);
    memset(sentmsg,'\0',sizeof(sentmsg));
    strcpy(sentmsg,"HELO SERVER\r\n");
    retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
    if(retConnect==-1)
    {
    printf("\nFailed to send meg to smtp port in step 2.\n");
    exit(1);
    }
    else
    {
    printf("%s",sentmsg);
    }
    memset(buf,'\0',sizeof(buf));
    retConnect=recv(sockfd,buf,sizeof(buf),0);
    if(retConnect ==-1)
    {
    printf("\nFailed to recive meg from smtp port in step 3.\n");
    exit(1);
    }
    else
    {
    printf("%s\n",buf);
    }

    memset(sentmsg,'\0',sizeof(sentmsg));
    strcpy(sentmsg,"MAIL FROM: qdcm@163.com\r\n");
    strcat(sentmsg,"RCPT TO: ");
    strcat(sentmsg,argv[1]);
    strcat(sentmsg,"\r\n");
    retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
    if(retConnect>0)
    printf("%s",sentmsg);
    memset(buf,'\0',sizeof(buf));
    retConnect=recv(sockfd,buf,sizeof(buf),0);
    if(retConnect>0)
    printf("%s\n",buf);

    memset(sentmsg,'\0',sizeof(sentmsg));
    strcpy(sentmsg,"DATA\r\n");
    retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
    if(retConnect>0)
    printf("%s",sentmsg);
    memset(buf,'\0',sizeof(buf));
    retConnect=recv(sockfd,buf,sizeof(buf),0);
    if(retConnect>0)
    printf("%s\n",buf);

    memset(sentmsg,'\0',sizeof(sentmsg));
    strcpy(sentmsg,"From:qdcm@163.com\r\n");
    strcat(sentmsg,"To:");
    strcat(sentmsg,argv[1]);
    strcat(sentmsg,"\r\n");
    strcat(sentmsg,"Subject:QDCM Host Check Data\r\n\r\n");
    strcat(sentmsg,fileContent);
    strcat(sentmsg,"\r\n");
    strcat(sentmsg,"\r\n.\r\n");
    retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
    memset(sentmsg,'\0',sizeof(sentmsg));

    strcpy(sentmsg,"QUIT\r\n");
    retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
    if(retConnect>0)
    printf("%s",sentmsg);
    memset(buf,'\0',sizeof(buf));
    retConnect=recv(sockfd,buf,sizeof(buf),0);
    if(retConnect>0)
    printf("%s\n",buf);

    }
    else
    {
    printf("connect 192.168.0.251 25 failed!");
    sleep(1);
    }
    close(sockfd);
    }
    else
    {
    printf("open socket failed!");
    }
    return0;
    }

     
    使用gcc编译
    如果你是在高版本的linux下编译的,在低版本上的linux使用时会报错
    此时需要在编译时追加参数:
    gcc -Wl,--hash-style=sysv qmail.c -o qmail

    上一篇文章:不再担心黑客入侵 美信ChipDNA技术为每片IC生成的密钥 下一篇文章:没有了
    相关链接
    发表评论
    用户评论
    版权所有 山西ag8亚游集团自控股份有限公司 晋ICP备09004627号    晋公网安备 14019202000008号     
    官方微信
    新浪官方微博
    腾讯官方微博