WSADATA wsa;
CString res,str;
WSAStartup(0x0101,&wsa);
struct sockaddr_in addr;
SOCKET s;
struct hostent* p=gethostbyname(smtp.etang.com);//prase Domain to IP
if(!p){
pc->m_status+="\r\nError get IP.";
pc->GetDlgItem(IDC_STATUS)->SetWindowText(pc->m_status);
goto out;
}
s=socket(AF_INET,SOCK_STREAM,0);
addr.sin_family=AF_INET;
addr.sin_port=htons(25);
addr.sin_addr.s_addr = *(unsigned long*)p->h_addr;
if(connect(s,(struct sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR){
pc->m_status+="\r\nError in connect to smtp server.";
goto out;
}
str="HELO "+pc->m_Server+"\r\n";//Say HELLO to SMTP Server.
if(send(s,(LPCSTR)str,str.GetLength(),0)==SOCKET_ERROR){
pc->m_status+="\r\nError in send HELO command.";
goto out;
}
recv(s,res.GetBufferSetLength(INFOLEN),INFOLEN,0);
pc->m_status+="\r\n"+res;
str="MAIL From: <"+soimeone@etang.com+">\r\n";//Tell the sender of this mail.
if(send(s,(LPCSTR)str,str.GetLength(),0)==SOCKET_ERROR){
pc->m_status+="\r\nError in MAIL FROM command";
goto out;
}
res.Empty();
recv(s,res.GetBufferSetLength(INFOLEN),INFOLEN,0);
pc->m_status+="\r\n"+res;
str="RCPT TO: <"+pc->m_To+">\r\n";//
if(send(s,(LPCSTR)str,str.GetLength(),0)==SOCKET_ERROR){
pc->m_status+="\r\nError in RCPT TO command";
//pc->GetDlgItem(IDC_STATUS)->SetWindowText(pc->m_status);
goto out;
}
res.Empty();
recv(s,res.GetBufferSetLength(INFOLEN),INFOLEN,0);
pc->m_status+="\r\n"+res;
str="DATA\r\n";//Start to transfer mail body.
if(send(s,(LPCSTR)str,str.GetLength(),0)==SOCKET_ERROR){
pc->m_status+="\r\nError in DATA command";
goto out;
}
res.Empty();
recv(s,res.GetBufferSetLength(INFOLEN),INFOLEN,0);
pc->m_status+="\r\n"+res;
if(send(s,(LPCSTR)pc->m_Content,pc->m_Content.GetLength(),0)==SOCKET_ERROR){
pc->m_status+="\r\nError in send mail body";
goto out;
}