簡體   English   中英

如果 Outlook 打開,則只能通過 Outlook 發送電子郵件

[英]Can only send email via Outlook if Outlook is open

我想按照此處所述通過 Outlook 使用發送電子郵件。 只要我已經打開 Outlook,它就可以正常工作。 因此,例如,如果將 Outlook 最小化並執行我的代碼,那么我就可以正常發送電子郵件。 但如果 Outlook 已關閉,則會出現異常:

{System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
   at Microsoft.Office.Interop.Outlook._MailItem.get_Recipients()
   at OutlookExample.Form1.btnSendEmail_Click(Object sender, EventArgs e) in C:\Users\abc\Documents\Visual Studio 2008\Projects\OutlookExample\OutlookExample\Form1.cs:line 28}

這是代碼:

using Outlook = Microsoft.Office.Interop.Outlook;

...

private void btnSendEmail_Click(object sender, EventArgs e)
{
    try
    {
        Outlook.Application oApp = new Outlook.Application();
        Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
            oMsg.HTMLBody = "Hello, here is your message!";
            oMsg.Subject = "This is a test message";
            Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
            Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("rhr@sonlinc.dk");
            oRecip.Resolve();
            oMsg.Send();
            oRecip = null;
            oRecips = null;
            oMsg = null;
            oApp = null;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

為什么這不起作用?

編輯:這是解決方案

using Outlook = Microsoft.Office.Interop.Outlook;

...

private void btnSendEmail_Click(object sender, EventArgs e)
{
    try
    {
        Outlook.Application oApp = new Outlook.Application();

        // These 3 lines solved the problem
        Outlook.NameSpace ns = oApp.GetNamespace("MAPI");
        Outlook.MAPIFolder f = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
        System.Threading.Thread.Sleep(5000); // test

        Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
            oMsg.HTMLBody = "Hello, here is your message!";
            oMsg.Subject = "This is a test message";
            Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
            Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("rhr@sonlinc.dk");
            oRecip.Resolve();
            oMsg.Send();
            oRecip = null;
            oRecips = null;
            oMsg = null;
            oApp = null;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

以下代碼已經可靠地為我工作了幾個月:

            app = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.NameSpace ns = app.GetNamespace("MAPI");
            f = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            Thread.Sleep(5000); // a bit of startup grace time.

如果 outlook 已打開,則使用它,如果未打開,則使用它。 當然,如果您的 outlook 要求您登錄,您的代碼將不允許這樣做。 某些系統使您難以自動登錄。

我不喜歡使用 Thread.Sleep 5 秒的想法,所以我找到了另一個對我有用的解決方案:

您只需要為新創建的 MailItem 獲取 Inspector 對象

Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector oInspector = oMsg.GetInspector;

答案最初是針對 Outlook 2007 在Google 群組中發布的(但它適用於 Outlook 2010)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM