Currently, email is widely used on the Internet. About half of all TCP connection lines are used to send and receive emails. Therefore, many network applications Program It is impossible to leave the POP3 protocol.
In addition, many email systems use the SMTP protocol as the sending protocol and POP3 protocol as the receiving protocol. For more information about the two Protocols, see rcf821 (about SMTP) and rcf1225 (about POP3 ). Although in windows, an mapi application manages emails, it is not powerful enough to directly process some email services. Therefore, I wrote a C ++ class about POP3 protocol. In this class, I also used csocket as a class member (which seems a bit unimaginable, we can use it during connection. In addition, some functions of the pop class are similar to some commands in POP3 protocol. NextCode:
/*--------------------------------------------------------------------
Pop. h: mainheaderfileforthepopapplication
-----------------------------------------------------------------------*/
# If! Defined (afx_pop_h1_a44b38b6_697c_11d1_881e_1_1c3025811_included _)
# Defineafx_pop_h _ a44b38b6_697c_11d1_881e_1_1c3025811_encoded _
# 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); // gettinganyparticipant ularmailmessage
Boolreset (); // issuetheresetcommandonemailserver
Intgetmessagesize (intmsgnumber); // returnasizeofanyparticle mail
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_h1_a44b38b6_697c_11d1_881e_1_1c3025811_included _)
/* Configure /*-----------------------------------------------------------------------------------------------
// Pop. cpp: definestheclassbehaviorsfortheapplication.
Optional ---------------------------------------------------------------------------------------------------*/
# 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-1081c302581}
Cpop ::~ Cpop ()
{
M_popserver.close ();
}
Boolcpop: connect (cstring & host, cstring & user, cstring & password)
{
Charbuf [2, 512];
If (! M_popserver.connect (host, 110) // 110pop3port
{
M_errormessage = _ T ("servercannotbeconnected ");
Returnfalse;
}
Else
{
If (checkresponse (connection_check) = false)
Returnfalse;
Wsprintf (BUF, "user % s \ r \ n", (lpcstr) User );
M_popserver.send (BUF, strlen (BUF ));
If (checkresponse (user_check) = false)
Returnfalse;
Wsprintf (BUF, "Pass % s \ r \ n", (lpcstr) password );
M_popserver.send (BUF, strlen (BUF ));
If (checkresponse (password_check) = false)
Returnfalse;
Returntrue;
}
}
Boolcpop: delete (Int & msgnumber)
{
Charbuf [2, 512];
Wsprintf (BUF, "DELE % d \ r \ n", msgnumber );
M_popserver.send (BUF, strlen (BUF ));
If (checkresponse (delete_check) = false)
Returnfalse;
Else
Returntrue;
}
Boolcpop: disconnect ()
{
Charbuf [2, 512];
Wsprintf (BUF, "Quit \ r \ n ");
M_popserver.send (BUF, strlen (BUF ));
If (checkresponse (quit_check) = false)
Returnfalse;
Else
Returntrue;
}
Boolcpop: Noop ()
{
Charbuf [2, 512];
Wsprintf (BUF, "Noop \ r \ n ");
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 [2, 512];
Wsprintf (BUF, "rset \ r \ n ");
M_popserver.send (BUF, strlen (BUF ));
If (checkresponse (rset_check) = false)
Returnfalse;
Else
Returntrue;
}
// Msgcontentswillholdthemsgbody
Boolcpop: retrieve (intmsgnumber)
{
Charbuf [2, 512];
Wsprintf (BUF, "retr % d \ r \ n", msgnumber );
M_popserver.send (BUF, strlen (BUF ));
If (checkresponse (retr_check) = false)
Returnfalse;
Else
Returntrue;
}
Boolcpop: Statistics ()
{
Charbuf [2, 512];
Wsprintf (BUF, "stat \ r \ n ");
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 [2, 1000];
For (INTI = 0; I <512; I ++)
Buf [I] = '\ 0 ';
M_popserver.receive (BUF, sizeof (BUF ));
Switch (responsetype)
{
Caseconnection_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("badconnection ");
Returnfalse;
}
Break;
Caseuser_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("badusername ");
Returnfalse;
}
Break;
Casepassword_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("badpasswordname ");
Returnfalse;
}
Break;
Casequit_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("erroccuredduringquit ");
Returnfalse;
}
Break;
Casedelete_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("erroccuredduringdele ");
Returnfalse;
}
Break;
Caserset_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("erroccuredduringrset ");
Returnfalse;
}
Break;
Casestat_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("erroccuredduringstat ");
Returnfalse;
}
Else
{
Boolemailnumber = true;
For (char * P = Buf; * P! = '\ 0'; P ++)
{
If (* P = '\ T' | * P = '')
{
If (emailnumber = true)
{
M_numbermail = atoi (P );
Emailnumber = false;
}
Else
{
M_totalsize = atoi (P );
Returntrue;
}
}
}
}
Break;
Casenoop_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("erroccuredduringnoop ");
Returnfalse;
}
Break;
Caselist_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("erroccuredduringlist ");
Returnfalse;
}
Else
{
M_popserver.receive (BUF, sizeof (BUF ));
For (char * P = Buf; * P! = '.'; P ++)
If (* P = '\ T' | * P = '')
M_sizeofmsg.add (atoi (p ));
}
Break;
Caseretr_check:
If (strnicmp (BUF, "-Err", 4) = 0)
{
M_errormessage = _ T ("erroccuredduringretr ");
Returnfalse;
}
Else
{
Chartemp [1, 9000];
M_popserver.receive (temp, sizeof (temp ));
M_msgcontents = temp;
}
Break;
}
Returntrue;
}
Cstringcpop: geterrormessage ()
{
Returnm_errormessage;
}
Boolcpop: List ()
{
Charbuf [2, 512];
Wsprintf (BUF, "list \ r \ n ");
M_popserver.send (BUF, strlen (BUF ));
If (checkresponse (list_check) = false)
Returnfalse;
Else
Returntrue;
}