从Linux中的C/C++程序发送电子邮件

Mad*_*car 11 c c++ linux email gmail

我希望每次模拟结束时都会向我的Gmail帐户发送一封电子邮件.我试过在网上搜索并发现sendEmail,但它已超时.如果有人能指出我们试过的包裹或链接,我会感激不尽.

谢谢

tro*_*foe 22

您可以直接调用本地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]);
}
Run Code Online (Sandbox Code Playgroud)

  • 你必须确定`message`的最后一个字符是`'\n'`. (3认同)
  • 只要您确定`message`不包含`"\n.\n"作为子字符串,这就可以正常工作. (2认同)

jup*_*p0r 5

libESMTP似乎正是您要找的。它有很好的文档记录并且似乎也在积极开发中(最后一个候选版本是从 2012 年 1 月中旬开始的)。它还支持 SSL 和各种身份验证协议。

源包中有示例应用程序。