Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

CPop3 class

Rate me:
Please Sign up or sign in to vote.
4.17/5 (13 votes)
31 Mar 20022 min read 138.2K   5K   39   26
This is simple CPop3 class that can connect to a pop3 server and receive messages.

Sample Image

Introduction

This is simple CPop3 class that can connect to a pop3 server and receive messages. What is the difference between this and other pop3 classes (ie. PJ Naughter's)? Well, first of all - it's mine :-) And the second thing - it's an CAsyncSocket derived class, so that's why using it is quite different from other one. Quick info...

How to use it?

Simply I think. The main class is CPop3, so you just create a variable of this type, set-up things, compile and run. Once you've set up stuff, everything will proceed automatically. There is another class - CBase4Pop3 declared in Gniazdo.h - but you usually don't use it directly (see Gniazdo.h).

Step 1 - starting

Create dialog-based app, add Gniazdo.h, Gniazdo.cpp, pop31.h and pop31.cpp files to project, then create a CPop3 variable in CYourDlg class.

Step 2 - setting up things

Edit gniazdo.h file. At the beginning you will se something like this:

#define DLG CPop3Dlg* //change it to your CDialog-based class
Change it to your dialog-based class. And don't forget to include property header files, if necessary. Now in your dialog-based class create a public function called Dispatch:
//your dlg.h file
public:
void Dispatch(LONG param); //declaration
//your dlg.cpp file
void CYourDlg::Dispatch(LONG param)
{
CString s;
switch(param)
{
case S_CONNECT: //we are connected
	//your code on OnConnect event
	break;
case S_RECEIVE: //OnReceive
//and so on...
So now, every message which comes to CPop3 will be passed to your function.

Step 3 - obtaining information

How to handle messages? Check out pop3Dlg.cpp for details. Basically, you can invoke a CPop3::GetLastMsg(CString &s) to get the most recent data from server. Other functions works similar, so you shouldn't have any problems with using them. There are currently 6 scoket messages:

//basic
#define S_CLOSE 1 //send when we are about to close connection
#define S_CONNECT 2 //send when we have just connected to a server
#define S_RECEIVE 3 //we are receiving some stuff
//and additional
#define S_GETNUMMSGS 5 //send when user can obtain number of messages
#define S_GETSIZEMSGS 6 //as above, but size of messages
#define S_ENDRETR 7 //send when done retrieving

Step 4 - initialization

First, make sure you have AfxInitSocket in your InitInstance. Second set parent window, user, password, server and connect to a server:

void CYourDlg::OnConnectBtn() 
{
	// TODO: Add your control notification handler code here
	pop3.Set(this); //set window that will receive messages
	UpdateData(TRUE);
	pop3.SetProp(user,pass); //set user and pass
	pop3.DelAfterRead(del); //want to delete messages from server?
	pop3.Create(); //create socket
	pop3.Connect((LPCSTR)server,110); //connect to a server
	UpdateData(FALSE);
}

Final step...

Once you've successfully connected to a server, everything will do automatically, that is check for messages, get them, also if you wish delete them and disconnect. When it receives an error, the class will disconnect from the server. To view a message simply call CString CPop3::GetMsgStuff(int i) and CString CPop3::GetMsgBody(int i) where i stands for the message number. CString CPop3::GetMsg(int i) returns all of the message. After sending a S_ENDRETR message (= all of messages have been received) and S_CLOSE, the class will disconnect from server. For other useful functions see pop31.h file.

Hope you like it!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaldo not wotk with gmail Pin
Balkrishna Talele31-Mar-09 20:48
Balkrishna Talele31-Mar-09 20:48 
GeneralRe: do not wotk with gmail Pin
Pogo Lin12-Jan-10 21:26
Pogo Lin12-Jan-10 21:26 
Generalbuffer overrun Pin
imagiro24-Aug-07 23:54
imagiro24-Aug-07 23:54 
QuestionHow can I judge the mail has been read?? Pin
Karlzheng9-Nov-06 22:43
Karlzheng9-Nov-06 22:43 
QuestionReceive Attachment? Pin
dragomir5-Jan-06 4:13
dragomir5-Jan-06 4:13 
GeneralGreat work friend! Pin
Georgi Petrov2-Jun-05 9:04
Georgi Petrov2-Jun-05 9:04 
GeneralRe: Great work friend! Pin
chris myself5-Jun-05 11:12
susschris myself5-Jun-05 11:12 
sure it can, it uses just plain smtp protocol
GeneralLIST/UIDL commands Pin
Member 59600812-Apr-05 2:35
Member 59600812-Apr-05 2:35 
GeneralGet the Attachment Pin
Gajalakshmi Krishnan11-Apr-05 5:41
Gajalakshmi Krishnan11-Apr-05 5:41 
GeneralRe: Get the Attachment Pin
xinxin062923-Jun-07 18:59
xinxin062923-Jun-07 18:59 
GeneralReceiving of messages Pin
marinrm30-Nov-04 4:07
marinrm30-Nov-04 4:07 
GeneralProblem with RETR command Pin
unpatrice4-May-04 23:18
unpatrice4-May-04 23:18 
GeneralSend an attachment Pin
Tjie Pouw15-Feb-04 4:01
Tjie Pouw15-Feb-04 4:01 
GeneralHTML TXT messages Pin
MMs_xH8-Jul-03 6:40
MMs_xH8-Jul-03 6:40 
GeneralRe: HTML TXT messages Pin
kubrick7-Dec-03 7:00
kubrick7-Dec-03 7:00 
GeneralI added TOP pop3 command Pin
forjunkmail25-May-03 18:57
forjunkmail25-May-03 18:57 
QuestionCan we implement this CPop3 class in an DLL without any window? Pin
abirnale7-May-03 21:31
abirnale7-May-03 21:31 
Generalassert Pin
Sonu Kapoor20-Mar-03 21:31
Sonu Kapoor20-Mar-03 21:31 
GeneralAbout RETR command Pin
Member 14719826-Dec-02 2:17
Member 14719826-Dec-02 2:17 
GeneralRe: About RETR command Pin
eggie514-Jun-04 16:29
eggie514-Jun-04 16:29 
Generala bug in CPop3Connection::ReadResponse Pin
sunnet_wang14-Oct-02 16:43
sunnet_wang14-Oct-02 16:43 
GeneralPOP3 protocol Pin
Deepak Kesarwani5-Aug-02 23:52
Deepak Kesarwani5-Aug-02 23:52 
GeneralBug in CPop3Connection::ReadUIDLResponse(int nNumberOfMails) Pin
Martin Eckardt15-Jul-02 22:06
Martin Eckardt15-Jul-02 22:06 
QuestionHow to do it through proxy server Pin
2-Apr-02 17:31
suss2-Apr-02 17:31 
AnswerRe: How to do it through proxy server Pin
2-Apr-02 18:33
suss2-Apr-02 18:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.