C语言 如何在C中发送电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2362989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I send e-mail in C?
提问by mehmet6parmak
I'm just wondering how can I send email using C? I Googled it a little bit, but could not find anything proper.
我只是想知道如何使用 C 发送电子邮件?我用谷歌搜索了一下,但找不到任何合适的东西。
回答by codaddict
On Unix like systems you can use systemand sendmailas follows:
在类 Unix 系统上,您可以使用system,sendmail如下所示:
#include <stdio.h>
#include <string.h>
int main() {
char cmd[100]; // to hold the command.
char to[] = "[email protected]"; // 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 :)
我知道它很丑,有几种更好的方法可以做到……但它有效:)
回答by Marc Kline
Use libcurl. It supports SMTP and also TLS, in case you need to authenticate for sending. They offer some example C code.
回答by Brecht Sanders
A more portable way is to use libquickmail(http://sf.net/p/libquickmail), licensed under GPL. It even allows sending attachments.
更便携的方法是使用libquickmail( http://sf.net/p/libquickmail),在 GPL 许可下。它甚至允许发送附件。
Example code:
示例代码:
quickmail_initialize();
quickmail mailobj = quickmail_create(FROM, "libquickmail test e-mail");
quickmail_set_body(mailobj, "This is a test e-mail.\nThis mail was sent using libquickmail.");
quickmail_add_attachment_file(mailobj, "attachment.zip", NULL);
const char* errmsg;
if ((errmsg = quickmail_send(mailobj, SMTPSERVER, SMTPPORT, SMTPUSER, SMTPPASS)) != NULL)
fprintf(stderr, "Error sending e-mail: %s\n", errmsg);
quickmail_destroy(mailobj);
回答by Messa
Run sendmailand pass the e-mail to its standard input (on unix-like systems), or use some SMTP client library to connect to SMTP mail server.
运行sendmail并将电子邮件传递到其标准输入(在类 Unix 系统上),或使用某些 SMTP 客户端库连接到 SMTP 邮件服务器。
回答by unwind
The most obvious choices:
最明显的选择:
- Use
system()to call an existing command-line tool to send mail. Not very portable (requires an external tool with a given calling syntax, etc) but very easy to implement. - Use some library.
- Implement SMTPyourself, and talk directly to a mail server. A lot of work.
- 使用
system()调用现有的命令行工具来发送邮件。不是很便携(需要具有给定调用语法等的外部工具)但很容易实现。 - 使用一些图书馆。
- 自己实现SMTP,并直接与邮件服务器对话。很多工作。
回答by muruga
You can use the mail command also.
您也可以使用邮件命令。
Inside the C program using the mail command and system function you can send the mail to the user.
在 C 程序中,使用 mail 命令和 system 函数可以向用户发送邮件。
system("mail -s subject address < filename")
Example
system ("mail -s test [email protected] < filename")
Note: The file should be exists. If you want to type the content, yiu can type the content inside the file, then send that file to receiver.
注意:该文件应该存在。如果你想输入内容,可以在文件中输入内容,然后将该文件发送给接收者。

