Updated POP3 Wrapper Class

This CPop class is an apdate from the POP3 Protocol wrapper class written by Asif Rasheed. I had some troubles with this class because it went out from short lists/top/messages and in real live we could have 200 messages waiting on our server (hmmm i do... it's the same as reading a newspaper every morning:), and we could also have large emails (with some zip files in it). So this class needs some changes.

We need to buffer the incomming messages where needed, for example when asking the pop server for a LIST command, that will be very large, we need to store this list somewhere before proceding it. We also need to buffer the TOP and RETR command, wich could be large as well.

I use 2 temp. files,because i dont want to know how my memory looks like when getting 100.000 messages and allocating memory for this. Also when you make some kind of program where more as 100 people could ask for a list at the same time, try figuring out how less memory your computer will have at that moment. I also made a new command to extract the TOP of a message (from,to, date ect.). This still needs some more options, but if you want to do this look at RFC822.

In the top of the CPP file you can define two names that will be used as tempory files.

#define templist "templist.pop"		// file used for temp list command
#define temptop	 "temptop.pop"		// file used for temp top command

The new procedures in this class are:
BOOL CPop::Retrieve(int  MsgNumber,CString fname) 
 char buf [512]; 

 if (SetOutputFile(fname)) m_todisk=TRUE;
	m_ErrorMessage = _T("Error opening output file");
	 return FALSE;
 wsprintf (buf, "RETR %d\r\n",MsgNumber );
 m_PopServer.Send(buf, strlen (buf)); 
 if (CheckResponse(RETR_CHECK)==FALSE) 
  return FALSE; 
  return TRUE; 

MsgNumber: the number of the message to get fname: the name of the file in wich the message is copyed.
BOOL CPop::ExtractTop(CString fname)
	CString tcstring;
	int pos;

	CStdioFile f(fname,CFile::modeRead);
	while (f.ReadString(tcstring))
		tcstring.TrimLeft(); tcstring.TrimRight();
		if (tcstring.Find('\r')!=-1) tcstring=tcstring.Left(tcstring.GetLength()-1);
		if (tcstring.Find('\n')!=-1) tcstring=tcstring.Left(tcstring.GetLength()-1);
		tcstring.TrimLeft(); tcstring.TrimRight();

		if (pos=tcstring.Find("To:")==0) t_To=tcstring.Right(tcstring.GetLength()-sizeof("To:"));
		if (pos=tcstring.Find("From:")==0) t_From=tcstring.Right(tcstring.GetLength()-sizeof("From:"));
		if (pos=tcstring.Find("Subject:")==0) t_Subject=tcstring.Right(tcstring.GetLength()-sizeof("Subject:"));
		if (pos=tcstring.Find("Date:")==0) t_Date=tcstring.Right(tcstring.GetLength()-sizeof("Date:"));
	if ((t_To=="")||(t_From=="")||(t_Date=="")) return FALSE;
	else return TRUE;
CATCH( CFileException, e ){
	m_ErrorMessage="cant open message file";
	return FALSE;

fname, the name of a message wich you stored with the Retreive function.
This command will will extract the top from a message.

New variables are:

	t_To:		top To field
	t_From: 	top From field
	t_Subject: 	top Subject field
	t_Date:		top Date field

For the rest the changes are in the class.

Download source - 3.86 KB

Date Posted:


  • I need to download E-mail attachments

    Posted by Legacy on 12/21/2001 12:00am

    Originally posted by: Masoud Aghapour

    Can anybody tell me how to doanload E-mail attachments? With best regards


    Posted by Legacy on 06/08/2001 12:00am

    Originally posted by: TSANG SUI


  • How to download files attached to the mail ?

    Posted by Legacy on 06/07/2001 12:00am

    Originally posted by: Raphael KISTER

    I would like to download files attached to the mail. How can i do?

    Thank you.

  • CheckResponse ---> Sleep (2000); ???

    Posted by Legacy on 01/06/2000 12:00am

    Originally posted by: Peter

    BOOL CPop::CheckResponse(int ResponseType) 
    char buf[8192];
    int totr,skip;
    BOOL ready;

    for (int i=0;i<sizeof(buf);i++)

    totr=m_PopServer.Receive(buf, sizeof(buf));

    Why Sleep (2000); ?

    I am using a class with the same problem. If I remove the Sleep (2000) and try to receive a message the program crashes. All other function like LIST STAT ... seems not to require the Sleep command.

    Any suggestions ?

  • Something to modify

    Posted by Legacy on 02/18/1999 12:00am

    Originally posted by: jof4002

    In function BOOL CPop::CheckResponse(int ResponseType),

    case LIST_CHECK:

    line 348 is...

    I think (buf[0]='O') should be (buf[0]=='O').

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Being first and fast to market is critical for success. In today's hyper-competitive environment, driving innovation to stay ahead of the competition relies on the ability to seize new opportunities to develop IT solutions faster and more efficiently. This Executive Brief discusses how organizations can meet this high-pressure challenge by moving development of enterprise applications to the cloud, leveraging Platform-as-a-Service (PaaS) and in the process reducing costs, avoiding complexity, and increasing …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date