sending an email from a C/C++ program in linux

后端 未结 5 1646
花落未央
花落未央 2020-12-24 08:57

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 poin

相关标签:
5条回答
  • 2020-12-24 09:45

    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

    0 讨论(0)
  • 2020-12-24 09:49

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

    0 讨论(0)
  • 2020-12-24 09:56

    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.

    0 讨论(0)
  • 2020-12-24 09:57

    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@hostname.com", "Subject", "Body");
    }
    
    0 讨论(0)
  • 2020-12-24 09:58

    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]);
    }
    
    0 讨论(0)
提交回复
热议问题