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

  1. /************************************************************************************************************
  2. Methods:
  3. 1. SendMail()        send mail in blocking way
  4. 2. SendQMail()        send mail in non-blocking way, use service
  5.  
  6. Properties:
  7. 1. PutBCC()            contains the address to whom, the blind carbon copy should be sent. for multiple
  8.                     recipients, addresses will be semicolon (;) separated
  9. 2. PutBody()        the property is to assign the body of the mail. for a long body one can call it multiple
  10.                     times. embedded images can also be sent within the body using a specified tag i.e.
  11.                     <AdvImageMail: c:\test\bubbles.bmp|0>. whereas the æ0Æ specifies that do not delete the
  12.                     file after the mail is sent. A æ1Æ here tells the component to delete the file after the
  13.                     mail is sent
  14. 3. PutCC()            contains the address to whom, the carbon copy of the mail should be sent. for multiple
  15.                     recipients, addresses will be semicolon (;) separated
  16. 4. GetErrorReason()    return number of files to be uploaded
  17. 5. GetIsError()        returns 1 if any error occur during the request otherwise 0
  18. 6. PutLogFile()        by default the logging is performed in a file AdvMail.log on the server present in a
  19.                     specific directory. if the user wants the logging to be performed in user defined path
  20.                     then he can specify his own path for logging
  21. 7. PutLogging()        used to specifying what type of logging should be performed. valid values are
  22.                     0 No Logging
  23.                     1 Error Logging (default)
  24.                     2 Complete logging
  25. 8. PutPort()        it is the port by which the mail should be sent if no port is specified then a port 25
  26.                     is used for sending the mails
  27. 9. PutReceipt()        it is used, if mail sender wants to get information back, when mail receiver reads the
  28.                     mail message. if this value is set TRUE, then mail sender will get information that the 
  29.                     mail receiver reads the mail, otherwise mail sender will not be informed. valid values are
  30.                     TRUE
  31.                     FALSE (default)
  32. 10. PutServer()        it is the SMTP server through which the mail will be sent. if it is not specified then the
  33.                     mail will be sent by any of the default servers
  34. 11. PutSubject()    it is the subject of the mail
  35. 12. PutType()        it defines the mail type. valid values are
  36.                     HTML (default)
  37.                     TEXT
  38. ************************************************************************************************************/
  39.  
  40. /***********************************/
  41. /*include all required header files*/
  42. /***********************************/
  43. #include <stdio.h>
  44. #include <conio.h>
  45.  
  46. /************************************************************************/
  47. /*import AdvDirectory COM library here to get its methods and properties*/
  48. /************************************************************************/
  49. #import "C:\AdvSMTP.dll" no_namespace
  50.  
  51. /*************************/
  52. /*prototype for functions*/
  53. /*************************/
  54. void SendMail();//SendMail function
  55. void SendQMail();//SendQMail function
  56.  
  57. /**************/
  58. /*main program*/
  59. /**************/
  60. void main(void)
  61. {
  62.     char Ch;
  63.     do
  64.     {
  65.         printf("\r\nSendMail    [S]");
  66.         printf("\r\nSendQMail    [M]");
  67.         printf("\r\nQuit        [Q]");
  68.         printf("\r\n\r\nEnter Choice = ");
  69.         scanf("%c", &Ch);
  70.         getchar();
  71.         Ch = toupper(Ch);
  72.         switch (Ch)
  73.         {
  74.             case 'S':SendMail();break;//call SendMail function
  75.             case 'M':SendQMail();break;//call SendQMail function
  76.             case 'Q':printf("\r\nGood Bye\r\n");break;
  77.             default: printf("\r\nError: Please enter valid choice\r\n");
  78.         }
  79.     }
  80.     while (Ch != 'Q');
  81. }
  82.  
  83. /******************************************************/
  84. /*SendMail function: The function is used to send mail*/
  85. /******************************************************/
  86. void SendMail()
  87. {
  88.     char m_Server[256], m_From[256], m_To[256], m_CC[256], m_BCC[256], m_Subject[256], m_Body[256], m_Attachment[256];//define charecter buffer of 256 to server name, port, from address, cc, bcc, subject, to address, body of mail message and mail attachment if any
  89.     long l_Port, l_Delete;//define server port and mail attachement delete flag
  90.     printf("\r\nEnter Server Name = ");
  91.     gets(m_Server);//read server name
  92.     printf("\r\nEnter Server Port = ");
  93.     scanf("%d", &l_Port);//read server port
  94.     getchar();
  95.     printf("\r\nEnter From Address = ");
  96.     gets(m_From);//read from
  97.     printf("\r\nEnter To Address = ");
  98.     gets(m_To);//read to
  99.     printf("\r\nEnter CC Address = ");
  100.     gets(m_CC);//read cc
  101.     printf("\r\nEnter BCC Address = ");
  102.     gets(m_BCC);//read bcc
  103.     printf("\r\nEnter Subject = ");
  104.     gets(m_Subject);//read subject
  105.     printf("\r\nEnter Body = ");
  106.     gets(m_Body);//read body
  107.     printf("\r\nEnter Attachment Complete Path = ");
  108.     gets(m_Attachment);//read complete attachment path
  109.     printf("\r\nEnter Attachment Delete Flag [1/0] = ");
  110.     scanf("%d", &l_Delete);//read attachment delete flag
  111.     getchar();
  112.     try//for COM library call, try...catch is used to handle exceptions
  113.     {
  114.         ISMTPPtr obj;//take smart pointer object obj for COM library
  115.         CoInitialize(NULL);//Initializes the COM library on the current thread
  116.         obj = ISMTPPtr("AdvSMTP.SMTP");
  117.         obj->PutServer(m_Server);//set server name property
  118.         obj->PutPort(l_Port);//set server port property
  119.         obj->PutCc(m_CC);//set cc property
  120.         obj->PutBcc(m_BCC);//set bcc property
  121.         obj->PutSubject(m_Subject);//set subject property
  122.         obj->PutBody(m_Body);//set body property
  123.         if (l_Delete)
  124.             obj->MimeAttach(m_Attachment, L"Delete");
  125.         else
  126.             obj->MimeAttach(m_Attachment, L"NoDelete");
  127.         HRESULT hr = obj->SendMail(m_From, m_To);//call SendMail method of COM library
  128.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  129.         {
  130.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  131.             return;
  132.         }
  133.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  134.         //GetErrorReason() property of COM will contain error message, for which this error occured
  135.         if (obj->GetIsError() == 1)
  136.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  137.         else
  138.             printf("\r\nSuccessfully Send\r\n");//display successfull message
  139.     }
  140.     catch (_com_error &error)//catch exception here
  141.     {
  142.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  143.     }
  144. }
  145.  
  146. /***********************************************************************/
  147. /*SendQMail function: The function is used to send mail through service*/
  148. /***********************************************************************/
  149. void SendQMail()
  150. {
  151.     char m_Server[256], m_From[256], m_To[256], m_CC[256], m_BCC[256], m_Subject[256], m_Body[256], m_Attachment[256];//define charecter buffer of 256 to server name, port, from address, cc, bcc, subject, to address, body of mail message and mail attachment if any
  152.     long l_Port, l_Delete;//define server port and mail attachement delete flag
  153.     printf("\r\nEnter Server Name = ");
  154.     gets(m_Server);//read server name
  155.     printf("\r\nEnter Server Port = ");
  156.     scanf("%d", &l_Port);//read server port
  157.     getchar();
  158.     printf("\r\nEnter From Address = ");
  159.     gets(m_From);//read from
  160.     printf("\r\nEnter To Address = ");
  161.     gets(m_To);//read to
  162.     printf("\r\nEnter CC Address = ");
  163.     gets(m_CC);//read cc
  164.     printf("\r\nEnter BCC Address = ");
  165.     gets(m_BCC);//read bcc
  166.     printf("\r\nEnter Subject = ");
  167.     gets(m_Subject);//read subject
  168.     printf("\r\nEnter Body = ");
  169.     gets(m_Body);//read body
  170.     printf("\r\nEnter Attachment Complete Path = ");
  171.     gets(m_Attachment);//read complete attachment path
  172.     printf("\r\nEnter Attachment Delete Flag [1/0] = ");
  173.     scanf("%d", &l_Delete);//read attachment delete flag
  174.     getchar();
  175.     try//for COM library call, try...catch is used to handle exceptions
  176.     {
  177.         ISMTPPtr obj;//take smart pointer object obj for COM library
  178.         CoInitialize(NULL);//Initializes the COM library on the current thread
  179.         obj = ISMTPPtr("AdvSMTP.SMTP");
  180.         obj->PutServer(m_Server);//set server name property
  181.         obj->PutPort(l_Port);//set server port property
  182.         obj->PutCc(m_CC);//set cc property
  183.         obj->PutBcc(m_BCC);//set bcc property
  184.         obj->PutSubject(m_Subject);//set subject property
  185.         obj->PutBody(m_Body);//set body property
  186.         if (l_Delete)
  187.             obj->MimeAttach(m_Attachment, L"Delete");
  188.         else
  189.             obj->MimeAttach(m_Attachment, L"NoDelete");
  190.         HRESULT hr = obj->SendQMail(m_From, m_To);//call SendMail method of COM library
  191.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  192.         {
  193.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  194.             return;
  195.         }
  196.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  197.         //GetErrorReason() property of COM will contain error message, for which this error occured
  198.         if (obj->GetIsError() == 1)
  199.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  200.         else
  201.             printf("\r\nSuccessfully Send\r\n");//display successfull message
  202.     }
  203.     catch (_com_error &error)//catch exception here
  204.     {
  205.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  206.     }
  207. }
  208.