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

标签 c++ c linux email gmail

我想在每次模拟结束时向我的 gmail 帐户发送一封电子邮件。我尝试在网上搜索并找到 sendEmail但它正在超时。如果有人可以向我指出他们尝试过的包或链接,我将不胜感激。

谢谢

最佳答案

您可以使用 popen() 直接调用您的本地 MTA,并向其提供符合 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]);
    }
}

关于c++ - 从 Linux 中的 C/C++ 程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9317305/

相关文章:

c++ - 如何从文件中读取编译器标志

带有 '<' 和 '- option' 的命令行 C

python - 关于将大量脚本部署到 Web 服务器(Java、Python)的建议

linux - 将 base64 字符串解码为文件中的十六进制字符串并覆盖

linux - sed命令用文件路径中的字符串替换字符串

c++ - 在 C++ 中覆盖 + 运算符时分配内存

c++ - 从 cin 中选择下一行

c++ - 带有常量参数的类声明中的类实例

c++ - 在 C++ 函数中就地处理参数

c - 使用 sock_stream 时连续调用 write 是否安全?