Outlook 邮件项目保存/另存为
我有一个 Outlook 插件,允许用户将电子邮件保存到数据库中。当用户保存电子邮件时,我会修改电子邮件主题,以便将其识别为已保存。
保存电子邮件可以通过两种方式进行。通过工具栏上的按钮,用户可以保存他们想要的任何电子邮件,也可以通过将新电子邮件放入“已发送邮件”文件夹时出现的提示。两种方法都使用相同的表单来保存电子邮件!
好的,现在解决问题......
在保存电子邮件的过程中,我使用 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() + " :: ");
}
}
I have an outlook add-in that allows the user to save an email into a database. When the user does save the email I modify the email subject so it can be identified as being saved.
Saving the email can happen in two ways. Via a button on the tool bar which allows the user to save any email they want, and also via a prompt which appears when a new email is put into the Sent Items folder. Both methods use the same form to save the email!
OK, now to the problem ....
In the process of saving the email I use the mailItem.SaveAs
method to put it into the file store. After this has completed successfully i want to change the subject of the email which still exists in outlook to say that it has been saved successfully. I do this by changing myItem.Subject
and then using the mailItem.Save
method to save the change.
The above works perfectly when the email isn't being saved via the prompt method. So when the user tries to save the email after they send it the mailItem.Save
method does not work.
I have narrowed it down to it actually working if i put the myItem.Save()
line before the myItem.SaveAs()
line, but obviously if I do this I can not guarantee the email was actually saved properly.
So does any one know of a reason that the mailItem.Save
method would want to not work after the mailItem.SaveAs
method as been called?
Thank you in advance to any suggestions to what might be the problem.
EDIT : Code
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);
}
}
and the markAsProfiled function ...
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() + " :: ");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这仍然与您相关:您可以使用自定义列,在其中可以写入保存是否成功。
示例代码:
唯一的缺点是您必须将字段添加到 Outlook 中显示的列中。 (也许可以通过编程方式完成)
关于为什么您的方法不起作用:我可以想象 Outlook 不喜欢您在发送电子邮件后更改主题。
if this is still relevant to you: You could use a self-defined column in which you could write wether the saving was successfull or not.
Example code:
The only disadvantage is that you've to add the field to the displayed columns in Outlook. (maybe that can be done programmatically)
About why your method doesn't work: I could imagine that Outlook doesn't like it when you change the subject of an email after it was sent.