使用 gmail 发送带有 libcurl/smtp 的电子邮件:登录被拒绝

Tom*_*Tom 7 c email gmail smtp libcurl

我知道那里有数百个帖子,但不知何故这对我不起作用。我正在尝试使用 libcurl 发送电子邮件。这是我的代码:

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

#define FROM    "<myemail@gmail.com>"
#define TO      "<someother@gmail.com>"
#define CC      "<someother2@hotmail.com>"

static const char *payload_text[] = {
  "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
  "To: " TO "\r\n",
  "From: " FROM "(Example User)\r\n",
  "Cc: " CC "(Another example User)\r\n",
  "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
  "rfcpedant.example.org>\r\n",
  "Subject: SMTP example message\r\n",
  "\r\n", /* empty line to divide headers from body, see RFC5322 */
  "The body of the message starts here.\r\n",
  "\r\n",
  "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
  "Check RFC5322.\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_URL, "smtp://smtp.gmail.com:587");
    curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
    curl_easy_setopt(curl, CURLOPT_USERNAME, "myemail@gmail.com");
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypassword");
    curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);

    recipients = curl_slist_append(recipients, TO);
    recipients = curl_slist_append(recipients, CC);
    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);

    res = curl_easy_perform(curl);

    /* Check for errors */
    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;
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过这个非常相似的例子,除了文本通过不同的文件传输。

但我总是得到

curl_easy_perform() 失败:登录被拒绝

然后 gmail 给我发了一条消息,说有人试图入侵我的帐户。(登录凭据正确)

这个 curl-config --feature

给我

SSL IPv6 UnixSockets libz NTLM NTLM_WB

阅读了几篇博文,我也尝试使用 hotmail smtp://smtp.live.com:465

但在这里我得到

curl_easy_perform() 失败:无法连接到服务器

我在这里做错了什么?

Nic*_*Bob 7

我不得不花费大量时间来让它工作,但我终于让它工作了。这是我发现的:

第一的:

#define FROM    "<myemail@gmail.com>"
#define TO      "<someother@gmail.com>"
#define CC      "<someother2@hotmail.com>"
Run Code Online (Sandbox Code Playgroud)

电子邮件地址中不应有任何“<”或“>”。我犯了重用定义的错误,我一生都无法理解登录失败的原因。

第二:

您很可能需要去这里并允许不安全的应用程序:https : //www.google.com/settings/security/lesssecureapps

第三:

如果您使用端口 587,则需要以下行:

curl_easy_setopt(m_curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
Run Code Online (Sandbox Code Playgroud)

465似乎不需要这个。

笔记:

可以使用端口 465 或 587。465 需要 SSL,因此您需要使用 smtps 而不是 smtp 来指定 - smtps://smtp.gmail.com:465。我在这里找到了一些相关信息:https : //support.google.com/a/answer/176600?hl=en


小智 0

我使用 libcurl,所以我已经下载了curl-7.49.1 并在目录中

我复制了 imap-fetch.c 来生成 gmail 的特定 imap,然后复制、粘贴并重命名为 imap-fetch-gmail.h

在 imap-fetch-gmail.hi 中设置我的参数:

curl_easy_setopt(curl, CURLOPT_USERNAME, "myemail@gmail.com");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypassword");

/* This will fetch message 1 from the user's inbox */
curl_easy_setopt(curl, CURLOPT_URL,
                 "imaps://imap.gmail.com:993/INBOX/;UID=1");
Run Code Online (Sandbox Code Playgroud)

然后,在curl文件夹中,我调用GCC来使用我的帐户设置(例如电子邮件、密码和参数)编译我的imap-fetch-gmail.h,并使用此命令

gcc imap-fetch-gmail.c -o imap-fetch-gmail -lcurl

在这之后,

首先,您需要在 GMAIL 帐户上启用 IMAP 设置,然后需要启用安全性较低的应用程序来访问您的 Gmail 帐户。如果您不这样做,您就会收到一封安全电子邮件,主题为:“尝试登录被阻止”

您可以通过此链接管理对不太安全的应用程序的访问: https ://www.google.com/settings/security/lesssecureapps

有关 IMAP URL 各个组件的更多信息,请参阅 RFC 5092。