hope you're all doing well.
I'm trying to make a program that will send an email using Gmail.
I have this command that works perfectly from CMD, but I can't figure out how to make it work in the application.
This is the command: 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
These are the contents of 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!
I followed this example on the cURL wesite
And this is what I got:
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;
}
But it doesn't works, it return a messaged that says "login failed", even though the username and password are right.
Can someone please tell me what I'm doing wrong?
Thanks in advance.
1条答案
按热度按时间iibxawm41#
这项工作一直持续到2022年5月30日:
但从那时起谷歌删除了不安全的登录选项,我正在寻找一个解决方案,因为你...