strcat(header,m_mailfrom);
strcat(header, ">\r\n");
}
// X-Mailer: <SP> <xmailer-app> <CRLF>
if(m_xmailer!= NULL)
{
strcat(header,"X-Mailer: ");
strcat(header, m_xmailer);
strcat(header, "\r\n");
}
// X-Priority: <SP> <number> <CRLF>
switch(m_priority)
{
case XPRIORITY_HIGH:
strcat(header,"X-Priority: 2 (High)\r\n");
break;
case XPRIORITY_NORMAL:
strcat(header,"X-Priority: 3 (Normal)\r\n");
break;
case XPRIORITY_LOW:
strcat(header,"X-Priority: 4 (Low)\r\n");
break;
default:
strcat(header,"X-Priority: 3 (Normal)\r\n");
}
// To: <SP> <remote-user-mail> <CRLF>
if(m_rcptto!=NULL)
{
strcat(header,"To: ");
strcat(header, m_rcptto);
strcat(header, "\r\n");
}
// Subject: <SP> <subject-text> <CRLF>
if(m_subject!=NULL)
{
strcat(header, "Subject: ");
strcat(header, m_subject);
strcat(header, "\r\n");
}
// MIME-Version: <SP> 1.0 <CRLF>
strcat(header,"MIME-Version: 1.0\r\n");
//attachment
if(m_attachment==NULL)
{ //如果没附件
strcat(header,"Content-type: text/plain; charset=GB2312\r\n");
strcat(header,"Content-Transfer-Encoding: 7bit\r\n");
strcat(header,"\r\n");
}
else
{ //如果有附件,设置分界线,每个子段以--BOUNDARY_TEXT行开始,父段则以--BOUNDARY_TEXT--行结束,不同段之间用空行分隔
strcat(header,"Content-Type: multipart/mixed; boundary=\"");
strcat(header,BOUNDARY_TEXT);
strcat(header,"\"\r\n");
strcat(header,"\r\n");//这个空行不能少
//第一部分为文本内容
strcat(header,"--");strcat(header,BOUNDARY_TEXT);strcat(header,"\r\n");//加上分界线
//以下跟没附件时的一样
strcat(header,"Content-type: text/plain; charset=GB2312\r\n");//注意GB2312 否则会出现中文乱码
strcat(header,"Content-Transfer-Encoding: 7bit\r\n");
}
// done
return true;
}
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::smtplogin()
{
//调用成员函数connect
if(connect()==0)
{
return 0;
}
if(recvdata()==0)
{
return 0;
}
if(strstr(m_recvbuff,"220")==NULL)//220
{
m_error=UNKNOWEN_ERROR;
return 0;
}
//发送helo 返回250
sprintf(m_sendbuff,"%s","helo 543880135\r\n");
if(senddata()==0)
{
return 0;
}
if(recvdata()==0)
{
return 0;
}
if(strncmp(m_recvbuff,"250",3)!=NULL)
{
m_error=HELO_ERROR;
return 0;
}
//发送auth login 返回334
sprintf(m_sendbuff,"%s","auth login\r\n");
if(senddata()==0)
{
return 0;
}
if(recvdata()==0)
{
return 0;
}
if(strncmp(m_recvbuff,"334",3)!=NULL)
{
m_error=AUTH_LOGIN_ERROR;
return 0;
}
//发送经过base64编码后的用户名,返回334
sprintf(m_sendbuff,"%s%s",base64(m_user,ENCODE,strlen(m_user)),"\r\n");
if(senddata()==0)
{
return 0;
}
if(recvdata()==0)
{
return 0;
}
if(strncmp(m_recvbuff,"334",3)!=NULL)
{
m_error=USER_ERROR;
return 0;
}
//发送经过base64编码后的密码 返回235
sprintf(m_sendbuff,"%s%s",base64(m_pass,ENCODE,strlen(m_pass)),"\r\n");
if(senddata()==0)
{
return 0;
}
if(recvdata()==0)
{
return 0;
}
if(strncmp(m_recvbuff,"235",3)!=NULL)
{
m_error=PASS_ERROR;
return 0;
}
return 1;
}
int CMail::connect()
{
WSADATA wsadata;
struct sockaddr_in sock_addr;
HOSTENT *phostent;
struct in_addr inaddr;
if(WSAStartup(MAKEWORD(1,1),&wsadata)<0)
{
m_error=WSA_INIT_ERROR;
return 0;
}
/*---------------根据域名获取网络地址,存放在struct in_addr中----------------*/
phostent=gethostbyname(m_server);//pop.163.com
if(phostent!=NULL)
{
// lpAddr=phostent->h_addr_list[0];
memmove(&inaddr,phostent->h_addr_list[0],4);//220.181.12.101
推荐阅读
- wwdc苹果开发者大会,苹果开发者大会
- 【专业】浦发银行信用卡能提取现金吗
- 女人梦见掉头发
- 宿州市埇桥区冉玉建材批发部_工商信用信息_经营范围期限状态_法人_地址_注册资本_怎么样
- 西瓜的由来和发展史图片 西瓜的由来和发展史
- 鲁班发明的工具是什么 鲁班发明了什么工具图片?
- 非典是什么时候发生的在中国 非典传到了哪些国家?
- 海底为什么会有火山喷发 海底里面有火山吗?
- 生辰八字解读,2020年二月初二龙抬头理发好不好,还有几天2020龙头节?
- 抖音发财的灯笼码,求抖音发财灯笼码