• Linux C实现发邮件功能


    此功能在嵌入式开发中常用来通知用户。 比如监控异常了,发邮件通知。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <sys/fcntl.h>
    #include <sys/stat.h>
     
    #define SOCKET_ERROR -1
    #define IPSTR "220.181.12.12"
    #define PRINT(x) printf("%s
    ",(x))
     
    int sockfd;
    int ret;
    char *send_data;
    char *recv_data;
    char From[128];
    char To[128];
    char Date[128];
    char Subject[512];
    char Context[6000]={0};
    char buffer[1024];
    
    int connectHost(const char *smtpaddr)
    {
        struct sockaddr_in servaddr;
        sockfd = socket(AF_INET,SOCK_STREAM,0);
        if(sockfd < 0)
        {
            printf("Create socket error!
    ");
            return -1;
        }
        bzero(&servaddr,sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(25);
        
        if(inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ) 
        {
            printf("inet_pton error!
    ");
            return -1;
        };
    
        if (connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)    
        {
            printf("Connect failed... 
    ");
            return -1;
        }
        printf("Connect to %s.... 
    ", IPSTR);
         
        memset(buffer, 0, sizeof(buffer));
         
        if(recv(sockfd, buffer, sizeof(buffer), 0) < 0)
        {    
            printf("receive failed... 
    ");
            return -1;
        }
        else 
        {        
             printf("%s
    ", buffer);
        }
        return sockfd;    
    }
     
    
    int getResponse()
    {    
        memset(buffer,0,sizeof(buffer));
        ret = recv(sockfd,buffer,1024,0);
        if(ret == SOCKET_ERROR)
        {
            printf("receive nothing
    ");
            return -1;
        }
        buffer[ret]='';
         
        if(*buffer == '5')
        {
            printf("the order is not support smtp host
     ");
            return -1;
        }
    
        printf("recieved buffer:%s
    ",buffer);
    
        return 0;
    }
     
    int login(char *username,char *password)
    {
        printf("login.....sockfd: %d
    ",sockfd);
        char ch[100];
        if(username == "" || password == "")
            return -1;
         
        send_data = "HELO 163.com
    ";
        printf("%s
    ",send_data);
        ret = send(sockfd, send_data, strlen(send_data), 0);
        if(ret == SOCKET_ERROR)
        {
            close(sockfd);
            return -1;
        }         
        if(getResponse() < 0)
            return -1;
         
        send_data = "AUTH LOGIN
    ";
        printf("%s
    ",send_data);
        ret = send(sockfd, send_data, strlen(send_data), 0);
        if(ret == SOCKET_ERROR)
        {
            close(sockfd);
            return -1;
        }
        if(getResponse() < 0)
            return -1;
        sprintf(ch, "%s
    ", username);
        printf("%s
    ",ch);
        ret = send(sockfd, (char *)ch, strlen(ch),0);
        if(ret == SOCKET_ERROR)
        {
            close(sockfd);
            return -1;
        }
         
        if(getResponse() < 0)
             return -1;
         
        sprintf(ch,"%s
    ",password);
        printf("%s
    ",ch);
        ret = send(sockfd,(char *)ch,strlen(ch),0);
        if(ret == SOCKET_ERROR)
        {
    
            close(sockfd);
            return -1;
        }
         
        if(getResponse() < 0)
            return -1;
         
        return 0;
     
    }
     
    int sendmail(char* from, char * to, char * subject, char * context)
    {
        if(from =="" ||to == ""||subject == ""||context == "")
        {
            printf("arguments error!
    ");
            return -1;
        }
        
        sprintf(From, "MAIL FROM: <%s>
    ", from);
        printf("%s
    ",From);
         
        if((ret = send(sockfd, From, strlen(From), 0)) == SOCKET_ERROR)
        {
            close(sockfd);
            return -1;
        }
         
        if(getResponse() < 0)
            return -1;
         
        sprintf(To, "RCPT TO: <%s>
    ", to);
        if((ret = send(sockfd, To, strlen(To),0)) == SOCKET_ERROR)
        {
            close(sockfd);
            return -1;
        }
        if(getResponse() < 0)
            return -1;
         
        send_data = "DATA
    ";
        if((ret = send(sockfd,send_data,strlen(send_data),0)) == SOCKET_ERROR)
        {
            close(sockfd);
            return -1;
        }
        if(getResponse() < 0)
            return -1;
            
        memset(Subject, 0, sizeof(Subject));
        sprintf(Subject, "Subject: %s
    ", subject);
        strcat(Context, Subject);
        strcat(Context, "
    
    ");
        //strcat(Context,context);
        strcat(Context,"
    .
    ");
        if((ret= send(sockfd, Context, strlen(Context), 0)) == SOCKET_ERROR)
        {
            close(sockfd);
            return -1;
        }
         
        printf("ret size:%d
    ", ret);
        
        memset(buffer, '', sizeof(buffer));
        
        if(getResponse() < 0)
             return -1;    
     
        printf("QUIT!!!!!!!!!!!!!!!!
    ");
        if((ret = send(sockfd,"QUIT
    ",strlen("QUIT
    "), 0)) == SOCKET_ERROR)
        {
            close(sockfd);
            return -1;
        }
        if(getResponse() < 0)
            return -1;
     
        printf("Send Mail Successful!
    ");
        return 0;
    }
     
    
    int main()
    {    
        char from[128] = "funnylinux@163.com";
        char to[128] = "754056915@qq.com";
    
        char subject[512] = "test....";
        char context[6000] = "Just a test.........
    ";
        char server[56]="smtp.163.com";
    
        char *name ="xxxxxxxxxxxxxx";//base64
        char *passwd = "xxxxxxxxxxxxxxx";//base64
     
        if(connectHost(server)<0)
        {
            printf("Can Not LOGIN !
    ");
            return -1;
        }
     
        if(login(name, passwd) < 0)
        {
            fprintf(stderr,"Can Not LOGIN !
    ");
            return -1;
        }
        sendmail(from, to, subject, context);
        return 0;
     }
  • 相关阅读:
    JVM heap中各generation的大小(Sizing the Generations)
    MySQL中分组取第一条, 以及删除多余的重复记录
    八芯网线水晶头做法(线序)
    Win7命令行下查看无线网络信息
    OpenWrt的开机启动服务(init scripts)
    犀牛书的实例代码:给对象添加freeze, hide, 查询descriptors
    ES6新特性:Javascript中Generator(生成器)
    ES6新特性:Function函数扩展, 扩展到看不懂
    ES6新特性:Javascript中的Map和WeakMap对象
    ES6新特性:Javascript中Set和WeakSet类型的数据结构
  • 原文地址:https://www.cnblogs.com/funnylinux/p/3574050.html
Copyright © 2020-2023  润新知