如何使用libcurl和c语言发送带有电子邮件的附件?

时间:2013-02-06 09:57:17

标签: c linux libcurl

我正在使用curl库进行电子邮件发送。 我能够成功发送电子邮件。我可以在接收端正确看到FROM,Subject和body。 现在我需要在电子邮件中附件。 我这样跟着: http://msdn.microsoft.com/en-us/library/ms526560(v=exchg.10).aspx

static const char *text[]={
  "Date: Wed, 06 Feb 2013 12:30:30 +1100\n",
  "To: " RECIPIENT "\n",
  "From: " MAILFROM "\n",
  "MIME-Version: 1.0",
  "Content-Type: multipart/mixed",
  "boundary=""XXXXX""\n",
  "Subject: email example message\n",
  "--XXXXX",
  "\n", /* empty line to divide headers from body, see RFC5322 */
  "The body of the message starts here.\n",
  "Check RFC5322.\n",
  "--XXXXX",
  "Content-Type: text/plain" "\n",
  "Content-Disposition: attachment" "\n",
  "filename="FILE_PATH "\n",    
  "--XXXXX--",
  NULL
};

他们建议使用RFC5322。但在那里我找不到像附件这样的东西。 这是对的吗?使用libcurl + C语言的任何其他方式也受到欢迎。

我认为名叫Jorge的人在2012年10月13日提出了同样的问题。但答案不存在。

4 个答案:

答案 0 :(得分:2)

最简单的方法是使用base64编码进行编码,将二进制数据转换为能够通过电子邮件发送的ASCII字符。 base64编码文件并插入字符串,如下所示。用正确的东西替换帽子里的所有东西。

--XXXXboundary text
Content-Type: application/FILETYPE; name="FILENAME"
Content-Disposition: attachment; filename="FILENAME"
Content-Transfer-Encoding: base64
STRINGFROMBASE64ENCODING

继承了base64上的一个主题:How do I base64 encode (decode) in C?

答案 1 :(得分:0)

我认为你是对的,但为什么你附上了一个空文件?我认为你需要在最后的附件身体公寓里放一些东西。

例如:

--XXXXboundary text 
Content-Type: text/plain;
Content-Disposition: attachment;
        filename="test.txt"

this is the attachment text ---- this is attachment content

--XXXXboundary text--

答案 2 :(得分:0)

也许你应该看看libquickmail(http://sf.net/p/libquickmail/)。 它具有附加文件所需的所有魔力,它使用libcurl进行SMTP传输(甚至在libquickmaillight中没有libcurl)。

答案 3 :(得分:0)

可以按照以下方式完成;

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: Mon, 29 Nov 2010 21:54:29 +1100\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: text/plain; charset=utf-8; format=flowed\r\n",
  "  name=\"send.c\"\r\n",
  "Content-Type: text/plain; charset=utf-8; format=flowed\r\n",
  "Content-Disposition: attachment;\r\n",
  "  filename=\"abc.txt\"\r\n",
  "\r\n",
  "bla bla file content is here\r\n",
  "--------------030203080101020302070708--\r\n",
  NULL
};