从 linux 中的 C/C++ 程序发送电子邮件 [英] sending an email from a C/C++ program in linux

查看:8
本文介绍了从 linux 中的 C/C++ 程序发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to send an email to my gmail account everytime my simulation ends. I have tried searching the web and found sendEmail but it is timing-out. If anyone could point me out to a package or link that they tried I would be thankful.

谢谢

推荐答案

您可以使用 popen() 直接调用本地 MTA,并为其提供符合 RFC822 标准的文本.

You could invoke your local MTA directly using popen() and feed it RFC822-compliant text.

#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
", to);
        fprintf(mailpipe, "From: %s
", from);
        fprintf(mailpipe, "Subject: %s

", subject);
        fwrite(message, 1, strlen(message), mailpipe);
        fwrite(".
", 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
", argc);

    for (i = 0; i < argc; i++)
        printf("argv[%d] = "%s"
", i, argv[i]);
    sendmail(argv[1], argv[2], argv[3], argv[4]);
}

这篇关于从 linux 中的 C/C++ 程序发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆