刘政道 - 应用程序框架

《31天学会CRM项目开发(C#编程入门及项目实战)》作者,IT经理,程序员
  博客园  :: 新随笔  :: 联系 :: 管理

c语言发送邮件

Posted on 2010-12-31 14:46  刘政道  阅读(1119)  评论(0编辑  收藏  举报
c语言发送邮件
Linux下使用c语言发送邮件

领导交代一个任务,需要将服务器上的df -hl的执行结果定时发给他。

尝试使用sendmail来发邮件,但是后来放弃了,并不是所有的服务器上都安装了sendmail。

于是,就用c写一个吧,还能够指定邮件服务器地址和端口,会更灵活些。

目标是创建程序qmail,然后通过命令qmail my@163.com your.txt将your.txt的内容发送给my@163.com。

在网上找了半天,没有一个例子能够完全跑起来,有些是提问题的,最总问题解决了却不告诉别人,这人真可恨。

虽然这些代码都是复制粘贴来的,但是本着公益的宗旨,就不追究版权了。


  1 
  2 #include <stdio.h>
  3 #include <sys/socket.h>
  4 #include <sys/types.h>
  5 #include <netinet/in.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 
  9 int main(int argc,char *argv[])
 10 {
 11     int sockfd = -1;
 12     int iconn = -2;
 13     ssize_t retConnect = -2;
 14     struct sockaddr_in servaddr;
 15     char ip[20= "192.168.0.251";//设置SMTP地址
 16     char sentmsg[2048= "";
 17     char buf[255= "";
 18     char fileContent[1024= "";
 19     FILE* file;
 20     char cin[255= "";
 21 
 22     int len = 0;
 23     while(!(argv[len]==NULL))
 24     {
 25         len++;
 26     }
 27 
 28     if(len!=3)
 29     {
 30         printf("Usage:qmail yourname@163.com df.log\n");
 31         exit(0);
 32     }
 33     memset(fileContent,'\0',sizeof(fileContent));
 34     if((file = fopen(argv[2], "r")) == NULL)
 35         printf("Not find file");
 36     else
 37     {
 38         while(fgets(cin, sizeof(cin), file) != NULL)
 39         {
 40             strcat(fileContent,cin);
 41             memset(cin,'\0',sizeof(cin));
 42         }
 43     } 
 44     fclose(file);
 45     printf("%s",fileContent);
 46     setvbuf(stdout,NULL,_IONBF,0);
 47     sockfd = socket(AF_INET,SOCK_STREAM,0);//以scoket方式和邮件服务器通讯
 48     if(sockfd>0)
 49     {
 50         printf("socket is open");
 51         bzero(&servaddr,sizeof(servaddr));
 52         servaddr.sin_family=AF_INET;
 53         servaddr.sin_port=htons(25);
 54         inet_pton(AF_INET,ip,&servaddr.sin_addr);
 55         iconn = connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));    
 56         if(iconn==0)
 57         {
 58             printf("connect to 192.168.0.251 25 success!");
 59             retConnect = recv(sockfd,buf,sizeof(buf),0);
 60             if(retConnect==-1)
 61             {
 62                 printf("Failed to receive msg from smtp port");
 63             }
 64             else
 65                 printf("\nServer:%s\n",buf);
 66             memset(sentmsg,'\0',sizeof(sentmsg));
 67             strcpy(sentmsg,"HELO SERVER\r\n");
 68             retConnect = send(sockfd,sentmsg,strlen(sentmsg),0);
 69             if(retConnect==-1)
 70             {
 71                 printf("\nFailed to send meg to smtp port in step 2.\n");
 72                 exit(1);
 73             }
 74             else
 75             {
 76                 printf("%s",sentmsg);
 77             }
 78             memset(buf,'\0',sizeof(buf));
 79             retConnect=recv(sockfd,buf,sizeof(buf),0);
 80             if(retConnect == -1)
 81             {
 82                 printf("\nFailed to recive meg from smtp port in step 3.\n");
 83                 exit(1);
 84             }
 85             else
 86             {
 87                 printf("%s\n",buf);
 88             }
 89             
 90                         memset(sentmsg,'\0',sizeof(sentmsg));
 91                         strcpy(sentmsg,"MAIL FROM: qdcm@163.com\r\n");               
 92                         strcat(sentmsg,"RCPT TO: ");
 93                         strcat(sentmsg,argv[1]);
 94                         strcat(sentmsg,"\r\n");
 95                         retConnect = send(sockfd,sentmsg,strlen(sentmsg),0);
 96                         if(retConnect>0)
 97                             printf("%s",sentmsg);
 98                         memset(buf,'\0',sizeof(buf));
 99                         retConnect=recv(sockfd,buf,sizeof(buf),0);
100                         if(retConnect>0)
101                             printf("%s\n",buf);
102                             
103                         memset(sentmsg,'\0',sizeof(sentmsg));
104                         strcpy(sentmsg,"DATA\r\n");
105                         retConnect = send(sockfd,sentmsg,strlen(sentmsg),0);
106                         if(retConnect>0)
107                             printf("%s",sentmsg);
108                         memset(buf,'\0',sizeof(buf));
109                         retConnect=recv(sockfd,buf,sizeof(buf),0);
110                         if(retConnect>0)
111                             printf("%s\n",buf);
112             
113                         memset(sentmsg,'\0',sizeof(sentmsg));
114                         strcpy(sentmsg,"From:qdcm@163.com\r\n");
115                         strcat(sentmsg,"To:");
116                         strcat(sentmsg,argv[1]);
117                         strcat(sentmsg,"\r\n");
118                         strcat(sentmsg,"Subject:QDCM Host Check Data\r\n\r\n");
119                         strcat(sentmsg,fileContent);
120                         strcat(sentmsg,"\r\n");            
121                         strcat(sentmsg,"\r\n.\r\n");
122                         retConnect = send(sockfd,sentmsg,strlen(sentmsg),0);
123                         memset(sentmsg,'\0',sizeof(sentmsg));
124                         
125                         strcpy(sentmsg,"QUIT\r\n");
126                         retConnect = send(sockfd,sentmsg,strlen(sentmsg),0);
127                         if(retConnect>0)
128                             printf("%s",sentmsg);
129                         memset(buf,'\0',sizeof(buf));
130                         retConnect=recv(sockfd,buf,sizeof(buf),0);
131                         if(retConnect>0)
132                             printf("%s\n",buf);
133 
134         }
135         else
136         {
137             printf("connect 192.168.0.251 25 failed!");
138             sleep(1);
139         }
140         close(sockfd);
141     }
142     else
143     {
144         printf("open socket failed!");
145     }
146     return 0;
147 }
148 

使用gcc编译

如果你是在高版本的linux下编译的,在低版本上的linux使用时会报错

此时需要在编译时追加参数:

gcc -Wl,--hash-style=sysv qmail.c -o qmail