Email.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. // *
  9. // * Email.sln
  10. // *
  11. // * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. // * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. // *
  14. // * Description: This code sample demonstrates how to run a report and send the output to a specific
  15. // * user using the run and deliver methods.
  16. // *
  17. using System;
  18. using System.Web.Services.Protocols;
  19. using System.Windows.Forms;
  20. using SamplesCommon;
  21. using cognosdotnet_10_2;
  22. namespace Email
  23. {
  24. class EmailArgsObject
  25. {
  26. public BaseClassWrapper reportObj = null;
  27. public string emailAddress = "";
  28. public string emailSubject = "";
  29. public string emailBody = "";
  30. public Email emailObject = null;
  31. public EmailArgsObject(Email emailObj, BaseClassWrapper repObj, string addr, string subj, string body)
  32. {
  33. emailObject = emailObj;
  34. reportObj = repObj;
  35. emailAddress = addr;
  36. emailSubject = subj;
  37. emailBody = body;
  38. }
  39. }
  40. /// <summary>
  41. /// Summary description for Email.
  42. /// </summary>
  43. class Email
  44. {
  45. public Email(){}
  46. /// <summary>
  47. /// The main entry point for the application.
  48. /// </summary>
  49. static void Main(string[] args)
  50. {
  51. Control.CheckForIllegalCrossThreadCalls = false;
  52. string cBIUrl = "";
  53. contentManagerService1 cmService = null;
  54. EmailDlg emailDlgObject = new EmailDlg();
  55. SamplesConnect connectDlg = new SamplesConnect();
  56. if (args.GetLength(0) == 0 )
  57. {
  58. // GUI mode
  59. connectDlg.ShowDialog();
  60. if (connectDlg.IsConnectedToCBI() == true)
  61. {
  62. cmService = connectDlg.CBICMS;
  63. cBIUrl = connectDlg.CBIURL;
  64. emailDlgObject.setConnection(connectDlg, cBIUrl);
  65. emailDlgObject.setReportList(BaseClassWrapper.buildReportQueryList(cmService));
  66. emailDlgObject.setSelectedReportIndex(0);
  67. emailDlgObject.addEmailOptions();
  68. emailDlgObject.setSelectedEmailOption(0);
  69. emailDlgObject.ShowDialog();
  70. }
  71. }
  72. }
  73. public string sendEmail(SamplesConnect connection,
  74. BaseClassWrapper reportOrQueryObject,
  75. string emailAddress,
  76. string emailSubject,
  77. string emailBody)
  78. {
  79. if (connection == null)
  80. {
  81. return "...the connection is invalid.\n";
  82. }
  83. option[] emailRunOptions = new option[4]; // holds the run options for running the report
  84. option[] emailOptions = new option[5]; // holds all other email options for delivering the output
  85. // 1. Set the run options
  86. runOptionStringArray deliveryFormat = new runOptionStringArray();
  87. string[] formatList = new string[1];
  88. formatList[0] = "HTML";
  89. deliveryFormat.name = runOptionEnum.outputFormat;
  90. deliveryFormat.value = formatList;
  91. emailRunOptions[0] = deliveryFormat;
  92. runOptionBoolean promptFlag = new runOptionBoolean();
  93. promptFlag.name = runOptionEnum.prompt;
  94. promptFlag.value = false;
  95. emailRunOptions[1] = promptFlag;
  96. // Set the primary wait threshold
  97. asynchOptionInt primaryWaitRunOpts = new asynchOptionInt();
  98. primaryWaitRunOpts.name = asynchOptionEnum.primaryWaitThreshold;
  99. primaryWaitRunOpts.value = 0;
  100. emailRunOptions[2] = primaryWaitRunOpts;
  101. asynchOptionBoolean alwaysIncludePrimaryRequestFlag = new asynchOptionBoolean();
  102. alwaysIncludePrimaryRequestFlag.name = asynchOptionEnum.alwaysIncludePrimaryRequest;
  103. alwaysIncludePrimaryRequestFlag.value = true;
  104. emailRunOptions[3] = alwaysIncludePrimaryRequestFlag;
  105. // 2. Set the delivery options
  106. // a) specify email delivery
  107. runOptionBoolean emailDelivery = new runOptionBoolean();
  108. emailDelivery.name = runOptionEnum.email;
  109. emailDelivery.value = true;
  110. emailOptions[0] = emailDelivery;
  111. // b) set the output to be an email attachment
  112. runOptionBoolean emailAttach = new runOptionBoolean();
  113. emailAttach.name = runOptionEnum.emailAsAttachment;
  114. emailAttach.value = true;
  115. emailOptions[1] = emailAttach;
  116. // c) Set the email body
  117. memoPartString memoText = new memoPartString();
  118. memoText.name = "Body";
  119. memoText.text = emailBody;
  120. memoText.contentDisposition = smtpContentDispositionEnum.inline;
  121. deliveryOptionMemoPart bodyMemo = new deliveryOptionMemoPart();
  122. bodyMemo.name = deliveryOptionEnum.memoPart;
  123. bodyMemo.value = memoText;
  124. emailOptions[2] = bodyMemo;
  125. // d) Set the email address(es)
  126. if (0 != emailAddress.CompareTo(""))
  127. {
  128. addressSMTP singleAddress = new addressSMTP();
  129. singleAddress.Value = emailAddress;
  130. addressSMTP[] addressValue = new addressSMTP[] {singleAddress};
  131. // c) i. Option is set to email to a specific user
  132. deliveryOptionAddressSMTPArray addressDeliveryOption = new deliveryOptionAddressSMTPArray();
  133. addressDeliveryOption.name = deliveryOptionEnum.toAddress;
  134. addressDeliveryOption.value = addressValue;
  135. emailOptions[3] = addressDeliveryOption;
  136. }
  137. else
  138. {
  139. // c) ii. Option is set to email to all contacts
  140. deliveryOptionAddressSMTPArray addressDeliveryOption = new deliveryOptionAddressSMTPArray();
  141. addressDeliveryOption.name = deliveryOptionEnum.toAddress;
  142. addressDeliveryOption.value = getContactEmails(connection.CBICMS);
  143. emailOptions[3] = addressDeliveryOption;
  144. }
  145. // e) Set the email subject
  146. deliveryOptionString subjectOptionString = new deliveryOptionString();
  147. subjectOptionString.name = deliveryOptionEnum.subject;
  148. subjectOptionString.value = emailSubject;
  149. emailOptions[4] = subjectOptionString;
  150. searchPathSingleObject reportPath = new searchPathSingleObject();
  151. reportPath.Value = reportOrQueryObject.searchPath.value;
  152. // execute the report
  153. // sn_dg_sdk_method_reportService_deliver_start_0
  154. asynchReply sendEmailResponse = connection.CBIRS.run(
  155. reportPath,
  156. new parameterValue[] {},
  157. emailRunOptions);
  158. connection.CBIRS.deliver(sendEmailResponse.primaryRequest, new parameterValue[] { }, emailOptions);
  159. // sn_dg_sdk_method_reportService_deliver_end_0
  160. //If the request has not yet completed, keep waiting until it has finished
  161. while ((sendEmailResponse.status != asynchReplyStatusEnum.complete) &&
  162. (sendEmailResponse.status != asynchReplyStatusEnum.conversationComplete))
  163. {
  164. sendEmailResponse = connection.CBIRS.wait(sendEmailResponse.primaryRequest, new parameterValue[] { }, new option[] { });
  165. }
  166. return "...the report : \"" + reportOrQueryObject.defaultName.value + "\" was successfully emailed.\n";
  167. }
  168. public addressSMTP[] getContactEmails(contentManagerService1 cmService)
  169. {
  170. try
  171. {
  172. baseClass[] bcContacts = new baseClass[0];
  173. contact contacts = new contact();
  174. runOptionStringArray arrStrAddressRunOpts = new runOptionStringArray();
  175. propEnum[] props =
  176. new propEnum[] { propEnum.searchPath, propEnum.defaultName, propEnum.email };
  177. searchPathMultipleObject contactSearchPath = new searchPathMultipleObject();
  178. contactSearchPath.Value = "CAMID(\":\")/contact";
  179. bcContacts = cmService.query(contactSearchPath, props, new sort[] {}, new queryOptions());
  180. int nbContacts = bcContacts.GetLength(0);
  181. addressSMTP[] contactList = new addressSMTP[nbContacts];
  182. if ( (bcContacts == null) || (nbContacts <= 0) )
  183. {
  184. //No contacts setup in Content Store
  185. return contactList;
  186. }
  187. else
  188. {
  189. for(int i=0; i<nbContacts; i++)
  190. {
  191. baseClass bc = bcContacts[i];
  192. contacts = (contact)bc;
  193. contactList[i] = new addressSMTP();
  194. contactList[i].Value = contacts.email.value;
  195. }
  196. }
  197. return contactList;
  198. }
  199. catch(SoapException ex)
  200. {
  201. SamplesException.ShowExceptionMessage( ex, true, "Email" );
  202. return null;
  203. }
  204. catch(System.Exception ex)
  205. {
  206. SamplesException.ShowExceptionMessage( ex.Message, true, "Email" );
  207. return null;
  208. }
  209. }
  210. }
  211. }