我正在创建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);
}
当用户发送电子邮件时,我保存了它。但我的电子邮件保存在撰写模式。如果我将在OutLook中打开它,我可以更改所有信息,我可以重新发送。
如果我订阅了MailItem Inspector关闭事件:
((InsPectorsEvents_10_Event)inspector).Close += CloseEventHandler;
并在CloseEventHandler中保存MailItem,我有一个错误:
“项目已被移动或删除”。
如何在“已发送项目”Outlook文件夹中发送并移动后保存我的MailItem? (保存在读取模式)
编辑
我正在使用Items.AddItem事件来保存我发送的_mailItem。获取文件夹:
_sentFolderItems = _mailItem.SaveSentMessageFolder;
订阅_addEventHandler中的一些逻辑:
_sentFolderItems.Items.ItemAdd += _addEventHandler;
好:
为:
Outlook主窗口未运行,任务管理器中没有进程。 我正在生成MailItem,为用户显示它。只有用于发送为用户显示的电子邮件的窗口。 Outlook进程显示在任务管理器中。用户可以根据需要进行一些更改,然后按发送按钮。邮件被发送,任务管理器没有OutLook进程。 未触发Items.ItemAdd且未执行_addEventHandler。 但是corse发送的项目位于发送文件夹中。
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);
}
}
答案 0 :(得分:0)
在“已发送邮件”文件夹中使用Items.ItemAdd
事件。