使用C和curl库发送电子邮件,不起作用

问题描述 投票:0回答:1

我需要使用 C 发送电子邮件,我正在尝试使用curl 库来做到这一点。但是,当尝试运行该程序时,它什么也不做,只是在控制台中返回此消息:

进程已完成,退出代码为 -1073741515 (0xC0000135)

如果您尝试使用调试模式,它会忽略所有断点并以相同的方式结束程序,但现在代码为-1。

如果您没有使用curl,程序可以正常运行。

这是我的代码:

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

#define FROM "[email protected]"
#define TO  "password"

static const char *headers_text[] = {
        "Date: Tue, 22 Aug 2017 14:08:43 +0100",
        "To: " TO,
        "From: " FROM " (Example User)",
        "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
        "rfcpedant.example.org>",
        "Subject: example sending a MIME-formatted message",
        NULL
};

static const char inline_text[] =
        "This is the inline text message of the email.\r\n"
        "\r\n"
        "  It could be a lot of lines that would be displayed in an email\r\n"
        "viewer that is not able to handle HTML.\r\n";

char * send_email(){

    CURL *curl;
    CURLcode res = CURLE_OK;

    curl = curl_easy_init();
    if(curl) {
        struct curl_slist *headers = NULL;
        struct curl_slist *recipients = NULL;
        struct curl_slist *slist = NULL;
        curl_mime *mime;
        curl_mime *alt;
        curl_mimepart *part;
        const char **cpp;

        /* This is the URL for your mailserver */
        curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.gmail.com:465");
        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_MAIL_RCPT_ALLOWFAILS, 1L);
        for(cpp = headers_text; *cpp; cpp++)
            headers = curl_slist_append(headers, *cpp);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        mime = curl_mime_init(curl);
        alt = curl_mime_init(curl);

        part = curl_mime_addpart(alt);
        curl_mime_data(part, inline_text, CURL_ZERO_TERMINATED);

        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_slist_free_all(headers);

        curl_easy_cleanup(curl);
        curl_mime_free(mime);

    }
    return "ok";
}

我的 CMakeList.txt 如下:

cmake_minimum_required(VERSION 3.26)
project(aplicacao_back_end C)

set(CMAKE_C_STANDARD 11)


include_directories(lib\\libmicrohttpd\\include)
link_directories(lib\\libmicrohttpd\\lib)

set(CURL_LIBRARY "-lcurl")
include_directories(lib\\libcurl\\include)
link_directories(lib\\libcurl\\lib)
find_package(CURL)

add_executable(aplicacao_back_end main.c
        scripts/email.c
        scripts/email.h
)

target_link_libraries(aplicacao_back_end microhttpd ws2_32 curl)

我试图找出错误,显然问题发生在

curl = curl_easy_init();
行之后。

我使用的是CLion环境。我的文件夹结构如下:

.想法 cmake 构建调试 CMakeLists.txt 控制器 库

  • libcurl
    • 包括
    • lib
  • libmicrohttpd
  • sqlite3.c
  • sqlite3.h
  • sqlite3.lib 主程序 脚本
  • 电子邮件.c
  • 电子邮件.h
c cmake libcurl
1个回答
0
投票

我遇到了问题,因为库安装不正确。

我按照以下教程进行操作,它解决了我的问题:

https://www.youtube.com/watch?v=ho9f0WiA-1o&t=897s

© www.soinside.com 2019 - 2024. All rights reserved.