发送后如何保存 MailItem

How to save MailItem after sending

我正在创建 MailItem:

    Application outlook = new Application();
    _mailItem = outlook.CreateItem(OIItemType.olMailItem);                
    Inspector inspector = _mailItem .GetInspector;
    inspector.Activate();     

设置收件人、正文等... 订阅发送事件:

    ((ItemEvents_10_Event)_mailItem).Send += MailItemSendHandler;

    void MailItemSendHandler(ref bool isSended) {
        _mailItem.SaveAs(filePath);
    }

当用户发送电子邮件时,我将其保存。但是我的电子邮件以 compose 模式保存。如果我将在 OutLook 中打开它,我可以更改所有信息并重新发送它。

如果我订阅 MailItem Inspector 关闭事件:

 ((InsPectorsEvents_10_Event)inspector).Close += CloseEventHandler;

并在 CloseEventHandler 中保存 MailItem,但出现错误:

"Item has been moved or deleted".

我的 MailItem 被发送并移动到 "Sent items" Outlook 文件夹后如何保存? (以读取模式保存)

编辑

我正在使用 Items.AddItem 事件来保存我发送的 _mailItem。获取文件夹:

_sentFolderItems = _mailItem.SaveSentMessageFolder;

订阅 _addEventHandler 中的一些逻辑:

_sentFolderItems.Items.ItemAdd += _addEventHandler;

好:

  1. Outlook 主 window 是 运行。
  2. Outlook 进程显示在任务管理器中。
  3. 我正在生成 MailItem,将其显示给用户。用户根据需要进行一些更改,然后按发送按钮。
    4。 Items.ItemAdd 被触发并执行 _addEventHandler。
  4. Outlook邮件window依旧运行,任务管理器中显示outlook进程

差:

Outlook mainwindow不是运行,任务管理器中也没有进程。 我正在生成 MailItem,将其显示给用户。只有 window 用于发送电子邮件显示给用户。 Outlook 进程显示在任务管理器中。用户根据需要进行一些更改,然后按发送按钮。邮件已发送,但任务管理器没有 OutLook 进程。 Items.ItemAdd 未触发且 _addEventHandler 未执行。 但最重要的是已发送的项目位于已发送文件夹中。

 public class MailItemWrapper {
    public MailItemWrapper(MailItem mailItem, ComposeEmailWrapper composeEmailWrapper, bool isCompose) {
        _mailItem = mailItem;
        _identifyProperty = Guid.NewGuid();
        _mailItem.AddUserProperty(_identifyProperty.ToString(), _identifyProperty.ToString());
        _sentFolderItems = _mailItem.SaveSentMessageFolder;
        _inspector = _mailItem.GetInspector;
        _composeEmailWrapper = composeEmailWrapper;
        InComposeMode = isCompose;
        SetEventHandlers();
        Subscribe();
        _isSending = false;
    }

    private MailItem _mailItem;
    private Inspector _inspector;
    private MAPIFolder _sentFolderItems;
    private InspectorEvents_10_CloseEventHandler _closeEventHandler;
    private ItemEvents_10_SendEventHandler _sendEventHandler;
    private ItemsEvents_ItemAddEventHandler _addEventHandler;
    private readonly ComposeEmailWrapper _composeEmailWrapper;
    private string _path;
    private bool _isSending;
    private Guid _identifyProperty;

    public bool InComposeMode {
        get; set;
    }

    public MailItem MailItem {
        get {
            return _mailItem;
        }
    }

    public void Subscribe() {
        ((ItemEvents_10_Event) _mailItem).Send += _sendEventHandler;
        ((InspectorEvents_10_Event) _inspector).Close += _closeEventHandler;
        _sentFolderItems.Items.ItemAdd += _addEventHandler;
    }

    public void UnsubscribeAndRelease() {
        if(InComposeMode) {
            ((ItemEvents_10_Event) _mailItem).Send -= _sendEventHandler;
            ((InspectorEvents_10_Event) _inspector).Close -= _closeEventHandler;
            _sentFolderItems.Items.ItemAdd -= _addEventHandler;

            Marshal.ReleaseComObject(_sentFolderItems);
            Marshal.ReleaseComObject(_mailItem);
            Marshal.ReleaseComObject(_inspector);

            _sentFolderItems = null;
            _mailItem = null;
            _inspector = null;
            InComposeMode = false;
            _isSending = false;
        }
    }

    private void SetEventHandlers() {
        _sendEventHandler = (ref bool cancel) =>{           
            _isSending = true;
        };

        _addEventHandler = delegate (object item) {
            var mailItem = item as MailItem;
            if(mailItem != null) {
                var identityer = mailItem.UserProperties.Find(_identifyProperty.ToString());
                if(identityer != null && _identifyProperty.ToString() == identityer.Value) {
                    _path = mailItem.SaveAsInTempFolder(); // extension
                    if(_composeEmailWrapper != null && _composeEmailWrapper.Callback != null) {
                        System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (System.Action) ExecuteCallBack);
                        UnsubscribeAndRelease();
                    }
                }
                Marshal.ReleaseComObject(mailItem);
            }

        };

        _closeEventHandler = () => {
            // if user close dialog without sending => unsibscribe
            if(!_isSending) {
                UnsubscribeAndRelease();
            }
        };
    }

    private void ExecuteCallBack() {
        _composeEmailWrapper.Callback(_path, _composeEmailWrapper.SessionId);
    }
}

对“已发送邮件”文件夹使用 Items.ItemAdd 事件。