Visual C++ 7.1Visual C++ 8.0Windows VistaVisual C++ 7.0C++/CLIWindows 2003Win32Windows 2000Visual C++ 6.0Windows XP初学者CWindowsC++
使用套接字编写的 POP3 客户端





2.00/5 (1投票)
邮局协议,用于检索邮件
引言
本文提供了一个关于如何从任何服务器检索邮件的简单思路。
背景
POP3协议用于允许工作站检索服务器为其保存的邮件。最初,服务器主机通过监听TCP端口110启动POP3服务。当客户端主机希望使用该服务时,它与服务器主机建立TCP连接。连接建立后,POP3服务器发送问候语。
使用代码
代码的工作方式如下:一旦与服务器建立连接,服务器就会验证用户名和密码,然后检索邮件。POP3协议有一些命令,用于身份验证和检索邮件。以下是使用各种命令检索邮件的函数。 1.CONNECT 用于连接到服务器。 2.USER 用于验证用户名。 3.PASS 用于验证密码。 4.STAT 用于获取消息数量。 5.QUIT 用于断开与服务器的连接。
void Tapp::ProcessPop3Commands(enum eCommands pCommand,HWND pHwnd)
{
switch(pCommand) {
case CONNECT:
char* address; ///<CONTAINS span server.< the of address>
char* portno; ///<CONTAINS span server.< the of no port>
//handles the submission of the details into the list box on clicking submit
address=vWin.RetrieveText(GetDlgItem(pHwnd,IDC_EDITBOX3));
portno=vWin.RetrieveText(GetDlgItem(pHwnd,IDC_EDITBOX4));
if(vPop.MakeConnection(address,portno)==TRUE){
//frees the memory allocated for address.
free(address);
//frees the memory allocated for portno.
free(portno);
//retrieves the information from the server.
vPop.RecvInfoFromServer();
//ADD THE RETRIEVED TEXT TO THE LIST BOX
MessageBox(pHwnd, vPop.Getresponse(), "Message",MB_OK);
//sets the command as USER.
pCommand=USER;
}else{
//frees the memory allocated for address.
free(address);
//frees the memory allocated for portno.
free(portno);
break;
}
case USER:
{
char *username; ///<CONTAINS span the of user< name>
username=vWin.RetrieveText(GetDlgItem(pHwnd,IDC_EDITBOX1));
if(vPop.SendInformation("USER",username,pHwnd)==TRUE){
//frees the memory allocated for username.
free(username);
//sets the command as PASS.
pCommand=PASS;
}else{
//frees the memory allocated for username.
free(username);
//sets the command as QUIT
pCommand=QUIT;
}
}
case PASS:
{
char *password; ///<CONTAINS span the password.
password=vWin.RetrieveText(GetDlgItem(pHwnd,IDC_EDITBOX2));
if(vPop.SendInformation("PASS",password,pHwnd)==TRUE){
//frees the memory allocated for password.
free(password);
//sets the command as STAT.
pCommand=STAT;
}else{
//frees the memory allocated for password.
free(password);
//sets the command as QUIT.
pCommand=QUIT;
}
}
case STAT:
if(vPop.SendInformation("STAT"," ",pHwnd)==TRUE){
break;
}else{
//sets the command as QUIT.
pCommand=QUIT;
}
case QUIT:
if(MessageBox(pHwnd,vPop.Getresponse(), "Message",MB_OK)==IDOK){
vPop.CloseSocket();
}
break;
}
}