如何使用C发送电子邮件? [英] How can I send e-mail in C?
本文介绍了如何使用C发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道如何使用C发送电子邮件?我用Google搜索了一下,但是找不到合适的东西.
I'm just wondering how can I send email using C? I Googled it a little bit, but could not find anything proper.
推荐答案
在类似Unix的系统上,您可以如下使用 system
和 sendmail
:
On Unix like systems you can use system
and sendmail
as follows:
#include <stdio.h>
#include <string.h>
int main() {
char cmd[100]; // to hold the command.
char to[] = "sample@example.com"; // email id of the recepient.
char body[] = "SO rocks"; // email body.
char tempFile[100]; // name of tempfile.
strcpy(tempFile,tempnam("/tmp","sendmail")); // generate temp file name.
FILE *fp = fopen(tempFile,"w"); // open it for writing.
fprintf(fp,"%s\n",body); // write body to it.
fclose(fp); // close it.
sprintf(cmd,"sendmail %s < %s",to,tempFile); // prepare command.
system(cmd); // execute it.
return 0;
}
我知道它很丑,并且有几种更好的方法可以做到...但是它可以工作:)
I know its ugly and there are several better ways to do it...but it works :)
这篇关于如何使用C发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文