代码之家  ›  专栏  ›  技术社区  ›  Jan021981

通过Outlook API发送邮件-Outlook关闭至fast

  •  0
  • Jan021981  · 技术社区  · 7 年前

    public class MyMail
    {
        private const double WaitingForSending = 30.0;
    
        #region Local variables
        public bool SSL_Encryption = true;
        public System.Collections.Generic.List<System.Net.Mail.MailAddress> Address = null;
        public System.Collections.Generic.List<System.Net.Mail.MailAddress> CC = null;
        public System.Collections.Generic.List<System.Net.Mail.MailAddress> BCC = null;
        public System.Collections.Generic.List<string> AttachmentFileName = null;
        public string Body = "";
        public string Subject = "";
        #endregion
    
    
        public void SendMail()
        {
            double Waited = .0;
            string fAddress = string.Empty;
    
            if (this.Address == null) { return; }
            Microsoft.Office.Interop.Outlook.Application OL = new Microsoft.Office.Interop.Outlook.Application();
    
            Microsoft.Office.Interop.Outlook.MailItem Mail = (Microsoft.Office.Interop.Outlook.MailItem)OL.CreateItem(OlItemType.olMailItem);
            Mail.Subject = this.Subject;
            if (this.Address != null) { foreach (System.Net.Mail.MailAddress MA in this.Address) { fAddress += MA.Address + "; "; } Mail.To = fAddress; fAddress = string.Empty; }
            if (this.CC != null) { foreach (System.Net.Mail.MailAddress MA in this.CC) { fAddress += MA.Address + "; "; } Mail.CC = fAddress; fAddress = string.Empty; }
            if (this.BCC != null) { foreach (System.Net.Mail.MailAddress MA in this.BCC) { fAddress += MA.Address + "; "; } Mail.BCC = fAddress; fAddress = string.Empty; }
            Mail.Body = this.Body;
            if (this.AttachmentFileName != null) { foreach (string Att in this.AttachmentFileName) { if (System.IO.File.Exists(Att)) { Mail.Attachments.Add(Att, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing); } } }
            Mail.Display(false);
    
            try
            {
                Mail.Send();
            } catch (System.Exception ex) { throw ex; }
    
            /*
            while (!Mail.Sent && Waited < WaitingForSending)
            {
                System.Threading.Thread.Sleep(500);
                Waited += 0.5;
            }
            */
        }
    }
    

    我评论说“等待循环”不起作用,因为outlook正在关闭 在函数中 Mail.Send() .

    3 回复  |  直到 7 年前
        1
  •  0
  •   Dmitry Streblechenko    7 年前

    Outlook将在最后一个浏览器或检查器关闭时立即关闭。为了防止这种情况,请确保显示explorer或inspector,或者至少保留对其中一个对象的引用(它们不必可见)。可以从以下位置检索检查器: MailItem.GetInspector MAPIFolder.GetExplorer

    您可能还想使用 Namespace.SendAndRecive 发送消息。请记住 SendAndRecive SyncObject.SyncEnd 要激发的事件(到那时,您可以安全地释放explorer或inspector并关闭Outlook)。SyncObject可以从 Namespace.SyncObjects 收集

        2
  •  0
  •   Eugene Astafiev    7 年前

    首先,我会遵循上面列出的Dmitry的建议。

    Send

    public class MyMail
    {
        private const double WaitingForSending = 30.0;
    
        #region Local variables
        public bool SSL_Encryption = true;
        public System.Collections.Generic.List<System.Net.Mail.MailAddress> Address = null;
        public System.Collections.Generic.List<System.Net.Mail.MailAddress> CC = null;
        public System.Collections.Generic.List<System.Net.Mail.MailAddress> BCC = null;
        public System.Collections.Generic.List<string> AttachmentFileName = null;
        public string Body = "";
        public string Subject = "";
        #endregion
    
        private Microsoft.Office.Interop.Outlook.Application OL;
        private Microsoft.Office.Interop.Outlook.MailItem Mail;
    
        public void SendMail()
        {
            double Waited = .0;
            string fAddress = string.Empty;
    
            if (this.Address == null) { return; }
            OL = new Microsoft.Office.Interop.Outlook.Application();
    
            Mail = (Microsoft.Office.Interop.Outlook.MailItem)OL.CreateItem(OlItemType.olMailItem);
            Mail.Subject = this.Subject;
            if (this.Address != null) { foreach (System.Net.Mail.MailAddress MA in this.Address) { fAddress += MA.Address + "; "; } Mail.To = fAddress; fAddress = string.Empty; }
            if (this.CC != null) { foreach (System.Net.Mail.MailAddress MA in this.CC) { fAddress += MA.Address + "; "; } Mail.CC = fAddress; fAddress = string.Empty; }
            if (this.BCC != null) { foreach (System.Net.Mail.MailAddress MA in this.BCC) { fAddress += MA.Address + "; "; } Mail.BCC = fAddress; fAddress = string.Empty; }
            Mail.Body = this.Body;
            if (this.AttachmentFileName != null) { foreach (string Att in this.AttachmentFileName) { if (System.IO.File.Exists(Att)) { Mail.Attachments.Add(Att, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing); } } }
            // the following method is not required for sending emails
            // Mail.Display(false);
    
            try
            {
                Mail.Send();
            } catch (System.Exception ex) { throw ex; }           
        }
    }
    

    确保消息成功发送的最佳方法是处理 ItemAdd 事件。在将一个或多个项添加到指定集合时激发事件。

        3
  •  0
  •   Jan021981    6 年前

    我现在找到了另一个解决方案。请参阅附件中的工作解决方案:

        public static class MAPIOutlook
    {
        public static OL.Application GetOutlook(out bool StillRunning)
        {
            OL.Application OLApp = null;
            OL.NameSpace nameSpace = null;
    
            if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Count() > 0)
            {
                StillRunning = true;
                try
                {
                    OLApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
                }
                catch {
                    KillOutlook();
                    OLApp = new OL.Application();
                    nameSpace = OLApp.GetNamespace("MAPI");
                    nameSpace.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                }
            }
            else
            {
                StillRunning = false;
                OLApp = new OL.Application();
                nameSpace = OLApp.GetNamespace("MAPI");
                nameSpace.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value);
            }
    
            return OLApp;
        }
    
    
        public static void KillOutlook()
        {
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("OUTLOOK")) { p.Kill(); }
        }
    
        public static void SendMailAdv(string[] To, string[] CC, string[] BCC, string Subject, string Body, string[] Attachment)
        {
            bool StillRunning = false;
            OL.Application OlApp = GetOutlook(out StillRunning);
            OL.NameSpace NS = OlApp.GetNamespace("MAPI");
            OL.MAPIFolder MFold = NS.GetDefaultFolder(OL.OlDefaultFolders.olFolderOutbox);
            OL.MailItem MI = (OL.MailItem)OlApp.CreateItem(OL.OlItemType.olMailItem);
            OL.Recipients oReci = MI.Recipients;
    
            foreach(string str in To)
            {
                OL.Recipient Rec = MI.Recipients.Add(str);
                Rec.Type = (int)OL.OlMailRecipientType.olTo;
            }
    
            foreach (string str in CC)
            {
                OL.Recipient Rec = MI.Recipients.Add(str);
                Rec.Type = (int)OL.OlMailRecipientType.olCC;
            }
    
            foreach (string str in To)
            {
                OL.Recipient Rec = MI.Recipients.Add(str);
                Rec.Type = (int)OL.OlMailRecipientType.olBCC;
            }
    
            MI.Subject = Subject;
            MI.Body = Body;
    
            foreach(string str in Attachment)
            {
                if (System.IO.File.Exists(str.Trim()))
                {
                    MI.Attachments.Add(str.Trim(), OL.OlAttachmentType.olByValue, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                }
            }
    
            int nOutItems = MFold.Items.Count;
    
            MI.Send();
    
            while(nOutItems != MFold.Items.Count)
            {
                System.Threading.Thread.Sleep(250);
            }
    
            if (!StillRunning)
            {
                OlApp.Application.Quit();
                OlApp.Quit();
                KillOutlook();
            }
        }
    }