-->

只能通过Outlook收发电子邮件如果Outlook处于打开状态(Can only send ema

2019-06-26 01:21发布

我想用所描述的通过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());
    }
}

Answer 1:

下面的代码工作可靠几个月了,我说:

            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要求你登录,您的代码不会允许为。 有些系统很难让你自动登录。



Answer 2:

我不喜欢这个主意,用了Thread.Sleep 5秒,所以我找到了另一种解决方案,为我工作:

所有你需要的就是检查对象为新创建的

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

答案刊登在谷歌组原本为Outlook 2007中(但它与Outlook 2010工作对我来说)



文章来源: Can only send email via Outlook if Outlook is open