'The item is moved or Deleted' Outlook.mailItem

'The item is moved or Deleted' Outlook.mailItem

我这样创建 outlook 邮件:

 Outlook.Application outlookApp = new Outlook.Application();
 Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
 mailItem.Subject = mailSubject;
 mailItem.To = "";
 mailItem.Attachments.Add(totalPath);
 mailItem.Body = mailBody;
 mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
 mailItem.Display(true);
 //Mail is send succesfully?

是否可以查看邮件是发送成功还是取消?

编辑: 当我执行下面的代码时,出现错误 'the item is moved or deleted'

Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = mailSubject;
mailItem.To = "";
mailItem.CC = "EDI.Orders@trust.com";
mailItem.Attachments.Add(totalPath);
mailItem.Body = mailBody;
mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
mailItem.Display(true);
//Mail is send succesfully?
if (mailItem.Sent)
{
   MessageBox.Show("Sended");
}
else
{
   MessageBox.Show("canceled");
}

错误出现在 mailItem.Sent。 我的问题是 mailItem.Sent 是如何工作的?

mailItem.Sent 属性 显示电子邮件是否已发送。

此外,您还可以附加到 mailItem.Write 事件。 (但要小心 - 也会在保存等时被调用)

mailItem.Write += mailItem_Write;

void mailItem_Write(ref bool Cancel)
    {
       //your code...
    }

MailItem.Sent 属性 returns 一个布尔值,指示是否已发送消息。

针对您的问题;

MailItem.Sent Property (Outlook)

Returns a Boolean value that indicates if a message has been sent. Read-only.

https://msdn.microsoft.com/EN-US/library/office/ff868242.aspx

在调用 Display 后检查此 属性 时,我怀疑由于该项目不再处于活动状态,因此对 Sent 的调用失败。您可以确认邮件是否确实已发送的一种方法是处理 Outlook.Application class.

ItemSend 事件
        Outlook.Application outlookApp = new Outlook.Application();

        outlookApp.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(outlookApp_ItemSend);

        Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
        mailItem.Subject = "Test";
        mailItem.To = "a.b@c.com";
        mailItem.CC = "";

        mailItem.Body = "Test Message";
        mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
        mailItem.Display(true);

并在发送事件处理程序中检查消息;

void outlookApp_ItemSend(object Item, ref bool Cancel)
{
    if (Item is Outlook.MailItem)
    {
        string subject = ((Outlook.MailItem)Item).Subject;


        if (subject == "Test")
        {
            //Item sent
        }


    }
}

这是一个不会阻塞 outlook 的解决方案。

class Class1
{
    static bool mailWasSent = false;
    private static System.Windows.Forms.ApplicationContext _context;
    static void Main(string[] args)
    {
        Outlook.Application outlookApp = new Outlook.Application();
        Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
        mailItem.Subject = "My Subject";
        mailItem.To = "";
        mailItem.Attachments.Add(@"C:\test.pdf");
        mailItem.Body = "This is my Body-Text";
        mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
        ((Outlook.ItemEvents_10_Event)mailItem).Close += MailItem_onClose;
        ((Outlook.ItemEvents_10_Event)mailItem).Send += MailItem_onSend;

        //mailItem.Display(true);   // This call will make mailItem MODAL - 
        // This way, you are not allowed to create another new mail, ob browse Outlook-Folders while mailItem is visible

        // Using ApplicationContext will wait until your email is sent or closed without blocking other Outlook actions.
        using (_context = new System.Windows.Forms.ApplicationContext())
        {
            mailItem.Display();
            System.Windows.Forms.Application.Run(_context);     
        }
        if (mailWasSent)
        {
            System.Windows.Forms.MessageBox.Show("Email was sent");
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("Email was NOT sent");
        }
    }

    private static void MailItem_onSend(ref bool Cancel)
    {
        mailWasSent = true;
    }

    private static void MailItem_onClose(ref bool Cancel)
    {
        _context.ExitThread();
    }
}