从 Linux 中的 C/C++ 程序发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9317305/
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
sending an email from a C/C++ program in linux
提问by Madagascar
I would like to send an email to my gmail account everytime my simulation ends. I have tried searching the web and found sendEmailbut it is timing-out. If anyone could point me out to a package or link that they tried I would be thankful.
我想在每次模拟结束时向我的 gmail 帐户发送一封电子邮件。我尝试在网上搜索并找到了sendEmail,但它超时了。如果有人可以向我指出他们尝试过的软件包或链接,我将不胜感激。
Thanks
谢谢
采纳答案by trojanfoe
You could invoke your local MTA directly using popen()
and feed it RFC822-compliant text.
您可以直接使用本地 MTA 调用popen()
并向其提供符合 RFC822 的文本。
#include <stdio.h>
#include <string.h>
#include <errno.h>
int sendmail(const char *to, const char *from, const char *subject, const char *message)
{
int retval = -1;
FILE *mailpipe = popen("/usr/lib/sendmail -t", "w");
if (mailpipe != NULL) {
fprintf(mailpipe, "To: %s\n", to);
fprintf(mailpipe, "From: %s\n", from);
fprintf(mailpipe, "Subject: %s\n\n", subject);
fwrite(message, 1, strlen(message), mailpipe);
fwrite(".\n", 1, 2, mailpipe);
pclose(mailpipe);
retval = 0;
}
else {
perror("Failed to invoke sendmail");
}
return retval;
}
main(int argc, char** argv)
{
int i;
printf("argc = %d\n", argc);
for (i = 0; i < argc; i++)
printf("argv[%d] = \"%s\"\n", i, argv[i]);
sendmail(argv[1], argv[2], argv[3], argv[4]);
}
回答by jupp0r
libESMTPseems to be what you are looking for. It's very well documented and also seems to be under active development (last Release Candidate is from mid-January 2012). It also supports SSL and various authentication protocols.
libESMTP似乎正是您要找的。它有很好的文档记录并且似乎也在积极开发中(最后一个候选版本是从 2012 年 1 月中旬开始的)。它还支持 SSL 和各种身份验证协议。
There are example applications in the source package.
源包中有示例应用程序。
回答by Kerrek SB
回答by tom
I like the answer of trojanfoe above, BUT in my case I needed to turn on an email sending agent.. an MTA to enable linux to send emails - I have found exim4 to be a relatively simple MTA to get working and that trojanfoe's program works very nicely with it.
我喜欢上面 trojanfoe 的答案,但在我的情况下,我需要打开电子邮件发送代理.. 一个 MTA 使 linux 能够发送电子邮件 - 我发现 exim4 是一个相对简单的 MTA 来开始工作,并且 trojanfoe 的程序可以工作很好用。
to get it to work I used (on a debian type system in a virtual box (crunchbang linux))
为了让它工作我使用过(在虚拟机中的 debian 类型系统上(crunchbang linux))
sudo apt-get install exim
sudo apt-get install mailutils
sudo apt-get install exim
sudo apt-get install mailutils
I configured exim4 with
我配置了 exim4
sudo dpkg-reconfigure exim4-config
须藤 dpkg-reconfigure exim4-config
and I told the computer about my normal (remote) email address with
我告诉计算机我的普通(远程)电子邮件地址
sudo emacs /etc/email-addresses
须藤 emacs /etc/email-addresses
hope this might be useful as these were the steps I found worked to get my linux system sending email with trojanfoe's email program above
希望这可能有用,因为这些是我发现可以让我的 linux 系统使用上面的特洛伊木马病毒电子邮件程序发送电子邮件的步骤
回答by ericcurtin
Do a fork exec and pipe the body to a program like sendmail/mail:
执行 fork exec 并将主体通过管道传输到 sendmail/mail 之类的程序:
#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
using std::string;
static const int READEND = 0;
static const int WRITEEND = 1;
int sendEmail(const string& to, const string& subject, const string& body) {
int p2cFd[2];
int ret = pipe(p2cFd);
if (ret) {
return ret;
}
pid_t child_pid = fork();
if (child_pid < 0) {
close(p2cFd[READEND]);
close(p2cFd[WRITEEND]);
return child_pid;
}
else if (!child_pid) {
dup2(p2cFd[READEND], READEND);
close(p2cFd[READEND]);
close(p2cFd[WRITEEND]);
execlp("mail", "mail", "-s", subject.c_str(), to.c_str(), NULL);
exit(EXIT_FAILURE);
}
close(p2cFd[READEND]);
ret = write(p2cFd[WRITEEND], body.c_str(), body.size());
if (ret < 0) {
return ret;
}
close(p2cFd[WRITEEND]);
if (waitpid(child_pid, &ret, 0) == -1) {
return ret;
}
return 0;
}
int main() {
return sendEmail("[email protected]", "Subject", "Body");
}