2016-05-07 31 views
1

我知道那里有数百个帖子,但不知何故,这对我不起作用。我正尝试用libcurl发送一封电子邮件。这是我的代码:用gmail发送带有libcurl/smtp的电子邮件:登录被拒绝

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

#define FROM "<[email protected]>" 
#define TO  "<[email protected]>" 
#define CC  "<[email protected]>" 

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: <[email protected]" 
    "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, "[email protected]"); 
    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; 
} 

我也试过这种example这是非常相似,除了文本获得通过不同的文件管道。

但我总是得到

curl_easy_perform()失败:拒绝登录

,然后Gmail中向我发送一条消息,说有人试图侵入我的帐户。 (登录凭据是正确的)

curl-config --feature

给我

SSL的IPv6 UnixSockets libz进行NTLM NTLM_WB

阅读使用smtp://smtp.live.com:465

一对夫妇的相关博客文章中我也尝试过使用Hotmail

但是我在这里得到

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

什么我错在这里做什么?

回答

0

我是使用libcurl的,所以我一直在下载的卷曲7.49.1和目录

里面我复制了IMAP-fetch.c产生一个Gmail的IMAP特定的,比我复制,粘贴并重新命名为IMAP取入gmail.h

的IMAP取入gmail.hi内设置参数我在这行:

curl_easy_setopt(curl, CURLOPT_USERNAME, "myem[email protected]"); 
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"); 

然后,卷曲文件夹内我打电话GCC编译我的IMAP -fetch-gmail.h与我的帐户设置,如电子邮件,密码和参数使用此命令

GCC IMAP取入gmail.c -o IMAP取Gmail邮箱-lcurl

在此之后,

首先,您需要启用您的Gmail帐户的IMAP设置,那么你需要启用安全性较低的应用访问您的Gmail帐户。如果你不这样做,你已经receice与主题的安全电子邮件:“尝试登录防止”

您可以管理存取安全性较低的应用程序此链接: https://www.google.com/settings/security/lesssecureapps

有关的更多信息IMAP URL的单个组件请参阅RFC 5092.

+0

听起来很糟糕... – Kusalananda

2

我不得不花费大量时间才能使这个工作正常,但我终于找到了工作。这里是我的发现:

第一:

#define FROM "<[email protected]>" 
#define TO  "<[email protected]>" 
#define CC  "<[email protected]>" 

不应有任何 “<” 或 “>” 中的电子邮件地址。我犯了重用定义的错误,并且无法理解为什么登录失败。

二:

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

三:

如果您正在使用端口587,你将需要以下line:

curl_easy_setopt(m_curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); 

This does似乎并不需要为465

注:

这是确定为使用端口465或587 465需要SSL,所以你需要,而不是使用SMTP的SMTPS来指定这个 - smtps://smtp.gmail.com:465。我在这里找到了一些有关它的信息:https://support.google.com/a/answer/176600?hl=en