How to send an attachment with email using libcurl and c language?

我的梦境 提交于 2019-12-12 14:44:56

问题


I am using curl library for email sending. I am able to send email successfully. I can see FROM, Subject and body properly at receiving end. Now my need is for attachment in the email. I have followed in this way: 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
};

They are suggesting RFC5322. But in that I could not find anything like attachment. Is it correct? Any other way using libcurl + C language is also welcomed.

I think the person named as Jorge has asked same kind of question on october 13,2012. But the answer is not there.


回答1:


The easiest way is to encode using something like base64 encoding which turns binary data into ASCII characters that are able to be sent via email. base64 encode the file and insert the string as below. Replace everything in caps with the correct stuff.

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

Heres a thread on base64: How do I base64 encode (decode) in C?




回答2:


I think you are right, but why did you attached a null file? I think you need put something in the final attachment body apartment.

For example:

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

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

--XXXXboundary text--



回答3:


Maybe you should take a look at libquickmail (http://sf.net/p/libquickmail/). It does all the magic needed to attach files and it uses libcurl for the SMTP transport (or even without libcurl in libquickmaillight).




回答4:


It can be done as below;

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
};


来源:https://stackoverflow.com/questions/14726119/how-to-send-an-attachment-with-email-using-libcurl-and-c-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!