C語言發送郵件

c語言發送郵件
Linux下使用c語言發送郵件 

領導交代一個任務,須要將服務器上的df -hl的執行結果定時發給他。linux

嘗試使用sendmail來發郵件,可是後來放棄了,並非全部的服務器上都安裝了sendmail。服務器

因而,就用c寫一個吧,還可以指定郵件服務器地址和端口,會更靈活些。socket

目標是建立程序qmail,而後經過命令qmail my@163.com your.txt將your.txt的內容發送給my@163.com。spa

在網上找了半天,沒有一個例子可以徹底跑起來,有些是提問題的,最總問題解決了卻不告訴別人,這人真可恨。ip

雖然這些代碼都是複製粘貼來的,可是本着公益的宗旨,就不追究版權了。ci

 
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編譯get

若是你是在高版本的linux下編譯的,在低版本上的linux使用時會報錯string

此時須要在編譯時追加參數:hash

gcc -Wl,--hash-style=sysv qmail.c -o qmail
相關文章
相關標籤/搜索