int CSmtp::SendAttachment_Ex() /*发送附件*/
{
for (list<FILEINFO *>::iterator pIter = listFile.begin(); pIter != listFile.end(); pIter++)
{
CString sendBuff;
sendBuff = "--qwertyuiop\r\n";
sendBuff += "Content-Type: application/octet-stream;\r\n";
sendBuff += " name=\"";
sendBuff += (*pIter)->fileName;
sendBuff += "\"";
sendBuff += "\r\n";
sendBuff += "Content-Transfer-Encoding: base64\r\n";
sendBuff += "Content-Disposition: attachment;\r\n";
sendBuff += " filename=\"";
sendBuff += (*pIter)->fileName;
sendBuff += "\"";
sendBuff += "\r\n";
sendBuff += "\r\n";
Send(sendBuff);
ifstream ifs((*pIter)->filePath,ios::in | ios::binary);
if (FALSE == ifs.is_open())
{
return 4; /*错误码4表示文件打开错误*/
}
char fileBuff[MAX_FILE_LEN];
char *chSendBuff;
memset(fileBuff, 0, sizeof(fileBuff));
/*文件使用base64加密传送*/
while (ifs.read(fileBuff, MAX_FILE_LEN))
{
//cout << ifs.gcount() << endl;
chSendBuff = base64Encode(fileBuff, MAX_FILE_LEN);
chSendBuff[strlen(chSendBuff)] = '\r';
chSendBuff[strlen(chSendBuff)] = '\n';
send(sockClient, chSendBuff, strlen(chSendBuff), 0);
delete[]chSendBuff;
}
//cout << ifs.gcount() << endl;
chSendBuff = base64Encode(fileBuff, ifs.gcount());
chSendBuff[strlen(chSendBuff)] = '\r';
chSendBuff[strlen(chSendBuff)] = '\n';
int err = send(sockClient, chSendBuff, strlen(chSendBuff), 0);
if (err != strlen(chSendBuff))
{
cout << "文件传送出错!" << endl;
return 1;
}
delete []chSendBuff;
}
return 0;
}
上边是发送附件源码,但是结果在163邮箱里 附件却以正文的形式显示,并且乱码了