我有一個outlook加載項,允許用戶將電子郵件保存到數據庫中。當用戶保存電子郵件時,我會修改電子郵件主題,以便將其標識爲保存。Outlook MailItem保存/另存爲
保存電子郵件可能發生在兩個方面。通過工具欄上的一個按鈕,用戶可以保存他們想要的任何電子郵件,也可以通過在將新電子郵件放入已發送郵件文件夾時出現的提示進行操作。兩種方法都使用相同的格式來保存電子郵件!
好了,現在的問題....
在保存電子郵件的過程中,我使用mailItem.SaveAs
方法來把它放到文件存儲。成功完成後,我想更改Outlook中仍存在的電子郵件的主題,說它已成功保存。我通過更改myItem.Subject
然後使用mailItem.Save
方法來保存更改。
當電子郵件未通過提示方法保存時,上述方法完美無缺。因此,當用戶在發送郵件後嘗試保存電子郵件時,mailItem.Save
方法不起作用。
我已經縮小到它實際上工作,如果我把myItem.Save()
線前myItem.SaveAs()
線,但很明顯,如果我這樣做,我不能保證電子郵件實際上保存正確。
因此,任何人都知道mailItem.Save
方法在調用mailItem.SaveAs
方法後不會工作的原因嗎?
非常感謝您提出任何可能導致問題的建議。
編輯:代碼
if (_item is Outlook.MailItem) { // if the incoming item is an Outlook mail Item
// cast as a mail item
Outlook.MailItem myItem = (Outlook.MailItem)_item;
if (directoryExists(directoryTemp)) { // if the temporary directory exists
bool _profiled = true;
// copy the item as type .msg in the temporary location
myItem.SaveAs(saveTemp, Outlook.OlSaveAsType.olMSG);
// setup impersonation to copy the file to a secure location
PImpersonateUser _iU = new PImpersonateUser();
// do impersonation
try {
_iU.Impersonate("******", "******", "******");
if (File.Exists(savefile)) { // if file already exists in the location
// delete existing file
File.Delete(savefile);
}
// move the temporary file to the secure location with the proper name
File.Move(saveTemp, savefile);
string year = "";
if (ipt_year.SelectedItem != null) { // else if year has been selected
year = ipt_year.SelectedItem.ToString();
}
_profile.profileEmail(folderString(_subject_), _fileName, year);
} catch (Exception e) {
_profiled = false;
// if impersonation fails cancel the impersonation
_iU.Undo();
// show error
MessageBox.Show(e.Source + "\n\n" + e.Message + "\n\n" + e.StackTrace, "SaveAs() Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
} finally {
_iU.Undo();
}
if (_profiled) { // if the email was profiled successfully
// mark the original email as being profiled
markAsProfiled();
}
} else {
// if temporary file save fails throw error
MessageBox.Show("Temporary Directory (" + directoryTemp + ") Does Not Exist!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
和markAsProfiled功能...
private void markAsProfiled() {
if (_item is Outlook.MailItem) { // if the incoming item is an Outlook mail Item
// cast as a mail item
Outlook.MailItem myItem = (Outlook.MailItem)_item;
// make sure subject doesnt already have a profiled flag in the subject
_subject_ = _subject_.Replace("[PROFILED] - ", "");
// add a profiled flag in the subject of the email
myItem.Subject = "[PROFILED] - " + _subject_;
// add a yellow flag to the email
myItem.FlagIcon = Microsoft.Office.Interop.Outlook.OlFlagIcon.olYellowFlagIcon;
// save email with changes made
myItem.Save();
//MessageBox.Show("Mark as Profiled :: " + myItem.Subject + " :: " + myItem.Saved.ToString() + " :: ");
}
}
您能否在al ittel中更詳細地解釋您的「即時工作流程」。你如何吸引發送等,因爲有很多方法可以做到這一點。 – 76mel 2009-12-08 10:54:54
通過「提示」我的意思是,當addItem事件觸發發送的項目文件夾時,它會詢問用戶是否要保存電子郵件。因此,電子郵件在通過發件箱並進入已發送郵件後觸發事件。如果用戶說「是」,他們想要保存電子郵件,那麼主窗體會打開,首先引用關閉事件的電子郵件。 – Stuv 2009-12-09 03:05:31
好的,那麼你的主表格中引用的電子郵件是怎麼樣的?是您的「主要」表單以任何方式更改電子郵件,因此需要保存。您可以檢查isSaved屬性。發佈代碼可能是一個想法。因爲它聽起來像是訂單,或者你以某種方式使郵件變得骯髒。 – 76mel 2009-12-10 14:29:06