Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5382229
  • 博文数量: 671
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 7310
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-14 09:56
文章分类

全部博文(671)

文章存档

2011年(1)

2010年(2)

2009年(24)

2008年(271)

2007年(319)

2006年(54)

我的朋友

分类: C/C++

2008-08-26 14:03:05

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

阅读(2193) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-06-08 17:15:44

你好,我现在急需CPop3 class pop3类邮件源码,能把它发给我吗?我的邮箱是leen1988@126.com