最近做一个程序要使用邮件发送功能传输图片,就在网上找了一个代码,代码链接是http://www.it165.net/pro/html/201407/16929.html,这个可以传输文本文件,但是传输BMP或JPG等图片时,就无法正常显示,求各位大神指点。附件传输部分代码如下 bool CSendMail::SendFileBody(SOCKET &sock) { char* file_path; int rt; int len; char file_name[128]; int pt = 0; int dest_length = 0; for (int index = 0; index < file_path_list_.size(); index++) { pt = 0; memset(file_name, 0, 128); file_path = file_path_list_[index]; len = GetFileData(file_path); GetFileName(file_name, file_path); sprintf_s(send_buff_, "--INVT\r\nContent-Type: application/octet-stream;\r\n name="%s"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment;\r\n filename="%s"\r\n\r\n", file_name, file_name); send(sock, send_buff_, strlen(send_buff_), 0); while (pt < len) { memset(send_buff_, 0, sizeof(send_buff_)); // 760为1024的最大整数,(760/4)*3=570 dest_length = 4096; Base64Encode((BYTE*)file_buffer_ + pt, min(len - pt, 570), send_buff_, &dest_length); int len1 = strlen(send_buff_); pt += min(len - pt, 570); if (pt == len) { send_buff_[len1] = ""\r""; send_buff_[len1 + 1] = ""\n""; send_buff_[len1 + 2] = ""\0""; } else send_buff_[len1] = ""\0""; rt = send(sock, send_buff_, strlen(send_buff_), 0); if (rt != strlen(send_buff_)) { return false; } } if (len != 0) { delete[] file_buffer_; } } return true; } |
|
不能分段base64编码,应该将整个文件一次base64编码
|
|
您能说一下具体怎么改吗?我对这部分不是太懂 |
|
60分 |
bool CSendMail::SendFileBody(SOCKET &sock) { char* file_path; int rt; int len; char file_name[128]; int pt = 0; int dest_length = 0; for (int index = 0; index < file_path_list_.size(); index++) { pt = 0; memset(file_name, 0, 128); file_path = file_path_list_[index]; len = GetFileData(file_path); GetFileName(file_name, file_path); sprintf_s(send_buff_, "--INVT\r\nContent-Type: application/octet-stream;\r\n name="%s"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment;\r\n filename="%s"\r\n\r\n", file_name, file_name); send(sock, send_buff_, strlen(send_buff_), 0); while (pt < len) { memset(send_buff_, 0, sizeof(send_buff_)); // 760为1024的最大整数,(760/4)*3=570 dest_length = 4096; Base64Encode((BYTE*)file_buffer_ + pt, min(len - pt, 570), send_buff_, &dest_length); int len1 = strlen(send_buff_); pt += min(len - pt, 570); if (pt == len) { send_buff_[len1] = ""\r""; send_buff_[len1 + 1] = ""\n""; send_buff_[len1 + 2] = ""\0""; } else send_buff_[len1] = ""\0""; rt = send(sock, send_buff_, strlen(send_buff_), 0); if (rt != strlen(send_buff_)) { return false; } } // if (len != 0) // { // delete[] file_buffer_; // } } return true; } |
|
|
你用一个8×8像素的jpg测试。
|
|
8X8的可以,但是传大图该怎么办呢
|
|
bool CSendMail::SendFileBody(SOCKET &sock) { char *file_path; int rt; int len; char file_name[256]; int dest_length; BYTE *file_buffer_base64; int index,len1; for (index = 0; index < file_path_list_.size(); index++) { file_path = file_path_list_[index]; len = GetFileData(file_path); GetFileName(file_name, file_path); sprintf_s(send_buff_, "--INVT\r\nContent-Type: application/octet-stream;\r\n name="%s"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment;\r\n filename="%s"\r\n\r\n", file_name, file_name); send(sock, send_buff_, strlen(send_buff_), 0); file_buffer_base64=new BYTE[len*4/3+256]; Base64Encode((BYTE*)file_buffer_ , len, file_buffer_base64, &dest_length); len1 = strlen(file_buffer_base64); file_buffer_base64[len1 ] = ""\r""; file_buffer_base64[len1 + 1] = ""\n""; file_buffer_base64[len1 + 2] = ""\0""; rt = send(sock, file_buffer_base64, strlen(file_buffer_base64), 0); delete [] file_buffer_base64; if (rt != strlen(file_buffer_base64)) { return false; } } return true; } |
|
参数类型不对不能运行,file_buffer_base64是BYTE*, Base64Encode((BYTE*)file_buffer_ , len, file_buffer_base64, &dest_length);中第二个参数要求是LPSTR类型,len1 = strlen(file_buffer_base64)中的参数是const char *类型。
|
|
Base64Encode((BYTE*)file_buffer_ , len, (LPSTR)file_buffer_base64, &dest_length);
en1 = strlen((const char *)file_buffer_base64) |
|
报错了。。。
|
|
顶上去
|
|
VS IDE中,在符号Base64Encode上点鼠标右键,选转到定义。看看该函数各参数的定义和调用是否一致。
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 |