I was able to overcome the error. Yes there was some memory leak and buffer was overrunning, I corrected that. Now I'm able to send email but getting following output after sending mail.
* Connection #0 to host smtp.gmail.com left intact > QUIT < 221 2.0.0 closing connection iu7sm77803912pbc.45 - gsmtp * Closing connection #0 Is it some kind of error or what? I'm using curl-7.28.1 I've compiled it myself in visual studio 2010 And my code is in the attachment. On Tue, Feb 4, 2014 at 6:29 PM, Steve Holme <[email protected]> wrote: > On Tue, 4 Feb 2014, ansh kumar wrote: > > > I'm successfully able to send email using libcurl from visual > > c++ (in debug mode), but when I use release mode I'm getting > > following error: > > How does that output compare to that generated when you run against the > debug build? > > > Any suggestion?? > > It kind of feels like it is the result of an uninitialised variable > somewhere. > > Can you give us some more information please, such as: > > * Which version of libcurl you are using > * Did you compile libcurl yourself? > * A list the curl_easy_setopt() calls you are using prior to invoking > libcurl > > Kind Regards > > Steve > ------------------------------------------------------------------- > List admin: http://cool.haxx.se/list/listinfo/curl-library > Etiquette: http://curl.haxx.se/mail/etiquette.html >
#include <stdio.h> #include <string.h> #include <curl/curl.h> #include <string> #include <cassert> #include <limits> #include <stdexcept> #include <cctype> #include <cmath> #define FROM "<[email protected]>" #define TO "<[email protected]>" #define FILENAME "Hello.txt" static const int CHARS= 200; //Sending 54 chararcters at a time with \r , \n and \0 it becomes 57 static const int ADD_SIZE= 7; // ADD_SIZE for TO,FROM,SUBJECT,CONTENT-TYPE,CONTENT-TRANSFER-ENCODING,CONETNT-DISPOSITION and \r\n static const int SEND_BUF_SIZE= 54; static char (*fileBuf)[CHARS] = NULL; static const char b64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char reverse_table[128] = { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64 }; std::string base64_encode(const ::std::string &bindata) { using ::std::string; using ::std::numeric_limits; // Maximum string size = 3221225469 //if (bindata.size() > (numeric_limits<string::size_type xmlns:string="#unknown">::max() / 4u) * 3u) //{ // throw ::std::length_error("Converting too large a string to base64."); //} const ::std::size_t binlen = bindata.size(); // Use = signs so the end is properly padded. string retval((((binlen + 2) / 3) * 4), '='); ::std::size_t outpos = 0; int bits_collected = 0; unsigned int accumulator = 0; const string::const_iterator binend = bindata.end(); for (string::const_iterator i = bindata.begin(); i != binend; ++i) { accumulator = (accumulator << 8) | (*i & 0xffu); bits_collected += 8; while (bits_collected >= 6) { bits_collected -= 6; retval[outpos++] = b64_table[(accumulator >> bits_collected) & 0x3fu]; } } if (bits_collected > 0) { // Any trailing bits that are missing. assert(bits_collected < 6); accumulator <<= 6 - bits_collected; retval[outpos++] = b64_table[accumulator & 0x3fu]; } assert(outpos >= (retval.size() - 2)); assert(outpos <= retval.size()); return retval; } struct fileBuf_upload_status { int lines_read; }; size_t read_file() { FILE* hFile=NULL; size_t fileSize(0),len(0),buffer_size(0); //opening the file hFile = fopen(FILENAME,"rb"); if (hFile == NULL) throw "File Does not exist."; //get file size fseek(hFile,0,SEEK_END); fileSize = ftell(hFile); fseek(hFile,0,SEEK_SET); //Checking file size if(fileSize > 1*1024) {} int no_of_rows = fileSize/SEND_BUF_SIZE + 1; int read(0); fileBuf = new char[ADD_SIZE + no_of_rows + 1][CHARS]; //Extra row for our special character to be used in conditional statements,here "" // 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]) + 1; // 1 for \0 strcpy(fileBuf[len++],"From: " FROM "\r\n"); buffer_size += strlen(fileBuf[len-1]) + 1; strcpy(fileBuf[len++],"Subject: SMTP TLS example message\r\n"); buffer_size += strlen(fileBuf[len-1]) + 1; strcpy(fileBuf[len++],"Content-Type: application/x-msdownload; name=\"" FILENAME "\"\r\n"); buffer_size += strlen(fileBuf[len-1]) + 1; strcpy(fileBuf[len++],"Content-Transfer-Encoding: base64\r\n"); buffer_size += strlen(fileBuf[len-1]) + 1; strcpy(fileBuf[len++],"Content-Disposition: attachment; filename=\"" FILENAME "\"\r\n"); buffer_size += strlen(fileBuf[len-1]) + 1; strcpy(fileBuf[len++],"\r\n"); buffer_size += strlen(fileBuf[len-1]) + 1; char* temp_buf = new char[SEND_BUF_SIZE + 4]; //taking extra size of 4 bytes std::string encodedStr, temp_buf_str; for (; len < no_of_rows + ADD_SIZE; ++len) { read = fread(temp_buf,sizeof(char),SEND_BUF_SIZE,hFile); temp_buf[read] ='\0'; temp_buf_str = std::string(temp_buf); encodedStr = base64_encode(temp_buf_str); encodedStr += "\r\n"; memcpy(fileBuf[len],encodedStr.c_str(),encodedStr.size()+1); buffer_size += encodedStr.size() + 1; // 1 for \0 } strcpy(fileBuf[len],""); delete[] temp_buf; return buffer_size; } static size_t fileBuf_source(void *ptr, size_t size, size_t nmemb, void *userp) { struct fileBuf_upload_status *upload_ctx = (struct fileBuf_upload_status *)userp; const char *fdata; if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) { return 0; } fdata = fileBuf[upload_ctx->lines_read]; if(strcmp(fdata,"")) { size_t len = strlen(fdata); memcpy(ptr, fdata, len); upload_ctx->lines_read++; return len; } return 0; } int main(void) { CURL *curl; CURLcode res = CURLE_OK; struct curl_slist *recipients = NULL; struct fileBuf_upload_status file_upload_ctx; size_t file_size(0); file_upload_ctx.lines_read = 0; curl = curl_easy_init(); file_size = read_file(); if(curl) { curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxx"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxx"); curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587"); curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); curl_easy_setopt(curl, CURLOPT_CAINFO, "cacert.pem"); curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM); recipients = curl_slist_append(recipients, TO); curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); curl_easy_setopt(curl, CURLOPT_INFILESIZE, file_size); curl_easy_setopt(curl, CURLOPT_READFUNCTION, fileBuf_source); curl_easy_setopt(curl, CURLOPT_READDATA, &file_upload_ctx); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 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_easy_cleanup(curl); } delete[] fileBuf; system("pause"); return (int)res; }
------------------------------------------------------------------- List admin: http://cool.haxx.se/list/listinfo/curl-library Etiquette: http://curl.haxx.se/mail/etiquette.html
