簡體   English   中英

如何在 C++ 中使用 cURL 發送 Email 和 Gmail

[英]How to Send Email with Gmail using cURL in C++

希望你們一切都好。

我正在嘗試制作一個程序,該程序將使用 Gmail 發送 email。

我有這個命令可以從 CMD 完美運行,但我不知道如何讓它在應用程序中運行。

這是命令: curl smtps://smtp.gmail.com:465 -v --mail-from "<sir@gmail.com>" --mail-rcpt "<mister@gmail.com>" --ssl -u sir@gmail.com:password -T "mail.txt" -k --anyauth

這些是 mail.txt 的內容:

From: "Sir" <sir@gmail.com>
To: "Mister" <mister@gmail.com>
Subject: This is a test

Hi,
I’m sending this mail with curl thru my gmail account.
Bye!

我在cURL 網站上遵循了這個例子

這就是我得到的:

static const char* payload_text[] = {
"From:<sir@gmail.com>\n"
"To:<mister@gmail.com>\n"
"Subject : This is a test\n\r"

"Hi,\n"
"I’m sending this mail with curl thru my gmail account.\n"
"Bye!\n"
};

struct upload_status {
    int lines_read;
};
static size_t payload_source(void* ptr, size_t size, size_t nmemb, void* userp)
{
    struct upload_status* upload_ctx = (struct upload_status*)userp;
    const char* data;

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

    data = payload_text[upload_ctx->lines_read];

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

        return len;
    }

    return 0;
}

int curl_send_email()
{
    CURL* curl;
    CURLcode res = CURLE_OK;
    struct curl_slist* recipients = NULL;
    struct upload_status upload_ctx = { 0 };

    curl = curl_easy_init();

    if (curl)
    {

        curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.gmail.com:465");

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "<sir@gmail.com>");

        recipients = curl_slist_append(recipients, "<mister@gmail.com>");
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);

        curl_easy_setopt(curl, CURLOPT_USE_SSL, 1L);

        curl_easy_setopt(curl, CURLOPT_USERNAME, "<sir@gmail.com>");
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "password");


        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_HTTPAUTH, (long)CURLAUTH_ANY);

        /* Send the message */
        res = curl_easy_perform(curl);

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

        /* Free the list of recipients */
        curl_slist_free_all(recipients);
        curl_easy_cleanup(curl);
    }

    return (int)res;
}


int main()
{

   int message = curl_send_email();
   std::cout << message << std::endl;

   return 0;
}

但它不起作用,它返回一條消息說“登錄失敗”,即使用戶名和密碼是正確的。

有人可以告訴我我做錯了什么嗎?

提前致謝。

這一直有效到 2022 年 5 月 30 日:

curl 
--url "smtp://smtp.gmail.com:587"
--mail-from "user@gmail.com"
--mail-rcpt "recipient@provider.com"
--upload-file mailContents.txt
--user "user@gmail.com:password"
 --ssl-reqd  --insecure --show-error --silent

但從那時起,谷歌刪除了不安全的登錄選項,我正在尋找一個解決方案,因為你......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM