分类: C/C++
2008-08-26 14:03:05
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...
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).
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.
Edit gniazdo.h file. At the beginning you will se something like this:
#define DLG CPop3Dlg* //change it to your CDialog-based classChange 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.
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
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); }
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!