C语言发送邮件
c语言发送邮件
Linux下使用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
9intmain(intargc,char*argv[])
10{
11intsockfd =-1;
12inticonn =-2;
13ssize_t retConnect =-2;
14structsockaddr_in servaddr;
15charip[20] ="192.168.0.251";//设置SMTP地址
16charsentmsg[2048] ="";
17charbuf[255] ="";
18charfileContent[1024] ="";
19FILE*file;
20charcin[255] ="";
21
22intlen =0;
23while(!(argv[len]==NULL))
24{
25len++;
26}
27
28if(len!=3)
29{
30printf("Usage:qmail yourname@163.com df.log\n");
31exit(0);
32}
33memset(fileContent,'\0',sizeof(fileContent));
34if((file =fopen(argv[2], "r")) ==NULL)
35printf("Not find file");
36else
37{
38while(fgets(cin, sizeof(cin), file) !=NULL)
39{
40strcat(fileContent,cin);
41memset(cin,'\0',sizeof(cin));
42}
43}
44fclose(file);
45printf("%s",fileContent);
46setvbuf(stdout,NULL,_IONBF,0);
47sockfd =socket(AF_INET,SOCK_STREAM,0);//以scoket方式和邮件服务器通讯
48if(sockfd>0)
49{
50printf("socket is open");
51bzero(&servaddr,sizeof(servaddr));
52servaddr.sin_family=AF_INET;
53servaddr.sin_port=htons(25);
54inet_pton(AF_INET,ip,&servaddr.sin_addr);
55iconn =connect(sockfd,(structsockaddr *)&servaddr,sizeof(servaddr));
56if(iconn==0)
57{
58printf("connect to 192.168.0.251 25 success!");
59retConnect =recv(sockfd,buf,sizeof(buf),0);
60if(retConnect==-1)
61{
62printf("Failed to receive msg from smtp port");
63}
64else
65printf("\nServer:%s\n",buf);
66memset(sentmsg,'\0',sizeof(sentmsg));
67strcpy(sentmsg,"HELO SERVER\r\n");
68retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
69if(retConnect==-1)
70{
71printf("\nFailed to send meg to smtp port in step 2.\n");
72exit(1);
73}
74else
75{
76printf("%s",sentmsg);
77}
78memset(buf,'\0',sizeof(buf));
79retConnect=recv(sockfd,buf,sizeof(buf),0);
80if(retConnect ==-1)
81{
82printf("\nFailed to recive meg from smtp port in step 3.\n");
83exit(1);
84}
85else
86{
87printf("%s\n",buf);
88}
89
90memset(sentmsg,'\0',sizeof(sentmsg));
91strcpy(sentmsg,"MAIL FROM: qdcm@163.com\r\n");
92strcat(sentmsg,"RCPT TO: ");
93strcat(sentmsg,argv[1]);
94strcat(sentmsg,"\r\n");
95retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
96if(retConnect>0)
97printf("%s",sentmsg);
98memset(buf,'\0',sizeof(buf));
99retConnect=recv(sockfd,buf,sizeof(buf),0);
100if(retConnect>0)
101printf("%s\n",buf);
102
103memset(sentmsg,'\0',sizeof(sentmsg));
104strcpy(sentmsg,"DATA\r\n");
105retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
106if(retConnect>0)
107printf("%s",sentmsg);
108memset(buf,'\0',sizeof(buf));
109retConnect=recv(sockfd,buf,sizeof(buf),0);
110if(retConnect>0)
111printf("%s\n",buf);
112
113memset(sentmsg,'\0',sizeof(sentmsg));
114strcpy(sentmsg,"From:qdcm@163.com\r\n");
115strcat(sentmsg,"To:");
116strcat(sentmsg,argv[1]);
117strcat(sentmsg,"\r\n");
118strcat(sentmsg,"Subject:QDCM Host Check Data\r\n\r\n");
119strcat(sentmsg,fileContent);
120strcat(sentmsg,"\r\n");
121strcat(sentmsg,"\r\n.\r\n");
122retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
123memset(sentmsg,'\0',sizeof(sentmsg));
124
125strcpy(sentmsg,"QUIT\r\n");
126retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
127if(retConnect>0)
128printf("%s",sentmsg);
129memset(buf,'\0',sizeof(buf));
130retConnect=recv(sockfd,buf,sizeof(buf),0);
131if(retConnect>0)
132printf("%s\n",buf);
133
134}
135else
136{
137printf("connect 192.168.0.251 25 failed!");
138sleep(1);
139}
140close(sockfd);
141}
142else
143{
144printf("open socket failed!");
145}
146return0;
147}
148
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
9intmain(intargc,char*argv[])
10{
11intsockfd =-1;
12inticonn =-2;
13ssize_t retConnect =-2;
14structsockaddr_in servaddr;
15charip[20] ="192.168.0.251";//设置SMTP地址
16charsentmsg[2048] ="";
17charbuf[255] ="";
18charfileContent[1024] ="";
19FILE*file;
20charcin[255] ="";
21
22intlen =0;
23while(!(argv[len]==NULL))
24{
25len++;
26}
27
28if(len!=3)
29{
30printf("Usage:qmail yourname@163.com df.log\n");
31exit(0);
32}
33memset(fileContent,'\0',sizeof(fileContent));
34if((file =fopen(argv[2], "r")) ==NULL)
35printf("Not find file");
36else
37{
38while(fgets(cin, sizeof(cin), file) !=NULL)
39{
40strcat(fileContent,cin);
41memset(cin,'\0',sizeof(cin));
42}
43}
44fclose(file);
45printf("%s",fileContent);
46setvbuf(stdout,NULL,_IONBF,0);
47sockfd =socket(AF_INET,SOCK_STREAM,0);//以scoket方式和邮件服务器通讯
48if(sockfd>0)
49{
50printf("socket is open");
51bzero(&servaddr,sizeof(servaddr));
52servaddr.sin_family=AF_INET;
53servaddr.sin_port=htons(25);
54inet_pton(AF_INET,ip,&servaddr.sin_addr);
55iconn =connect(sockfd,(structsockaddr *)&servaddr,sizeof(servaddr));
56if(iconn==0)
57{
58printf("connect to 192.168.0.251 25 success!");
59retConnect =recv(sockfd,buf,sizeof(buf),0);
60if(retConnect==-1)
61{
62printf("Failed to receive msg from smtp port");
63}
64else
65printf("\nServer:%s\n",buf);
66memset(sentmsg,'\0',sizeof(sentmsg));
67strcpy(sentmsg,"HELO SERVER\r\n");
68retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
69if(retConnect==-1)
70{
71printf("\nFailed to send meg to smtp port in step 2.\n");
72exit(1);
73}
74else
75{
76printf("%s",sentmsg);
77}
78memset(buf,'\0',sizeof(buf));
79retConnect=recv(sockfd,buf,sizeof(buf),0);
80if(retConnect ==-1)
81{
82printf("\nFailed to recive meg from smtp port in step 3.\n");
83exit(1);
84}
85else
86{
87printf("%s\n",buf);
88}
89
90memset(sentmsg,'\0',sizeof(sentmsg));
91strcpy(sentmsg,"MAIL FROM: qdcm@163.com\r\n");
92strcat(sentmsg,"RCPT TO: ");
93strcat(sentmsg,argv[1]);
94strcat(sentmsg,"\r\n");
95retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
96if(retConnect>0)
97printf("%s",sentmsg);
98memset(buf,'\0',sizeof(buf));
99retConnect=recv(sockfd,buf,sizeof(buf),0);
100if(retConnect>0)
101printf("%s\n",buf);
102
103memset(sentmsg,'\0',sizeof(sentmsg));
104strcpy(sentmsg,"DATA\r\n");
105retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
106if(retConnect>0)
107printf("%s",sentmsg);
108memset(buf,'\0',sizeof(buf));
109retConnect=recv(sockfd,buf,sizeof(buf),0);
110if(retConnect>0)
111printf("%s\n",buf);
112
113memset(sentmsg,'\0',sizeof(sentmsg));
114strcpy(sentmsg,"From:qdcm@163.com\r\n");
115strcat(sentmsg,"To:");
116strcat(sentmsg,argv[1]);
117strcat(sentmsg,"\r\n");
118strcat(sentmsg,"Subject:QDCM Host Check Data\r\n\r\n");
119strcat(sentmsg,fileContent);
120strcat(sentmsg,"\r\n");
121strcat(sentmsg,"\r\n.\r\n");
122retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
123memset(sentmsg,'\0',sizeof(sentmsg));
124
125strcpy(sentmsg,"QUIT\r\n");
126retConnect =send(sockfd,sentmsg,strlen(sentmsg),0);
127if(retConnect>0)
128printf("%s",sentmsg);
129memset(buf,'\0',sizeof(buf));
130retConnect=recv(sockfd,buf,sizeof(buf),0);
131if(retConnect>0)
132printf("%s\n",buf);
133
134}
135else
136{
137printf("connect 192.168.0.251 25 failed!");
138sleep(1);
139}
140close(sockfd);
141}
142else
143{
144printf("open socket failed!");
145}
146return0;
147}
148
使用gcc编译
如果你是在高版本的linux下编译的,在低版本上的linux使用时会报错
此时需要在编译时追加参数:
gcc -Wl,--hash-style=sysv qmail.c -o qmail
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/3414524.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2013-11-08 16:23 jack_Meng 阅读(1372) 评论(1) 编辑 收藏 举报