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

2023-11-27

我想在每次模拟结束时向我的 Gmail 帐户发送一封电子邮件。我尝试在网上搜索并发现发电子邮件但已经超时了。如果有人能向我指出他们尝试过的包或链接,我将不胜感激。

Thanks


您可以直接使用调用本地 MTApopen()并向其提供符合 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)
{
    if (argc == 5) {
        sendmail(argv[1], argv[2], argv[3], argv[4]);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Linux 中的 C/C++ 程序发送电子邮件 的相关文章

随机推荐