smtp mime 附件+文本编码发送

3500阅读 0评论2014-01-22 strongerII
分类:LINUX

调了两天,终于调通。
回车换行都会有影响,可以用foxmail,查看邮件源码来调试。

点击(此处)折叠或打开

  1.     fprintf(pFile, "From:%s\n",smtp.szSender);
  2.     fprintf(pFile, "To:%s\n",smtp.szRecver);
  3.     fprintf(pFile, "Subject:%s\n",smtp.szTitle);
  4.     fprintf(pFile, "MIME-Version: 1.0\n");
  5.     fprintf(pFile, "Content-Type:multipart/mixed;\n");                        //添加附件必须!可以看下MIME协议
  6.     fprintf(pFile, " boundary=\"=====001_NextPart167448323667_=====\"\n");    //邮件正文开始

  7.     fprintf(pFile, "\r\n");
  8.     fprintf(pFile, "--=====001_NextPart167448323667_=====\n");                 //段开始标识
  9.     fprintf(pFile, "Content-Type:multipart/alternative;\n");                   //文本和超文本,添加此标识
  10.     fprintf(pFile, " boundary=\"=====002_NextPart167448323667_=====\"\r\n");   //boundary 有开始也要有结束
  11.     
  12.     fprintf(pFile, "--=====002_NextPart167448323667_=====\n");
  13.     fprintf(pFile, "Content-Type: text/plain; charset=\"UTF-8\"\n");
  14.     fprintf(pFile, "Content-Transfer-Encoding: base64\r\n");
  15.     
  16.     sprintf(buf_str,"Hello!\n    This e-mail is come from montion detection of IPNC.\n");

  17.     base64Encode(buf_str,buf_out,strlen(buf_str) + 1,&len_out);
  18.     fprintf(pFile, "%s\r\n",buf_out);
  19.     fprintf(pFile, "--=====002_NextPart167448323667_=====--\r\n");

  20.     
  21.     fprintf(pFile, "--=====001_NextPart167448323667_=====\n");
  22.     fprintf(pFile, "Content-Transfer-Encoding: base64\n");
  23.     fprintf(pFile, "Content-Type:image/jpeg; name=\"%s\"\n",pt);
  24.     fprintf(pFile, "Content-Disposition: attachment; filename=%s\r\n\r\n",pt);
  25.     
  26.     base64Encode_file(in, pFile, ufile_size);

  27.     
  28.     fprintf(pFile, "\n--=====001_NextPart167448323667_=====--\r\n\r\n");    // 邮件结束

上一篇:lib库 编译、使用
下一篇:关于网络通信知识