POP3 Protocol Encapsulation Class

Source: Internet
Author: User
Tags include connect strlen

Currently on the Internet, the use of email is more and more extensive. Of all the TCP connections, about half of the lines are used to send and receive emails. Therefore, it is impossible for many network applications to leave the POP3 protocol.

Moreover, many email systems use the SMTP protocol as the routing protocol, and the POP3 protocol as an acceptance protocol. For a lot of information about these 2 protocols, you can look at RCF821 (about SMTP) and RCF1225 (about POP3). Although in the Windows operating system, there is a MAPI application to manage the email, but its function is not strong enough to directly deal with some of the email services. So, I wrote a C + + class with a POP3 protocol, in which I also used CSocket as a class member (which seemed a bit unthinkable), so that we could use it when we connected. Also, some of the features used by the pop class resemble some of the commands in the POP3 protocol. Here's the code:

/*--------------------------------------------------------------------
Pop.h:mainheaderfileforthepopapplication
-----------------------------------------------------------------------*/
#if!defined (Afx_pop_h__a44b38b6_697c_11d1_881e_00001c302581__included_)
#defineAFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_
#defineCONNECTION_CHECK0
#defineUSER_CHECK1
#definePASSWORD_CHECK2
#defineQUIT_CHECK3
#defineDELETE_CHECK4
#defineRSET_CHECK5
#defineSTAT_CHECK6
#defineNOOP_CHECK7
#defineLIST_CHECK8
#defineRETR_CHECK9
/////////////////////////////////////////////////////////////////////////////
Classcpop
{
Public
Boollist ();
cwordarraym_sizeofmsg;
Cstringgeterrormessage ();//ifthereisanyerrorthiswillreturnitmethod
Cstringgetpassword ();//gettingpasswordstoredinclass
Voidsetpassword (Cstring&password);//settingpasswordinclass
Cstringgetuser ();//gettingusernamestoredinclass
Voidsetuser (cstring&user);//settingusernameinclass
Cstringgethost ();//gettinghostname (Emailservername) Storedinclass
Voidsethost (Cstring&host);//settinghostname (Emailservername) Inclass
Boolconnect ();//connectingtoemailserver
Intgettotalmailsize ();//itreturnsthetotalmailsize
Intgetnumberofmails ();//itreturnthenumberofmails
Cstringgetmsgcontents ();
Boolstatistics ();//issuethestatcommandonemailserver
Boolretrieve (intmsgnumber);//gettinganyparticularmailmessage
Boolreset ();//issuetheresetcommandonemailserver
Intgetmessagesize (intmsgnumber);//returnasizeofanyparticularmail
Boolnoop ();//issuethenoopcommandonemailserver
Booldisconnect ();//issuethequitcommandonemailserver
Booldelete (int&msgnumber);//deleteingaparticularmessagefromemailserver
Boolconnect (Cstring&host,cstring&user,cstring&password);
Cpop ();
Virtual~cpop ();
Private
Cstringm_errormessage;
Boolcheckresponse (Intresponsetype);
Cstringm_password;
Cstringm_user;
Cstringm_host;
cstringm_msgcontents;
Intm_totalsize;
Intm_numbermail;
Csocketm_popserver;
};
/#endif//!defined (afx_pop_h__a44b38b6_697c_11d1_881e_00001c302581__included_)
/*-----------------------------------------------------------------------------------------------
Pop.cpp:Definestheclassbehaviorsfortheapplication.
---------------------------------------------------------------------------------------------------*/
#include "stdafx.h"
#include "Pop.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharthis_file[]=__file__;
#endif
////////////////////////////////////////////////////////////////////
Cpopclass
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
Construction/destruction
//////////////////////////////////////////////////////////////////////
Cpop::cpop ()
{
M_popserver.create ();
}
{4eec1c91-6be1-11d1-8824-00001c302581}
Cpop::~cpop ()
{
M_popserver.close ();
}
Boolcpop::connect (Cstring&host,cstring&user,cstring&password)
{
CHARBUF[512];
if (!m_popserver.connect (host,110))//110pop3port
{
m_errormessage=_t ("servercannotbeconnected");
Returnfalse;
}
Else
{
if (Checkresponse (Connection_check) ==false)
Returnfalse;
wsprintf (buf, "user%s", (LPCSTR) USER);
M_popserver.send (Buf,strlen (BUF));
if (Checkresponse (User_check) ==false)
Returnfalse;
wsprintf (buf, "pass%s", (LPCSTR) Password);
M_popserver.send (Buf,strlen (BUF));
if (Checkresponse (Password_check) ==false)
Returnfalse;
Returntrue;
}
}
Boolcpop::D elete (Int&msgnumber)
{
CHARBUF[512];
wsprintf (buf, "dele%d", Msgnumber);
M_popserver.send (Buf,strlen (BUF));
if (Checkresponse (Delete_check) ==false)
Returnfalse;
Else
Returntrue;
}
Boolcpop::D isconnect ()
{
CHARBUF[512];
wsprintf (buf, "QUIT");
M_popserver.send (Buf,strlen (BUF));
if (Checkresponse (Quit_check) ==false)
Returnfalse;
Else
Returntrue;
}
Boolcpop::noop ()
{
CHARBUF[512];
wsprintf (buf, "NOOP");
M_popserver.send (Buf,strlen (BUF));
if (Checkresponse (Noop_check) ==false)
Returnfalse;
Else
Returntrue;
}
Returnthemsgsizeforgivenmsgnumber
Intcpop::getmessagesize (Intmsgnumber)
{
if (M_sizeofmsg.getsize () <msgnumber+1)
Return0;
Else
RETURNM_SIZEOFMSG[MSGNUMBER+1];
}
Boolcpop::reset ()
{
CHARBUF[512];
wsprintf (buf, "RSET");
M_popserver.send (Buf,strlen (BUF));
if (Checkresponse (Rset_check) ==false)
Returnfalse;
Else
Returntrue;
}
Msgcontentswillholdthemsgbody
Boolcpop::retrieve (Intmsgnumber)
{
CHARBUF[512];
wsprintf (buf, "retr%d", Msgnumber);
M_popserver.send (Buf,strlen (BUF));
if (Checkresponse (Retr_check) ==false)
Returnfalse;
Else
Returntrue;
}
Boolcpop::statistics ()
{
CHARBUF[512];
wsprintf (buf, "STAT");
M_popserver.send (Buf,strlen (BUF));
if (Checkresponse (Stat_check) ==false)
Returnfalse;
Else
Returntrue;
}
Cstringcpop::getmsgcontents ()
{
returnm_msgcontents;
}
Intcpop::getnumberofmails ()
{
Returnm_numbermail;
}
Intcpop::gettotalmailsize ()
{
Returnm_totalsize;
}
Boolcpop::connect ()
{
Connect (M_host,m_user,m_password);
Returntrue;
}
Voidcpop::sethost (Cstring&host)
{
M_host=host;
}
Cstringcpop::gethost ()
{
Returnm_host;
}
Voidcpop::setuser (Cstring&user)
{
M_user=user;
}
Cstringcpop::getuser ()
{
Returnm_user;
}
Voidcpop::setpassword (Cstring&password)
{
M_password=password;
}
Cstringcpop::getpassword ()
{
Returnm_password;
}
Boolcpop::checkresponse (Intresponsetype)
{
CHARBUF[1000];
for (inti=0;i<512;i++)
Buf[i]= '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.