send email without using libcurl.dll in c++

Viewed 92

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

0 Answers
Related