CURL电子邮件发送出现附件问题

问题描述

我尝试使用libcurl发送电子邮件。我只能发送带有文本的电子邮件,但是尝试使用附件发送电子邮件时遇到麻烦。我从官方网站https://curl.haxx.se/libcurl/c/smtp-mime.html

使用此示例
#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 *headers_text[] = {
  "Date: Tue,22 Aug 2017 14:08:43 +0100","To: " TO,"From: " FROM " (Example User)","Cc: " CC " (Another example User)","Message-ID: <dcd7cb36-16gb-487a-9f3a-e652a9458efd@"
    "rfcpedant.example.org>",/// I change ID every sending
  "Subject: example sending a MIME-formatted message",NULL
};
static const char inline_text[] =
  "This is the inline text message of the e-mail.\r\n"
  "\r\n"
  "  It Could be a lot of lines that would be displayed in an     e-mail\r\n"
  "viewer that is not able to handle HTML.\r\n";
static const char inline_html[] =
  "<html><body>\r\n"
  "<p>This is the inline <b>HTML</b> message of the e-mail.</p>"
  "<br />\r\n"
  "<p>It Could be a lot of HTML data that would be displayed by "
  "e-mail viewers able to handle HTML.</p>"
  "</body></html>\r\n";

int main() {
  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;
    curl_easy_setopt(curl,CURLOPT_URL,"smtps://smtp.yandex.ru");
    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_USERNAME,"Kuzka30z"); ///Yandex SMTP need another login,than email
    curl_easy_setopt(curl,CURLOPT_PASSWORD,"Password"); /// This password unreal
    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_html,CURL_ZERO_TERMINATED);
    curl_mime_type(part,"text/html")
    part = curl_mime_addpart(alt);
    curl_mime_data(part,inline_text,CURL_ZERO_TERMINATED);
    part = curl_mime_addpart(mime);
    curl_mime_subparts(part,alt);
    curl_mime_type(part,"image/jpeg"); ///O try to chnge mime and commented this line
    slist = curl_slist_append(NULL,"Content-disposition: inline"); ///This line I tried comment too
    curl_mime_headers(part,slist,1);  
    part = curl_mime_addpart(mime);
    curl_mime_filedata(part,"/home/konstantimp/Pictures/Background/akatsiia_listia_kust_181905_1280x720.jpg");
    curl_easy_setopt(curl,CURLOPT_MIMEPOST,mime);
    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 0;
}

它可以工作,但是如果我发送带有'\ n'新行符号的文件,它将在“ \ r \ n”上更改。我以为是错误的mime类型,但是我先更改了它(它不起作用),然后用设置类型(curl_mime_type(part,“ multipart / alternative”);)注释了行。它也不起作用。

然后我尝试发送图像文件。我发送了.jpeg和.gif,但它们发送不正确...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)