发送部分:
void CMyDlg::OnSendmail()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
char buf[4];//定义缓冲区
CString data="Data:Tue,04,June 2012.5.17,21:15:01+0800\r\n";//构造发送字符串
CString sender="MAIL FROM:";
CString recver="RCPT TO:";
CString subject="Subject:";
CString l,r,sl,mailtext;
GetDlgItem(IDC_SENDER)->GetWindowText(l);//获取控件内容
GetDlgItem(IDC_RECVER)->GetWindowText(r);
GetDlgItem(IDC_SUBJECT)->GetWindowText(sl);
GetDlgItem(IDC_MAILTEXT)->GetWindowText(mailtext);
sender+=l;//添加获取内容
recver+=r;
subject+=sl;
CString sendmail[8];
sendmail[0]="HELO";//构造发送数组
sendmail[1]=*sender.GetBuffer(1);
sendmail[2]=*recver.GetBuffer(1);
sendmail[3]="DATA\r\n";//发送DATA命令
sendmail[4]=*subject.GetBuffer(1);
sendmail[5]=*mailtext.GetBuffer(1);
sendmail[6]="QUIT\r\n";//退出会话
sendmail[7]= "\0";
send(s,*sendmail,sizeof(sendmail),0);
recv(s,buf,4,0);//接受响应数据
if(buf!=NULL)//是否接收到数据
{
if ( (atoi)(buf)==250 )
::SendMessage(statu,SB_SETTEXT,0,(long)"邮件发送成功!");
else
::SendMessage(statu,SB_SETTEXT,0,(long)"邮件发送失败!");
}
else
{::SendMessage(statu,SB_SETTEXT,0,(long)"邮件正在发送");}
}
接收部分:
void CRecv::OnRecv()
{
// TODO: Add your control notification handler code here
sockaddr_in addr;
addr.sin_family=AF_INET;//为地址结构中的成员赋值
CSet set;
addr.sin_port=htons(set.m_port);
//host=::gethostbyname(set.m_severadd.GetBuffer(1));//获取主机地址
addr.sin_addr.S_un.S_addr=inet_addr(set.m_severadd.GetBuffer(1));//转换IP地址
int s;
s=::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//创建套接字
if(connect(s,(sockaddr*)&addr,sizeof(addr)))//连接服务器
statu=::CreateStatusWindow(WS_CHILD|WS_VISIBLE,"",this->m_hWnd,1008);
::SendMessage(statu,SB_SETTEXT,0,(long)"构造请求命令!");
CString str,strl;//定义字符串
GetDlgItem(IDC_ZHANGHU)->GetWindowText(name);//获取用户名
GetDlgItem(IDC_PASS)->GetWindowText(pass);//获取用户密码
str.Format("USER &s",name);//格式化用户名命令字符串
str+="\r\n";//添加回车换行符
str+=strl;//连接两个字符串
::SendMessage(statu,SB_SETTEXT,0,(long)"正在发送请求命令!");//提示用户正在发送命令
send(s,str.GetBuffer(1),sizeof(str),0);//发送命令字符串
char recvbuffer[100]={0};//定义字符数组用于接收数据
if(recv(s,recvbuffer,100,0))//接收数据
{
if (recvbuffer[0]=='O'&&recvbuffer[1]=='K')//服务器应答成功
{
::SendMessage(statu,SB_SETTEXT,0,(long)"服务器应答成功!");
SendCmdAndRecv(0);//调用自定义函数进行命令发送
}
else//接收失败
{::SendMessage(statu,SB_SETTEXT,0,(long)"接收失败!");
}
}
else//连接失败
{::SendMessage(statu,SB_SETTEXT,0,(long)"连接失败!");
}
}
void CRecv::SendCmdAndRecv(int x)
{
n=x;//将参数值赋予该类中的变量n
CString str;//定义字符串用于构造命令
char recvdata[1024]={0},ch[1024],ch2[1024];//接收数据数组
str.Format("retr d%,n");//格式化字符串
str+="\r\n";//添加回车换行符
int s;
s=::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//创建套接字
send(s,str.GetBuffer(1),sizeof(str),0);//发送获取邮件命令
if (recv(s,recvdata,1024,0))//接收数据
{
if(recvdata[0]=='O'&&recvdata[1]=='K')//接收成功
{
recvdata[sizeof(recvdata)+1 ]='\0';//将字符数组转换为字符串
mailadd=recvdata;
while(i=mailadd.Find("from:")!=-1 || i<mailadd.Find("\r\n"))//循环查找
{
ch[i++]=recvdata[i++];//拷贝字符
}
GetDlgItem(IDC_NAME)->SetWindowText(ch);//设置发送者邮件地址
if(i=mailadd.Find("\n")!=-1)//查找空行
{
ch2[i++]=recvdata[i++];//拷贝字符
GetDlgItem(IDC_EDIT1)->SetWindowText(ch2);//显示邮件内容
}
}
else
{MessageBox("接收失败!");
}
}
}
3 个解决方案
#1
失败的代码是多少,查一下说明;
#3
http://download.csdn.net/download/chelp/3881684
#1
失败的代码是多少,查一下说明;
#2
#3
http://download.csdn.net/download/chelp/3881684