C++程序发送邮件附件中的图片无法显示,该如何解决
C++程序发送邮件附件中的图片无法显示
最近做一个程序要使用邮件发送功能传输图片,就在网上找了一个代码,代码链接是http://www.it165.net/pro/html/201407/16929.html,这个可以传输文本文件,但是传输BMP或JPG等图片时,就无法正常显示,求各位大神指点。附件传输部分代码如下
------解决思路----------------------
最近做一个程序要使用邮件发送功能传输图片,就在网上找了一个代码,代码链接是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;
}
------解决思路----------------------
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;
}