簡體   English   中英

如何取消從Outlook發送郵件並關閉表單

[英]how to cancel sending a message from Outlook and close the form

我嘗試取消從Outlook發送郵件。 我需要從mailItem中獲取所有字段並關閉而不發送。

在我的代碼中,我得到一個錯誤-無法在事件Item.Send中執行命令Item.Close。

public partial class MyMainForm
{
    Outlook.Application oApp;
    Outlook.MailItem mailItem;
    System.Threading.Timer timer_CloseMail;

  private void Mail_in_outlook()
    {
        oApp = new Outlook.Application();
        mailItem = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

        TimerCallback tm = new TimerCallback(CloseMail);
        timer_CloseMail = new Timer(tm);

        oApp.ItemSend += oApp_ItemSend;
        mailItem.DeleteAfterSubmit = true;

        mailItem.Display(true);

        // get mailItem.HTMLBody and other field....

    }

void oApp_ItemSend(object Item,ref bool Cancel)
    {
        Cancel = true;
        MailtimeTimer.Change(0, 0);
    }

private void CloseMail(object sender)
    {
        mailItem.Close(Outlook.OlInspectorClose.olDiscard);
    }

不能在該事件處理程序中調用某些MAilItem方法(例如Send或Close)。 您可以使用一個計時器(由於它在同一線程上運行,因此可以使用Forms命名空間中的計時器)並在ItemSEnd事件中啟用它。 當計時器觸發時,將其禁用並調用MailItem.Close-到那時,您將不在ItemSent事件中。

暫無
暫無

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

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