选择启动Outlook时显示的配置文件提示

时间:2019-06-03 13:42:57

标签: c# outlook office-interop

我想使用c#脚本获取Outlook,但始终显示“选择配置文件”提示。在控制面板>>邮件>> Pofile中,将我的“ Outlook”配置文件设置为默认配置。如果我启动Outlook,则代码可以正常运行。如果我启动Outlook并关闭它,有时会出现提示。如果我启动Outlook并将其终止,则在运行代码时会显示提示。

我不知道如何以编程方式避免出现这种提示。请在下面查看我的代码:

public static void SendMail(string ToStr, string CCStr, string BCCStr, string Subject, string Body, string AttachmentStr, string AccountUserName = "")
    {
        OL.Application OlApp = null;
        OL.NameSpace NS = null;
        OL.Account Account = null;
        OL.MAPIFolder MFold = null;
        OL.MailItem MI = null;
        OL.Recipients oReci = null;

        bool StillRunning = false;
        bool FoundAccount = false;
        string[] To = ToStr.Split(';');
        string[] CC = CCStr.Split(';');
        string[] BCC = BCCStr.Split(';');
        string[] Attachment = AttachmentStr.Split(';');

        try { OlApp = GetOutlook(out StillRunning); } catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to get Outlook.Application instance." + System.Environment.NewLine + "Error details: " + ex.Message); }

        try
        {
            NS = OlApp.GetNamespace("MAPI");
            NS.Logon("Outlook", "", false, true); /*has no effect on the prompt*/
        }
        catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to get namespace folder 'MAPI'" + System.Environment.NewLine + "Error details: " + ex.Message); }

        try
        {
            if (AccountUserName != string.Empty)
            {
                foreach (OL.Account Acc in NS.Accounts)
                {
                    //System.Console.WriteLine(Acc.UserName);
                    if (Acc.UserName == AccountUserName)
                    {
                        Account = Acc;
                        FoundAccount = true;
                        break;
                    }
                }

                if (!FoundAccount) { throw new System.Exception("ERROR: Unable to find account '" + AccountUserName + "' for sending mail!"); }
            }
        }
        catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to get target account '" + AccountUserName + "'!" + System.Environment.NewLine + "Error details: " + ex.Message); }

        try
        {
            MFold = NS.GetDefaultFolder(OL.OlDefaultFolders.olFolderOutbox);
        }
        catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to get default folder!" + System.Environment.NewLine + "Error details: " + ex.Message); }

        try
        {
            MI = (OL.MailItem)OlApp.CreateItem(OL.OlItemType.olMailItem);
        }
        catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to create new mail item!" + System.Environment.NewLine + "Error details: " + ex.Message); }

        try
        {
            oReci = MI.Recipients;
        }
        catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to define recipients!" + System.Environment.NewLine + "Error details: " + ex.Message); }

        foreach (string str in To)
        {
            if (str.Trim() != string.Empty)
            {
                try
                {
                    OL.Recipient Rec = MI.Recipients.Add(str.Trim());
                    Rec.Type = (int)OL.OlMailRecipientType.olTo;
                }
                catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to define 'To' recipient!" + System.Environment.NewLine + "Error details: " + ex.Message); }
            }
        }

        foreach (string str in CC)
        {
            if (str.Trim() != string.Empty)
            {
                try
                {
                    OL.Recipient Rec = MI.Recipients.Add(str.Trim());
                    Rec.Type = (int)OL.OlMailRecipientType.olCC;
                }
                catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to define 'Cc' recipient!" + System.Environment.NewLine + "Error details: " + ex.Message); }
            }
        }

        foreach (string str in BCC)
        {
            if (str.Trim() != string.Empty)
            {
                try
                {
                    OL.Recipient Rec = MI.Recipients.Add(str.Trim());
                    Rec.Type = (int)OL.OlMailRecipientType.olBCC;
                }
                catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to define 'Bcc' recipient!" + System.Environment.NewLine + "Error details: " + ex.Message); }
            }
        }

        try
        {
            MI.Body = Body;
            if (AccountUserName != string.Empty)
            {
                MI.SendUsingAccount = Account;
            }
        }
        catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to define email subject and body!" + System.Environment.NewLine + "Error details: " + ex.Message); }

        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;

        try
        {
            MI.Send();
        }
        catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to send mail!" + System.Environment.NewLine + "Error details: " + ex.Message); }

        while (nOutItems != MFold.Items.Count)
        {
            System.Threading.Thread.Sleep(250);
        }

        if (!StillRunning)
        {
            OlApp.Application.Quit();
            OlApp.Quit();
            KillOutlook();
        }
    }

        public static OL.Application GetOutlook(out bool StillRunning)
    {
        OL.Application OLApp = null;

        if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Count() > 0)
        {
            StillRunning = true;
            return System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
        }
        else
        {
            StillRunning = false;
            OLApp = new OL.Application();
            OL.NameSpace nameSpace = OLApp.GetNamespace("MAPI");
            nameSpace.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value);
            nameSpace = null;
            return OLApp;
        }
    }

0 个答案:

没有答案