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

发布于 2025-01-06 12:16:27 字数 221 浏览 1 评论 0原文

我想在每次模拟结束时向我的 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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三生池水覆流年 2025-01-13 12:16:27

您可以使用 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]);
    }
}

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)
{
    if (argc == 5) {
        sendmail(argv[1], argv[2], argv[3], argv[4]);
    }
}
霊感 2025-01-13 12:16:27

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

源码包中有示例应用程序。

libESMTP seems to be what you are looking for. It's very well documented and also seems to be under active development (last Release Candidate is from mid-January 2012). It also supports SSL and various authentication protocols.

There are example applications in the source package.

我要还你自由 2025-01-13 12:16:27

VMimelibcurl 是用于电子邮件发送(以及更多)的优秀库。

Both VMime and libcurl are good libraries for email sending (and more).

安静 2025-01-13 12:16:27

我喜欢上面 trojanfoe 的答案,但就我而言,我需要打开电子邮件发送代理.. MTA 来使 linux 能够发送电子邮件 - 我发现 exim4 是一个相对简单的 MTA 来工作,并且 trojanfoe 的程序可以工作非常好用。

为了让它工作,我使用了(在虚拟机中的 debian 类型系统上(crunchbang linux))

sudo apt-get install exim

sudo apt-get install mailutils

我配置了 exim4

sudo dpkg-重新配置 exim4-config

我告诉计算机我的正常(远程)电子邮件地址

sudo emacs /etc/email-addresses

希望这可能有用,因为我发现这些步骤可以让我的 Linux 系统使用上面的 trojanfoe 电子邮件程序发送电子邮件

I like the answer of trojanfoe above, BUT in my case I needed to turn on an email sending agent.. an MTA to enable linux to send emails - I have found exim4 to be a relatively simple MTA to get working and that trojanfoe's program works very nicely with it.

to get it to work I used (on a debian type system in a virtual box (crunchbang linux))

sudo apt-get install exim

sudo apt-get install mailutils

I configured exim4 with

sudo dpkg-reconfigure exim4-config

and I told the computer about my normal (remote) email address with

sudo emacs /etc/email-addresses

hope this might be useful as these were the steps I found worked to get my linux system sending email with trojanfoe's email program above

天气好吗我好吗 2025-01-13 12:16:27

执行 fork exec 并将主体通过管道传输到 sendmail/mail 等程序:

#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

using std::string;

static const int READEND = 0;
static const int WRITEEND = 1;

int sendEmail(const string& to, const string& subject, const string& body) {
  int p2cFd[2];

  int ret = pipe(p2cFd);
  if (ret) {
    return ret;
  }

  pid_t child_pid = fork();
  if (child_pid < 0) {
    close(p2cFd[READEND]);
    close(p2cFd[WRITEEND]);

    return child_pid;
  }
  else if (!child_pid) {
    dup2(p2cFd[READEND], READEND);
    close(p2cFd[READEND]);
    close(p2cFd[WRITEEND]);

    execlp("mail", "mail", "-s", subject.c_str(), to.c_str(), NULL);

    exit(EXIT_FAILURE);
  }

  close(p2cFd[READEND]);

  ret = write(p2cFd[WRITEEND], body.c_str(), body.size());
  if (ret < 0) {
    return ret;
  }

  close(p2cFd[WRITEEND]);

  if (waitpid(child_pid, &ret, 0) == -1) {
    return ret;
  }

  return 0;
}

int main() {
  return sendEmail("[email protected]", "Subject", "Body");
}

Do a fork exec and pipe the body to a program like sendmail/mail:

#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

using std::string;

static const int READEND = 0;
static const int WRITEEND = 1;

int sendEmail(const string& to, const string& subject, const string& body) {
  int p2cFd[2];

  int ret = pipe(p2cFd);
  if (ret) {
    return ret;
  }

  pid_t child_pid = fork();
  if (child_pid < 0) {
    close(p2cFd[READEND]);
    close(p2cFd[WRITEEND]);

    return child_pid;
  }
  else if (!child_pid) {
    dup2(p2cFd[READEND], READEND);
    close(p2cFd[READEND]);
    close(p2cFd[WRITEEND]);

    execlp("mail", "mail", "-s", subject.c_str(), to.c_str(), NULL);

    exit(EXIT_FAILURE);
  }

  close(p2cFd[READEND]);

  ret = write(p2cFd[WRITEEND], body.c_str(), body.size());
  if (ret < 0) {
    return ret;
  }

  close(p2cFd[WRITEEND]);

  if (waitpid(child_pid, &ret, 0) == -1) {
    return ret;
  }

  return 0;
}

int main() {
  return sendEmail("[email protected]", "Subject", "Body");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文