Ejemplo n.º 1
0
 public string[] getAddresses(int index)
 {
     string[] addressesArr = null; // 保存邮件组下的成员地址
     if (this.addressList[index] is Outlook.DistListItem)
     {
         Outlook.DistListItem distListItem = (Outlook.DistListItem) this.addressList[index];
         addressesArr = new string[distListItem.MemberCount];
         for (int i = 0; i < distListItem.MemberCount; i++)
         {
             Outlook.Recipient recipient = distListItem.GetMember(i + 1);
             addressesArr[i] = recipient.Name + " " + recipient.Address;
         }
     }
     else if (this.addressList[index] is Outlook.AddressEntries)
     {
         Outlook.AddressEntries addresses = (Outlook.AddressEntries) this.addressList[index];
         addressesArr = new string[addresses.Count];
         for (int i = 1; i <= addresses.Count; i++)
         {
             Outlook.AddressEntry membAddress = addresses[i];
             addressesArr[i - 1] = membAddress.Name + " " + membAddress.GetExchangeUser().PrimarySmtpAddress;
         }
     }
     return(addressesArr);
 }
Ejemplo n.º 2
0
 public void addAddressSelfBuildGroupList(Outlook.AddressEntry address, String groupName) // 自建组列表成员list添加到addressList区存储
 {
     Outlook.NameSpace  nameSpace  = address.Application.GetNamespace("MAPI");
     Outlook.MAPIFolder mAPIFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
     for (int i = 0; i < mAPIFolder.Items.Count; i++)
     {
         dynamic dyna = mAPIFolder.Items.GetNext();
         Outlook.DistListItem distListItem = (Outlook.DistListItem)dyna;
         if (groupName == distListItem.DLName)
         {
             this.addressList.Add(distListItem);
         }
         break;
     }
 }
        static void AutomateOutlook()
        {
            object missing = Type.Missing;

            Outlook.Application oOutlook  = null;
            Outlook.NameSpace   oNS       = null;
            Outlook.Folder      oCtFolder = null;
            Outlook.Items       oCts      = null;
            Outlook.MailItem    oMail     = null;

            try
            {
                // Start Microsoft Outlook and log on with your profile.

                // Create an Outlook application.
                oOutlook = new Outlook.Application();
                Console.WriteLine("Outlook.Application is started");

                Console.WriteLine("User logs on ...");

                // Get the namespace.
                oNS = oOutlook.GetNamespace("MAPI");

                // Log on by using a dialog box to choose the profile.
                oNS.Logon(missing, missing, true, true);

                // Alternative logon method that uses a specific profile.
                // If you use this logon method, change the profile name to an
                // appropriate value. The second parameter of Logon is the password
                // (if any) associated with the profile. This parameter exists only
                // for backwards compatibility and for security reasons, and it is
                // not recommended for use.
                //oNS.Logon("YourValidProfile", missing, false, true);

                Console.WriteLine("Press ENTER to continue when Outlook is ready.");
                Console.ReadLine();

                // Enumerate the contact items.

                Console.WriteLine("Enumerate the contact items");

                oCtFolder = (Outlook.Folder)oNS.GetDefaultFolder(
                    Outlook.OlDefaultFolders.olFolderContacts);
                oCts = oCtFolder.Items;

                // Enumerate the contact items. Be careful with foreach loops.
                // See: http://tiny.cc/uXw8S.
                for (int i = 1; i <= oCts.Count; i++)
                {
                    object oItem = oCts[i];

                    if (oItem is Outlook.ContactItem)
                    {
                        Outlook.ContactItem oCt = (Outlook.ContactItem)oItem;
                        Console.WriteLine(oCt.Email1Address);
                        // Do not need to Marshal.ReleaseComObject oCt because
                        // (Outlook.ContactItem)oItem is a simple .NET type
                        // casting, instead of a COM QueryInterface.
                    }
                    else if (oItem is Outlook.DistListItem)
                    {
                        Outlook.DistListItem oDl = (Outlook.DistListItem)oItem;
                        Console.WriteLine(oDl.DLName);
                        // Do not need to Marshal.ReleaseComObject oDl because
                        // (Outlook.DistListItem)oItem is a simple .NET type
                        // casting, instead of a COM QueryInterface.
                    }

                    // Release the COM object of the Outlook item.
                    Marshal.FinalReleaseComObject(oItem);
                    oItem = null;
                }

                // Create and send a new mail item.

                Console.WriteLine("Create and send a new mail item");

                oMail = (Outlook.MailItem)oOutlook.CreateItem(
                    Outlook.OlItemType.olMailItem);

                // Set the properties of the email.
                oMail.Subject  = "Feedback of All-In-One Code Framework";
                oMail.To       = "*****@*****.**";
                oMail.HTMLBody = "<b>Feedback:</b><br />";

                // Displays a new Inspector object for the item and allows users to
                // click on the Send button to send the mail manually.
                // Modal = true makes the Inspector window modal
                oMail.Display(true);
                // [-or-]
                // Automatically send the mail without a new Inspector window.
                //((Outlook._MailItem)oMail).Send();

                // User logs off and quits Outlook.

                Console.WriteLine("Log off and quit the Outlook application");
                oNS.Logoff();
                ((Outlook._Application)oOutlook).Quit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("AutomateOutlook throws the error: {0}", ex.Message);
            }
            finally
            {
                // Manually clean up the explicit unmanaged Outlook COM resources by
                // calling Marshal.FinalReleaseComObject on all accessor objects.
                // See http://support.microsoft.com/kb/317109.

                if (oMail != null)
                {
                    Marshal.FinalReleaseComObject(oMail);
                    oMail = null;
                }
                if (oCts != null)
                {
                    Marshal.FinalReleaseComObject(oCts);
                    oCts = null;
                }
                if (oCtFolder != null)
                {
                    Marshal.FinalReleaseComObject(oCtFolder);
                    oCtFolder = null;
                }
                if (oNS != null)
                {
                    Marshal.FinalReleaseComObject(oNS);
                    oNS = null;
                }
                if (oOutlook != null)
                {
                    Marshal.FinalReleaseComObject(oOutlook);
                    oOutlook = null;
                }
            }
        }