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

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

问题描述

我想发邮件到我的Gmail帐户,每次我的模拟结束。我曾尝试在网上搜索,发现<一个href=\"http://www.debianadmin.com/how-to-sendemail-from-the-command-line-using-a-gmail-account-and-others.html\">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.

感谢

推荐答案

您可以调用本地MTA直接使用的popen()和饲料它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\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]);
}

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

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