简体   繁体   English

如何使用 libcurl 发送电子邮件

[英]how to send an email using libcurl

I'm able to send an email to my gmail successfully, but it's sending an empty message and not able to send contain message , anyway the code I'm using is as follows:我能够成功地向我的 gmail 发送电子邮件,但它发送的是空消息并且无法发送包含消息,无论如何我使用的代码如下:

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#define FROM    "<xxxxxx@gmail.com>"
#define TO      "<xxxxx@gmail.com>"

static const char* payload_text[] = 
{
  "To: " TO "\r\n",
  "From: " FROM "\r\n",
  "Subject: SMTP TLS example message\r\n",
  "Contain: Helllo\r\n",
  NULL
};

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 main(void)
{
    CURL* curl;
    CURLcode res = CURLE_OK;
    struct curl_slist* recipients = NULL;
    struct upload_status upload_ctx;
    upload_ctx.lines_read = 0;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxxxx@gmail.com");
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxxxxxx");
        curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587");
        curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
        recipients = curl_slist_append(recipients, TO);
        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 (int)res;
}

How to fix this ?如何解决这个问题? I have no acquaintance with this library,and the only thing I can receive is the subject, Thanks if anyone can help我对这个图书馆不熟悉,我唯一能收到的就是主题,如果有人可以帮忙,谢谢

static const char* payload_text[] = 
{
  "To: " TO "\r\n",
  "From: " FROM "\r\n",
  "Subject: SMTP TLS example message\r\n",
  "Contain: Helllo\r\n",
  NULL
};

should be something like应该是这样的

static const char* payload_text[] = 
{
  "To: " TO "\r\n",
  "From: " FROM "\r\n",
  "Subject: SMTP TLS example message\r\n",
  "\r\n",
  "Helllo\r\n",
  NULL
};

That is Contain: is wrong, and there should be a blank line between the headers and the message.Contain:是错误的,标题和消息之间应该有一个空行。

Email format is described here .此处描述电子邮件格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM