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

如何解决CURL电子邮件发送出现附件问题

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

使用此示例
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#define FROM    "<Kuzka30z@yandex.ru>"
#define TO      "<mihedovkos@gmail.com>"
#define CC      "<info@example.org>"
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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-