PageRenderTime 115ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSAutomateOutlook/Program.cs

#
C# | 191 lines | 104 code | 29 blank | 58 comment | 14 complexity | 85cebedf19165e84eb5a051a2d9b1f21 MD5 | raw file
  1. /********************************** Module Header **********************************\
  2. * Module Name: Program.cs
  3. * Project: CSAutomateOutlook
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * The CSAutomateOutlook example demonstrates the use of Visual C# code to automate
  7. * Microsoft Outlook to log on with your profile, enumerate contacts, send a mail, log
  8. * off, close the Microsoft Outlook application and then clean up unmanaged COM
  9. * resources.
  10. *
  11. * This source is subject to the Microsoft Public License.
  12. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  13. * All other rights reserved.
  14. *
  15. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  16. * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  18. \***********************************************************************************/
  19. #region Using directives
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Text;
  23. using System.Reflection;
  24. using System.Runtime.InteropServices;
  25. using Outlook = Microsoft.Office.Interop.Outlook;
  26. #endregion
  27. namespace CSAutomateOutlook
  28. {
  29. class Program
  30. {
  31. [STAThread]
  32. static void Main(string[] args)
  33. {
  34. AutomateOutlook();
  35. // Clean up the unmanaged Outlook COM resources by forcing a garbage
  36. // collection as soon as the calling function is off the stack (at which
  37. // point these objects are no longer rooted).
  38. GC.Collect();
  39. GC.WaitForPendingFinalizers();
  40. // GC needs to be called twice in order to get the Finalizers called -
  41. // the first time in, it simply makes a list of what is to be finalized,
  42. // the second time in, it actually is finalizing. Only then will the
  43. // object do its automatic ReleaseComObject.
  44. GC.Collect();
  45. GC.WaitForPendingFinalizers();
  46. }
  47. static void AutomateOutlook()
  48. {
  49. object missing = Type.Missing;
  50. Outlook.Application oOutlook = null;
  51. Outlook.NameSpace oNS = null;
  52. Outlook.Folder oCtFolder = null;
  53. Outlook.Items oCts = null;
  54. Outlook.MailItem oMail = null;
  55. try
  56. {
  57. // Start Microsoft Outlook and log on with your profile.
  58. // Create an Outlook application.
  59. oOutlook = new Outlook.Application();
  60. Console.WriteLine("Outlook.Application is started");
  61. Console.WriteLine("User logs on ...");
  62. // Get the namespace.
  63. oNS = oOutlook.GetNamespace("MAPI");
  64. // Log on by using a dialog box to choose the profile.
  65. oNS.Logon(missing, missing, true, true);
  66. // Alternative logon method that uses a specific profile.
  67. // If you use this logon method, change the profile name to an
  68. // appropriate value. The second parameter of Logon is the password
  69. // (if any) associated with the profile. This parameter exists only
  70. // for backwards compatibility and for security reasons, and it is
  71. // not recommended for use.
  72. //oNS.Logon("YourValidProfile", missing, false, true);
  73. Console.WriteLine("Press ENTER to continue when Outlook is ready.");
  74. Console.ReadLine();
  75. // Enumerate the contact items.
  76. Console.WriteLine("Enumerate the contact items");
  77. oCtFolder = (Outlook.Folder)oNS.GetDefaultFolder(
  78. Outlook.OlDefaultFolders.olFolderContacts);
  79. oCts = oCtFolder.Items;
  80. // Enumerate the contact items. Be careful with foreach loops.
  81. // See: http://tiny.cc/uXw8S.
  82. for (int i = 1; i <= oCts.Count; i++)
  83. {
  84. object oItem = oCts[i];
  85. if (oItem is Outlook.ContactItem)
  86. {
  87. Outlook.ContactItem oCt = (Outlook.ContactItem)oItem;
  88. Console.WriteLine(oCt.Email1Address);
  89. // Do not need to Marshal.ReleaseComObject oCt because
  90. // (Outlook.ContactItem)oItem is a simple .NET type
  91. // casting, instead of a COM QueryInterface.
  92. }
  93. else if (oItem is Outlook.DistListItem)
  94. {
  95. Outlook.DistListItem oDl = (Outlook.DistListItem)oItem;
  96. Console.WriteLine(oDl.DLName);
  97. // Do not need to Marshal.ReleaseComObject oDl because
  98. // (Outlook.DistListItem)oItem is a simple .NET type
  99. // casting, instead of a COM QueryInterface.
  100. }
  101. // Release the COM object of the Outlook item.
  102. Marshal.FinalReleaseComObject(oItem);
  103. oItem = null;
  104. }
  105. // Create and send a new mail item.
  106. Console.WriteLine("Create and send a new mail item");
  107. oMail = (Outlook.MailItem)oOutlook.CreateItem(
  108. Outlook.OlItemType.olMailItem);
  109. // Set the properties of the email.
  110. oMail.Subject = "Feedback of All-In-One Code Framework";
  111. oMail.To = "codefxf@microsoft.com";
  112. oMail.HTMLBody = "<b>Feedback:</b><br />";
  113. // Displays a new Inspector object for the item and allows users to
  114. // click on the Send button to send the mail manually.
  115. // Modal = true makes the Inspector window modal
  116. oMail.Display(true);
  117. // [-or-]
  118. // Automatically send the mail without a new Inspector window.
  119. //((Outlook._MailItem)oMail).Send();
  120. // User logs off and quits Outlook.
  121. Console.WriteLine("Log off and quit the Outlook application");
  122. oNS.Logoff();
  123. ((Outlook._Application)oOutlook).Quit();
  124. }
  125. catch (Exception ex)
  126. {
  127. Console.WriteLine("AutomateOutlook throws the error: {0}", ex.Message);
  128. }
  129. finally
  130. {
  131. // Manually clean up the explicit unmanaged Outlook COM resources by
  132. // calling Marshal.FinalReleaseComObject on all accessor objects.
  133. // See http://support.microsoft.com/kb/317109.
  134. if (oMail != null)
  135. {
  136. Marshal.FinalReleaseComObject(oMail);
  137. oMail = null;
  138. }
  139. if (oCts != null)
  140. {
  141. Marshal.FinalReleaseComObject(oCts);
  142. oCts = null;
  143. }
  144. if (oCtFolder != null)
  145. {
  146. Marshal.FinalReleaseComObject(oCtFolder);
  147. oCtFolder = null;
  148. }
  149. if (oNS != null)
  150. {
  151. Marshal.FinalReleaseComObject(oNS);
  152. oNS = null;
  153. }
  154. if (oOutlook != null)
  155. {
  156. Marshal.FinalReleaseComObject(oOutlook);
  157. oOutlook = null;
  158. }
  159. }
  160. }
  161. }
  162. }