home *** CD-ROM | disk | FTP | other *** search
/ Internet Pratica / IPRAT_01.iso / ASP / ASPFusion_Basic_Suite / examples / c++ / advpop3 / AdvPOP3.cpp next >
Encoding:
C/C++ Source or Header  |  2002-01-26  |  18.7 KB  |  361 lines

  1. /************************************************************************************************************
  2. Methods:
  3. 1. Delete()            delete specified message(s) from the mailbox using their sequence numbers
  4. 2. GetAll()            retrieves the data associated with messages using message sequence numbers
  5. 3. GetHeaderOnly()    retrieves the headers data associated with messages using message sequence numbers
  6. 4. Status()            requests the current status
  7.  
  8. Properties:
  9. 1. PutAttachmentPath()                the physical path where attachments will be saved
  10. 2. GetAttachmentPathError()            returns 1 if any error occur in AttachmentPath otherwise 0
  11. 3. GetAttachmentPathErrorReason()    reports any error that occur due to AttchmentPath
  12. 4. PutAttachmentPathHTTP()            URL for the AttachmentPath where attachments are saved
  13. 5. PutDownloadedUIDs()                this attribute is optional. the UIDs of messages previously downloaded 
  14.                                     from server. Used to determine which messages are new. we can also write
  15.                                     it as 3:8 means do not download messages with UIDs starts from 3 and ends
  16.                                     at 8. also 3,8 (comma separated list) can be given which means do not
  17.                                     download messages with sequence number 3 and 8 only
  18. 6. GetErrorReason()                    reports any errors that occur during the request
  19. 7. GetIsError()                        returns 1 if any error occur during the request otherwise 0
  20. 8. PutMessageNumber()                required for Delete, GetHeaderOnly(optional, default is all message numbers)
  21.                                     and GetAll(optional, default is all message numbers). message sequence
  22.                                     numbers of messages, for performing action. We can also write it as 3:8
  23.                                     means messages with sequence number starts from 3 and ends at 8. Also 3,8
  24.                                     (comma separated list) can be given which means messages with sequence
  25.                                     number 3 and 8 only  
  26. 9. PutOrderBy()                        required for GetHeaderOnly(optional), GetAll(optional). required for sorting
  27.                                     of mails. if not specified then no sorting is performed and mail messages
  28.                                     are returned in order as returned by POP3 server. valid sorting criteria are
  29.                                     Subject_ASC
  30.                                     Subject_DESC
  31.                                     From_ASC
  32.                                     From_DESC
  33.                                     To_ASC
  34.                                     To_DESC
  35.                                     Date_ASC
  36.                                     Date_DESC
  37.                                     Size_ASC
  38.                                     Size_DESC
  39.                                     Attachment_ASC(without attachment mails first)
  40.                                     Attachment_DESC(with attachment mails first)
  41. 10. PutPassword()                    required for connection to server in all methods
  42. 11. PutPort()                        optional. if not specified then default port 110 is used
  43. 12. PutServerName()                    required. host name or IP address of the POP3 server
  44. 13. PutSortFirst()                    required for GetHeaderOnly(optional), required for sorting mail messages first
  45.                                     and then return required mail messages. this parameter is only valid for
  46.                                     GetHeaderOnly and ignored in case of GetAll. if parameter MessageNumber
  47.                                     contains 1:5 and this parameter is set as TRUE then the component will fetch
  48.                                     all messages first and will sort on given sorting criteria and then will
  49.                                     return mail messages that come in sequence from 1 to 5, and if this parameter
  50.                                     is set as FALSE then the component will fetch messages from 1 to 5 and will
  51.                                     sort on given sorting criteria and then will return mail messages. valid
  52.                                     sorting criteria are
  53.                                     TRUE
  54.                                     FALSE (default)
  55. 14. PutUserName()                    required for connection to server in all method
  56. ************************************************************************************************************/
  57.  
  58. /***********************************/
  59. /*include all required header files*/
  60. /***********************************/
  61. #include <stdio.h>
  62. #include <conio.h>
  63.  
  64. /************************************************************************/
  65. /*import AdvDirectory COM library here to get its methods and properties*/
  66. /************************************************************************/
  67. #import "C:\AdvPOP3.dll" no_namespace
  68.  
  69. /*************************/
  70. /*prototype for functions*/
  71. /*************************/
  72. void Delete();//Delete function
  73. void GetAll();//GetAll function
  74. void GetHeaderOnly();//GetHeaderOnly function
  75. void Status();//Status function
  76.  
  77. /**************/
  78. /*main program*/
  79. /**************/
  80. void main(void)
  81. {
  82.     char Ch;
  83.     do
  84.     {
  85.         printf("\r\nDelete        [D]");
  86.         printf("\r\nGetAll        [A]");
  87.         printf("\r\nGetHeaderOnly    [H]");
  88.         printf("\r\nStatus        [S]");
  89.         printf("\r\nQuit        [Q]");
  90.         printf("\r\n\r\nEnter Choice = ");
  91.         scanf("%c", &Ch);
  92.         getchar();
  93.         Ch = toupper(Ch);
  94.         switch (Ch)
  95.         {
  96.             case 'D':Delete();break;//call Delete function
  97.             case 'A':GetAll();break;//call GetAll function
  98.             case 'H':GetHeaderOnly();break;//call GetHeaderOnly function
  99.             case 'S':Status();break;//call Status function
  100.             case 'Q':printf("\r\nGood Bye\r\n");break;
  101.             default: printf("\r\nError: Please enter valid choice\r\n");
  102.         }
  103.     }
  104.     while (Ch != 'Q');
  105. }
  106.  
  107. /************************************************************************************/
  108. /*Delete function: The function is used to delete mail messages from any POP3 server*/
  109. /************************************************************************************/
  110. void Delete()
  111. {
  112.     char m_Server[256], m_User[256], m_Password[256], m_MessageNo[256];//define charecter buffer of 256 to read server name, user name, password and message numbers to be deleted
  113.     printf("\r\nEnter Server Name = ");
  114.     gets(m_Server);//read server name
  115.     printf("\r\nEnter User Name = ");
  116.     gets(m_User);//read user name
  117.     printf("\r\nEnter Password = ");
  118.     gets(m_Password);//read password name
  119.     printf("\r\nEnter Message Number = ");
  120.     gets(m_MessageNo);//read message numbers
  121.     try//for COM library call, try...catch is used to handle exceptions
  122.     {
  123.         IPOP3Ptr obj;//take smart pointer object obj for COM library
  124.         CoInitialize(NULL);//Initializes the COM library on the current thread
  125.         obj = IPOP3Ptr("AdvPOP3.POP3");
  126.         obj->PutServerName(m_Server);//set server name property
  127.         obj->PutUserName(m_User);//set user name property
  128.         obj->PutPassword(m_Password);//set password property
  129.         obj->PutMessageNumber(m_MessageNo);//set message number property
  130.         HRESULT hr = obj->Delete();//call Delete method of COM library
  131.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  132.         {
  133.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  134.             return;
  135.         }
  136.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  137.         //GetErrorReason() property of COM will contain error message, for which this error occured
  138.         if (obj->GetIsError() == 1)
  139.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  140.         else
  141.             printf("\r\nSuccessfully Deleted\r\n");//display successfull message
  142.     }
  143.     catch (_com_error &error)//catch exception here
  144.     {
  145.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  146.     }
  147. }
  148.  
  149. /***************************************************************************/
  150. /*Status function: The function is used to get current status about mailbox*/
  151. /***************************************************************************/
  152. void Status()
  153. {
  154.     char m_Server[256], m_User[256], m_Password[256];//define charecter buffer of 256 to read server name, user name and password
  155.     printf("\r\nEnter Server Name = ");
  156.     gets(m_Server);//read server name
  157.     printf("\r\nEnter User Name = ");
  158.     gets(m_User);//read user name
  159.     printf("\r\nEnter Password = ");
  160.     gets(m_Password);//read password name
  161.     try//for COM library call, try...catch is used to handle exceptions
  162.     {
  163.         IPOP3Ptr obj;//take smart pointer object obj for COM library
  164.         CoInitialize(NULL);//Initializes the COM library on the current thread
  165.         obj = IPOP3Ptr("AdvPOP3.POP3");
  166.         obj->PutServerName(m_Server);//set server name property
  167.         obj->PutUserName(m_User);//set user name property
  168.         obj->PutPassword(m_Password);//set password property
  169.         IDispatch *p = obj->Status();//call Status method of COM library
  170.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  171.         //GetErrorReason() property of COM will contain error message, for which this error occured
  172.         if (obj->GetIsError() == 1)
  173.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  174.         else
  175.         {
  176.             printf("\r\nSuccessfully Status\r\n");//display successfull message
  177.             IStatusPtr statusobj;
  178.             p->QueryInterface(IID_IDispatch,(void**)&statusobj);//call query interface
  179.             printf("\r\nCount = %d\r\nSize = %d\r\n", statusobj->GetCount(), statusobj->GetSize());
  180.         }
  181.     }
  182.     catch (_com_error &error)//catch exception here
  183.     {
  184.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  185.     }
  186. }
  187.  
  188. /*****************************************************************************/
  189. /*GetHeaderOnly function: The function is used to get headers of mail message*/
  190. /*****************************************************************************/
  191. void GetHeaderOnly()
  192. {
  193.     char m_Server[256], m_User[256], m_Password[256], m_MessageNo[256], m_DownloadUIDs[256], m_OrderBy[256];//define charecter buffer of 256 to read server name, user name, password, message numbers, dowmload uids and sorting criteria
  194.     long l_SortFirst;//sort first
  195.     printf("\r\nEnter Server Name = ");
  196.     gets(m_Server);//read server name
  197.     printf("\r\nEnter User Name = ");
  198.     gets(m_User);//read user name
  199.     printf("\r\nEnter Password = ");
  200.     gets(m_Password);//read password name
  201.     printf("\r\nEnter Message Number = ");
  202.     gets(m_MessageNo);//read message numbers
  203.     printf("\r\nEnter Downloaded UIDs = ");
  204.     gets(m_DownloadUIDs);//read downloaded uids
  205.     printf("\r\nEnter Sorting Criteria = ");
  206.     gets(m_OrderBy);//read sorting criteria
  207.     printf("\r\nEnter Sort First Flag [0/1] = ");
  208.     scanf("%d", &l_SortFirst);//read sort first
  209.     getchar();
  210.     try//for COM library call, try...catch is used to handle exceptions
  211.     {
  212.         IPOP3Ptr obj;//take smart pointer object obj for COM library
  213.         CoInitialize(NULL);//Initializes the COM library on the current thread
  214.         obj = IPOP3Ptr("AdvPOP3.POP3");
  215.         obj->PutServerName(m_Server);//set server name property
  216.         obj->PutUserName(m_User);//set user name property
  217.         obj->PutPassword(m_Password);//set password property
  218.         obj->PutMessageNumber(m_MessageNo);//set message number property
  219.         obj->PutDownloadedUIDs(m_DownloadUIDs);//set download uids property
  220.         obj->PutOrderBy(m_OrderBy);//set sorting criteria property
  221.         obj->PutSortFirst(l_SortFirst);//set sort first flag property
  222.         _variant_t pVal = obj->GetHeaderOnly();//call GetHeaderOnly method of COM library
  223.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  224.         //GetErrorReason() property of COM will contain error message, for which this error occured
  225.         if (obj->GetIsError() == 1)
  226.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  227.         else
  228.         {
  229.             printf("\r\nSuccessfully GetHeaderOnly\r\n");//display successfull message
  230.             IDispatch* p = pVal.pdispVal;//IDispatch object is assigned VARIANT object
  231.             IFetchPtr fetchobj;//take Fetch interface object
  232.             p->QueryInterface(IID_IDispatch,(void**)&fetchobj);//call query interface
  233.             long count = fetchobj->GetCount();//get number of nodes
  234.             for (int j = 1; j <= count; j++)//loop over each node
  235.             {
  236.                 _variant_t retVal = fetchobj->GetItem(j);//get node item
  237.                 IDispatch* pp = retVal.pdispVal;//IDispatch object is assigned VARIANT object
  238.                 IFetchEntriesPtr fetchentriesobj;//take FetchEntries interface object
  239.                 pp->QueryInterface(IID_IDispatch,(void**)&fetchentriesobj);//call query interface
  240.                 printf("\r\nMessage No = %s\r\nPriority = %s\r\nDate = %s\r\nMessage Id = %s\r\nMime Version = %s\r\nContent Type = %s\r\nFrom = %s\r\nReply To = %s\r\nTo = %s\r\nSubject = %s\r\nX-Mailer = %s\r\nUID = %s\r\nStatus = %s\r\nSize = %d\r\nCC = %s\r\n",
  241.                     (char*)fetchentriesobj->GetMessageNo(), (char*)fetchentriesobj->GetPriority(),
  242.                     (char*)fetchentriesobj->GetDate(), (char*)fetchentriesobj->GetMessageId(),
  243.                     (char*)fetchentriesobj->GetMimeVersion(), (char*)fetchentriesobj->GetContentType(),
  244.                     (char*)fetchentriesobj->GetFrom(), (char*)fetchentriesobj->GetReplyTo(),
  245.                     (char*)fetchentriesobj->GetTo(), (char*)fetchentriesobj->GetSubject(),
  246.                     (char*)fetchentriesobj->GetXMailer(), (char*)fetchentriesobj->GetUID(),
  247.                     (char*)fetchentriesobj->GetStatus(), fetchentriesobj->GetSize(),
  248.                     (char*)fetchentriesobj->GetCC());//format data
  249.                 _variant_t subpVal = fetchentriesobj->ExtraHeaders();//call ExtraHeaders method
  250.                 IDispatch* ppp = subpVal.pdispVal;//IDispatch object is assigned VARIANT object
  251.                 IExtraHeadersPtr extraheadersobj;//take ExtraHeaders interface object
  252.                 ppp->QueryInterface(IID_IDispatch,(void**)&extraheadersobj);//call query interface
  253.                 long subcount = extraheadersobj->GetCount();//get number of nodes
  254.                 for (int i = 1; i <= subcount; i++)
  255.                 {
  256.                     _variant_t subretVal = extraheadersobj->GetItem(i);//get node item
  257.                     IDispatch* pppp = subretVal.pdispVal;//IDispatch object is assigned VARIANT object
  258.                     IExtraHeadersEntriesPtr extraheadersentriesobj;//take ExtraHeadersEntries interface object
  259.                     pppp->QueryInterface(IID_IDispatch,(void**)&extraheadersentriesobj);//call query interface
  260.                     printf("\r\n%s = %s", (char*)extraheadersentriesobj->GetName(), (char*)extraheadersentriesobj->GetValue());
  261.                 }
  262.             }
  263.             printf("\r\n");
  264.         }
  265.     }
  266.     catch (_com_error &error)//catch exception here
  267.     {
  268.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  269.     }
  270. }
  271.  
  272. /********************************************************************/
  273. /*GetAll function: The function is used to get complete mail message*/
  274. /********************************************************************/
  275. void GetAll()
  276. {
  277.     char m_Server[256], m_User[256], m_Password[256], m_MessageNo[256], m_DownloadUIDs[256], m_OrderBy[256], m_Path[256], m_HTTPPath[256];//define charecter buffer of 256 to read server name, user name, password, message numbers, dowmload uids, sorting criteria, attachment path and its http path
  278.     long l_SortFirst;//sort first
  279.     printf("\r\nEnter Server Name = ");
  280.     gets(m_Server);//read server name
  281.     printf("\r\nEnter User Name = ");
  282.     gets(m_User);//read user name
  283.     printf("\r\nEnter Password = ");
  284.     gets(m_Password);//read password name
  285.     printf("\r\nEnter Message Number = ");
  286.     gets(m_MessageNo);//read message numbers
  287.     printf("\r\nEnter Downloaded UIDs = ");
  288.     gets(m_DownloadUIDs);//read downloaded uids
  289.     printf("\r\nEnter Sorting Criteria = ");
  290.     gets(m_OrderBy);//read sorting criteria
  291.     printf("\r\nEnter Sort First Flag [0/1] = ");
  292.     scanf("%d", &l_SortFirst);//read sort first
  293.     getchar();
  294.     printf("\r\nEnter Attachment Path = ");
  295.     gets(m_Path);//read attachment path
  296.     printf("\r\nEnter HTTP Attachment Path = ");
  297.     gets(m_HTTPPath);//read http attachment path
  298.     try//for COM library call, try...catch is used to handle exceptions
  299.     {
  300.         IPOP3Ptr obj;//take smart pointer object obj for COM library
  301.         CoInitialize(NULL);//Initializes the COM library on the current thread
  302.         obj = IPOP3Ptr("AdvPOP3.POP3");
  303.         obj->PutServerName(m_Server);//set server name property
  304.         obj->PutUserName(m_User);//set user name property
  305.         obj->PutPassword(m_Password);//set password property
  306.         obj->PutMessageNumber(m_MessageNo);//set message number property
  307.         obj->PutDownloadedUIDs(m_DownloadUIDs);//set download uids property
  308.         obj->PutOrderBy(m_OrderBy);//set sorting criteria property
  309.         obj->PutSortFirst(l_SortFirst);//set sort first flag property
  310.         _variant_t pVal = obj->GetAll();//call GetAll method of COM library
  311.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  312.         //GetErrorReason() property of COM will contain error message, for which this error occured
  313.         if (obj->GetIsError() == 1)
  314.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  315.         else
  316.         {
  317.             printf("\r\nSuccessfully GetHeaderOnly\r\n");//display successfull message
  318.             IDispatch* p = pVal.pdispVal;//IDispatch object is assigned VARIANT object
  319.             IFetchPtr fetchobj;//take Fetch interface object
  320.             p->QueryInterface(IID_IDispatch,(void**)&fetchobj);//call query interface
  321.             long count = fetchobj->GetCount();//get number of nodes
  322.             for (int j = 1; j <= count; j++)//loop over each node
  323.             {
  324.                 _variant_t retVal = fetchobj->GetItem(j);//get node item
  325.                 IDispatch* pp = retVal.pdispVal;//IDispatch object is assigned VARIANT object
  326.                 IFetchEntriesPtr fetchentriesobj;//take FetchEntries interface object
  327.                 pp->QueryInterface(IID_IDispatch,(void**)&fetchentriesobj);//call query interface
  328.                 printf("\r\nMessage No = %s\r\nPriority = %s\r\nDate = %s\r\nMessage Id = %s\r\nMime Version = %s\r\nContent Type = %s\r\nFrom = %s\r\nReply To = %s\r\nTo = %s\r\nSubject = %s\r\nX-Mailer = %s\r\nUID = %s\r\nStatus = %s\r\nSize = %d\r\nCC = %s\r\nAttachment Sent = %s\r\nAttachment Saved = %s\r\nAttachment Files = %s\r\nBody = %s\r\nHTML Body = %s\r\n",
  329.                     (char*)fetchentriesobj->GetMessageNo(), (char*)fetchentriesobj->GetPriority(),
  330.                     (char*)fetchentriesobj->GetDate(), (char*)fetchentriesobj->GetMessageId(),
  331.                     (char*)fetchentriesobj->GetMimeVersion(), (char*)fetchentriesobj->GetContentType(),
  332.                     (char*)fetchentriesobj->GetFrom(), (char*)fetchentriesobj->GetReplyTo(),
  333.                     (char*)fetchentriesobj->GetTo(), (char*)fetchentriesobj->GetSubject(),
  334.                     (char*)fetchentriesobj->GetXMailer(), (char*)fetchentriesobj->GetUID(),
  335.                     (char*)fetchentriesobj->GetStatus(), fetchentriesobj->GetSize(),
  336.                     (char*)fetchentriesobj->GetCC(), (char*)fetchentriesobj->GetAttachmentSent(),
  337.                     (char*)fetchentriesobj->GetAttachmentSaved(), (char*)fetchentriesobj->GetAttachmentFiles(),
  338.                     (char*)fetchentriesobj->GetBody(), (char*)fetchentriesobj->GetHTMLBody());//format data
  339.                 _variant_t subpVal = fetchentriesobj->ExtraHeaders();//call ExtraHeaders method
  340.                 IDispatch* ppp = subpVal.pdispVal;//IDispatch object is assigned VARIANT object
  341.                 IExtraHeadersPtr extraheadersobj;//take ExtraHeaders interface object
  342.                 ppp->QueryInterface(IID_IDispatch,(void**)&extraheadersobj);//call query interface
  343.                 long subcount = extraheadersobj->GetCount();//get number of nodes
  344.                 for (int i = 1; i <= subcount; i++)
  345.                 {
  346.                     _variant_t subretVal = extraheadersobj->GetItem(i);//get node item
  347.                     IDispatch* pppp = subretVal.pdispVal;//IDispatch object is assigned VARIANT object
  348.                     IExtraHeadersEntriesPtr extraheadersentriesobj;//take ExtraHeadersEntries interface object
  349.                     pppp->QueryInterface(IID_IDispatch,(void**)&extraheadersentriesobj);//call query interface
  350.                     printf("\r\n%s = %s", (char*)extraheadersentriesobj->GetName(), (char*)extraheadersentriesobj->GetValue());
  351.                 }
  352.             }
  353.             printf("\r\n");
  354.         }
  355.     }
  356.     catch (_com_error &error)//catch exception here
  357.     {
  358.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  359.     }
  360. }
  361.