Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

POP3 Client using Sockets

2.00/5 (1 vote)
13 May 2008CPOL1 min read 1   802  
post office protocol to retrieve mails

Introduction

This article gives a small idea as to how we can retrieve the mails from any server.

Background

The POP3 protocol is used to allow a workstation to retrieve mail that the server is holding for it. Initially, the server host starts the POP3 service by listening on TCP port 110. When a client host wishes to make use of the service, it establishes a TCP connection with the server host. When the connection is established, the POP3 server sends a greeting.

Using the code

The working of the code is as follows:- Once the connection with the server is established,the server authenticates the user name and the password and then retrieves the mails. The POP3 protocol has certain commands which it uses for authentication and for retieval of mails. Below is the function which makes use of the various commands to retrieve the mails. 1.CONNECT this is used to connect to the server. 2.USER this is used to authenticate the user name. 3.PASS this is used to authenticate the password. 4.STAT this is used to get the number of messages. 5.QUIT this is used to disconnect from the server.

C#
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; 
   
        } 
     
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)