gpt4 book ai didi

c++ - 将字符串添加到文件缓冲区,用于 cURL smtp 发送电子邮件的消息正文

转载 作者:太空狗 更新时间:2023-10-29 12:41:07 24 4
gpt4 key购买 nike

我正在尝试修改有效的 cURL 电子邮件发送示例以添加邮件正文。我不确定为什么我发现的所有带有附件的 curl 电子邮件示例都没有邮件正文。

我需要发送带有 PDF 文件附件的简短文本电子邮件。

这是我到目前为止尝试过的,没有注释的行,它编译并运行,但无法发送。我知道消息正文应该与主题分开一个“\r\n”(空行),但这不是正确的方法。

//Create structure of email to be sent
fileBuf = new char[ADD_SIZE + no_of_rows][CHARS]; //ADD_SIZE for TO,FROM,SUBJECT,CONTENT-TYPE,CONTENT-TRANSFER-
//ENCODING,CONETNT-DISPOSITION and \r\n
strcpy(fileBuf[len++],"To: " TO "\r\n");
buffer_size += strlen(fileBuf[len-1]);
strcpy(fileBuf[len++],"From: " FROM "\r\n");
buffer_size += strlen(fileBuf[len-1]);
strcpy(fileBuf[len++],"Subject: SMTP TLS example message\r\n");
buffer_size += strlen(fileBuf[len-1]);

//strcpy(fileBuf[len++],"\r\n");
//buffer_size += strlen(fileBuf[len-1]);
//strcpy(fileBuf[len++],"Message goes here, hopefully...\r\n");
//buffer_size += strlen(fileBuf[len-1]);

strcpy(fileBuf[len++],"Content-Type: application/x-msdownload; name=\"" FILENAME "\"\r\n");
buffer_size += strlen(fileBuf[len-1]);
strcpy(fileBuf[len++],"Content-Transfer-Encoding: base64\r\n");
buffer_size += strlen(fileBuf[len-1]);
strcpy(fileBuf[len++],"Content-Disposition: attachment; filename=\"" FILENAME "\"\r\n");
buffer_size += strlen(fileBuf[len-1]);
strcpy(fileBuf[len++],"\r\n");
buffer_size += strlen(fileBuf[len-1]);

完整的项目代码是here请参阅解决方案 7。

如有任何关于如何实现这一点的建议,我们将不胜感激。

[编辑] 使用非常简单的 cURL 进行测试,生成 0 字节附件:

#define FILENAME  "Rpt05162017.pdf"
static const char *payload_text[] = {
"To: " TO "\r\n",
"From: " FROM "(Example User)\r\n",
//"Cc: " CC "(Another example User)\r\n",
"Subject: SMTPS Example\r\n",
"Date: 17-May-2017\r\n",
"User-Agent: My eMail Client\r\n",
"MIME-Version: 1.0\r\n",
"Content-Type: multipart/mixed;\r\n",
" boundary=\"------------030203080101020302070708\"\r\n",
"\r\nThis is a multi-part message in MIME format.\r\n",
"--------------030203080101020302070708\r\n",
"Content-Type: text/plain; charset=utf-8; format=flowed\r\n",
"Content-Transfer-Encoding: 7bit\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\r\n",
"--------------030203080101020302070708\r\n",
"Content-Type: application/x-msdownload; name=\"" FILENAME "\"\r\n",
"Content-Transfer-Encoding: base64\r\n",
"Content-Disposition: attachment; filename=\"" FILENAME "\"\r\n",
"\r\n--------------030203080101020302070708--",
NULL
};

最佳答案

目前尚不清楚您是如何发送所有这些内容的。您创建了字符串的二维数组,并将 strcpy 放入每个标题中。它们都有填充字节,会破坏您的消息。

也许可以尝试简化它?

std:string header =
"To: " TO "\r\n"
"From: " FROM "\r\n"
"Subject: SMTP TLS example message\r\n"
"Content-Type: application/x-msdownload; name=\"" FILENAME "\"\r\n"
"Content-Transfer-Encoding: base64\r\n"
"Content-Disposition: attachment; filename=\"" FILENAME "\"\r\n"
"\r\n";

然后只需发送您的消息 header,然后发送消息的 body


使用更新后的 payload_text,您收到 0 大小的附件并不奇怪,因为您发送的是空文件。在您发送您的 payload_text 之后,您必须发送您的文件正文,然后在正文之后您需要附加多部分结束后缀:

"\r\n--------------030203080101020302070708--"

关于c++ - 将字符串添加到文件缓冲区,用于 cURL smtp 发送电子邮件的消息正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44081632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com