2

I written a simple little code based around libcurl that sends a email to myself:

#include <cstdio>
#include <cstring>
#include <curl/curl.h>

static const char* payload_text[] = {
  "To: " "<myemail@web.de>" "\r\n",
  "From: " "<myemail@web.de>" " (Example User)\r\n",
  "Cc: " "<myemail@web.de>" " (Another example User)\r\n",
  "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
  "rfcpedant.example.org>\r\n",
  "Subject: Hello\r\n",
  "\r\n",
  "The body of the message starts here.\r\n",
  "\r\n",
  "Test.\r\n",
  nullptr
};

struct upload_status {
    int lines_read;
};

static size_t payload_source(void* ptr, const size_t size, const size_t nmemb, void* userp)
{
    auto* const upload_ctx = static_cast<struct upload_status*>(userp);

    if (size == 0 || nmemb == 0 || size * nmemb < 1) {
        return 0;
    }

    const auto* const data = payload_text[upload_ctx->lines_read];

    if (data) {
        const auto len = strlen(data);
        memcpy(ptr, data, len);
        upload_ctx->lines_read++;

        return len;
    }

    return 0;
}

int _stdcall WinMain(struct HINSTANCE__* hinstance, struct HINSTANCE__* hprevinstance, char* cmdline, int cmdshow)
{
    auto res = CURLE_OK;
    struct curl_slist* recipients = nullptr;
    struct upload_status upload_ctx;

    upload_ctx.lines_read = 0;

    auto* const curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_USERNAME, "myemail@web.de");
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");

        curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.web.de:587");

        curl_easy_setopt(curl, CURLOPT_USE_SSL, static_cast<long>(CURLUSESSL_ALL));

        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "<myemail@web.de>");

        recipients = curl_slist_append(recipients, "<myemail@web.de>");
        recipients = curl_slist_append(recipients, "<myemail@web.de>");
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);

        curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
        curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);

        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        curl_slist_free_all(recipients);

        curl_easy_cleanup(curl);
    }

    return static_cast<int>(res);
}

NOTE: thats not my password there in curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");

The above code works. but i needs libcurl.dll and zlib1.dll

I would like it in a single .exe file

My question is.. Is that possible in c++, i know its possible in c# with using System.Net.Mail or something, or is there are alternative to my code that doesn't require external .dlls

  • 1
    There are no facilities in *standard* C++ for sending emails. You'll need a library. If you don't like `libcurl`, you could find another one. Otherwise, you'll have to write your own that is portable across platforms. – Thomas Matthews Jul 06 '20 at 15:58
  • You can statically link cURL. Check around your development environment. You may already have a static library. If not, you'll have to build the cURL from source and set a few extra compiler options, most notably, `-DCURL_STATICLIB`. – user4581301 Jul 06 '20 at 15:59
  • You may be able to get the source code for those libraries and build them as `static libraries`. The static libraries will be included into your executable. – Thomas Matthews Jul 06 '20 at 15:59
  • @ThomasMatthews thank you for your answer i will try – Margarita Weiß Jul 06 '20 at 16:01
  • Looking for a duplicate, I stumbled across this nasty reminder: https://stackoverflow.com/a/7280030/4581301 – user4581301 Jul 06 '20 at 16:01
  • @user4581301 i added a static curl lib and im still getting errors. do i have to modify something in my code? my environment is visual studio 2019 – Margarita Weiß Jul 06 '20 at 16:11
  • Add `CURL_STATICLIB` to project->properties->Configuration Properties->C/C++->Preprocessor->Preprocessor definitions or make sure you have `#define CURL_STATICLIB` in every file that includes curl headers before it includes the headers. – user4581301 Jul 06 '20 at 16:25

0 Answers0