通过 Outlook API 发送邮件 - Outlook 关闭速度很快

Sending mail via Outlook API - Outlook closes to fast

我写了一个 class 来通过 outlook 自动发送电子邮件。除了小案例,一切都很好,邮件已生成,放入 "outgoing" 文件夹,然后 outlook 关闭。关闭的太快了,下次打开outlook就发邮件了

这是我的代码:

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;
        }
        */
    }
}

我评论的"waiting loop"没用,因为outlook正在关闭 在函数 Mail.Send().

有没有人知道,我怎样才能让 outlook 等到邮件发送?

您好, 一月

Outlook 将在最后一个资源管理器或检查器关闭后立即关闭。为防止出现这种情况,要么确保显示浏览器或检查器,要么至少保留对这些对象之一的引用(它们不必是可见的)。可以从 MailItem.GetInspectorMAPIFolder.GetExplorer.

检索检查员

您可能还想使用 Namespace.SendAndRecive 发送消息。请记住 SendAndRecive 是异步的,因此您需要等待 SyncObject.SyncEnd 事件触发(到那时您可以安全地释放资源管理器或检查器并让 Outlook 关闭)。可以从 Namespace.SyncObjects 集合中检索 SyncObject。

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

    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();
        }
    }
}

首先,我会遵循此处列出的德米特里的建议。

我还注意到您在调用 MailItem class 的 Send 方法之前显示了检查器 window。这不是必需的可能会导致您当前看到的行为。需要承认所有 Outlook 对象都是在方法范围内声明的,这将生命周期限制在方法中。

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 事件。当一个或多个项目添加到指定的集合时,将触发该事件。