2011年计算机等级考试二级C辅导实例编程(13)
![](http://www.onekao.net/templets/default/images/content_ad.gif)
用c调用sendmail发邮件
#
最近在做一个小项目,需要用到c来调用sendmail发送邮件,参考了: #
http://www.9php.com/FAQ/cxsjl/rjgc/2007/11/1484369104490.html
的提示,做出来了以下的实现:
意思是打开两个文件,一个是管道的fp,一个是写好了邮件内容的文件,这样就可以发送了,厉害啊
#
[root@bjxdurs235 20090816]# cat -n sendmail.c #
1 #include #
2 #define MAX_LINE_CHAR 512
#
3 #
4 int main(void) #
5 { #
6 char *line; #
7 char buffer[MAX_LINE_CHAR+1];
8 FILE *email_txt;
#
9 FILE *fp;
#
10
#
11 #
12 email_txt = fopen( "email.txt","r" ); #
13 if( email_txt == NULL ){ #
14 perror("email.txt"); #
15 exit (1); #
16 }
#
17 fp=popen("/usr/lib/sendmail -F monitor -t","w");
18
19 while ( (line = fgets( buffer, MAX_LINE_CHAR, email_txt )) != NULL ){
20 fprintf(fp,"%s",buffer); #
21 #
22 } #
23 pclose(fp); #
24 #
25 } #