sprintf(m_sendbuff,"%s","\r\n\r\n");
if(senddata()==0)
{
return 0;
}
//发送邮件body
sprintf(m_sendbuff,"%s%s",m_data,"\r\n");
if(senddata()==0)
{
return 0;
}
if(m_attachment!=NULL)
{
char **pp;
for(pp=m_attachment;*pp!=0;pp++)
{
FILE* fp;
char *buf;
//准备发送附件,首先发送分界线,必须!
sprintf(m_sendbuff,"%s%s%s","--",BOUNDARY_TEXT,"\r\n");
if(senddata()==0)
{
return 0;
}
sprintf(m_sendbuff,"%s%s%s","Content-Type: application/x-msdownload; name=\"",strrchr(*pp,'\\')+1,"\"\r\n");
if(senddata()==0)
{
return 0;
}
sprintf(m_sendbuff,"%s","Content-Transfer-Encoding: base64\r\n");
if(senddata()==0)
{
return 0;
}
sprintf(m_sendbuff,"%s%s%s","Content-Disposition: attachment; filename=\"",strrchr(*pp,'\\')+1,"\"\r\n\r\n");
if(senddata()==0)
{
return 0;
}
//将文件base64编码后发送
fp=fopen(*pp,"rb");
if(fp==NULL)
{
m_error=OPEN_FILE_ERROR;
return 0;
}
buf=new char[BUFFERSIZE+1];
memset(buf,0,BUFFERSIZE+1);
int readcount=0;
while(!feof(fp))
{
readcount=fread(buf,1,BUFFERSIZE,fp);
if(readcount>0)
{
*(buf+readcount)='\0';
sprintf(m_sendbuff,"%s%s",base64(buf,ENCODE,readcount),"\r\n");
if(senddata()==0)
{
return 0;
}
memset(buf,0,BUFFERSIZE+1);
}
}
sprintf(m_sendbuff,"%s","\r\n");
if(senddata()==0)
{
return 0;
}
delete[] buf;
fclose(fp);
}
}
if(m_attachment!=NULL)
{
//附件发送完毕,发送分界线
sprintf(m_sendbuff,"%s%s%s","--",BOUNDARY_TEXT,"--\r\n");
if(senddata()==0)
{
return 0;
}
}
//发送.\r\n 结束返回250
sprintf(m_sendbuff,"%s",".\r\n");
if(senddata()==0)
{
return 0;
}
crecv=recv(s,m_recvbuff,MAX_LENGTH,0);
if(strncmp(m_recvbuff,"250",3)!=NULL)
{
m_error=END_DATA_ERROR;
return 0;
}
else
{
showmessage(NULL,"邮件发送成功","",NULL);
}
//退出
sprintf(m_sendbuff,"%s","quit\r\n");
if(senddata()==0)
{
return 0;
}
if(closesocket(s)!=0)
{
m_error=CLOSE_SOCKET_ERROR;
return 0;
}
if(WSACleanup()!=0)
{
m_error=CLEANUP_WSA_ERROR;
return 0;
}
return 1;
}
int CMail::senddata()
{
int idx = 0;
int csend;
int count=strlen(m_sendbuff);
while(count>0)
{
if((csend=send(s,m_sendbuff+idx,count,0))==SOCKET_ERROR)
{
m_error =WSA_SEND_ERROR;
return 0;
}
if(csend==0)
{
break;
}
count-=csend;
idx+=csend;
}
return 1;
}
int CMail::recvdata()
{
int crecv;
crecv=recv(s,m_recvbuff,MAX_LENGTH,0);
if(crecv==SOCKET_ERROR)
{
m_error =WSA_RECV_ERROR;
return 0;
}
else if(crecv==0)
{
m_error=WSA_RECV_ERROR;
return 0;
}
else
{
}
return 1;
}
int CMail::chartoindex(char c)
{
if(c>=65&&c<=90)
{
c-=65;
}
else if(c>=97&&c<=122)
{
c-=71;
}
else if(c>=48&&c<=57)
{
c+=4;
}
else if(c==43)//'+'
{
c=62;
}
else if(c==47)//'/'
{
c=63;
}
else if(c==61)//'='
{
c=0;
}
else
{
m_error=DECODE_ERROR;
return 0;
}
return c;
}
bool CMail::SetMailHeader(char* header)
{
// "Date:","From:","To:",
//"Message-ID:","Subject:","MIME-Version:",
//"Content-Type:","X-Priority:",
//"X-Mailer:","X-CM-TRANSID:","X-CM-SenderInfo:",
//"X-Coremail-Antispam:","\r\n\r\n"
TCHAR szDate[500];
TCHAR sztTime[500];
// Date: <SP> <dd> <SP> <mon> <SP> <yy> <SP> <hh> ":" <mm> ":" <ss> <SP> <zone> <CRLF>
SYSTEMTIME st={0};
::GetSystemTime(&st);
::GetDateFormat(MAKELCID(0x0409,SORT_DEFAULT),0,&st,"ddd\',\' dd MMM yyyy",szDate,sizeof(szDate));
::GetTimeFormat(MAKELCID(0x0409,SORT_DEFAULT),TIME_FORCE24HOURFORMAT,&st,"HH\':\'mm\':\'ss",sztTime,sizeof(sztTime));
sprintf(header,"Date: %s %s\r\n", szDate, sztTime);
// From: <SP> <sender><SP> "<" <sender-email> ">" <CRLF>
if(m_mailfrom!=NULL)
{
strcat(header,"From: ");
strcat(header," <");
推荐阅读
- wwdc苹果开发者大会,苹果开发者大会
- 【专业】浦发银行信用卡能提取现金吗
- 女人梦见掉头发
- 宿州市埇桥区冉玉建材批发部_工商信用信息_经营范围期限状态_法人_地址_注册资本_怎么样
- 西瓜的由来和发展史图片 西瓜的由来和发展史
- 鲁班发明的工具是什么 鲁班发明了什么工具图片?
- 非典是什么时候发生的在中国 非典传到了哪些国家?
- 海底为什么会有火山喷发 海底里面有火山吗?
- 生辰八字解读,2020年二月初二龙抬头理发好不好,还有几天2020龙头节?
- 抖音发财的灯笼码,求抖音发财灯笼码