首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Outlook MailItem Save/SaveAs

Outlook MailItem Save/SaveAs
EN

Stack Overflow用户
提问于 2009-12-08 01:27:37
回答 1查看 14.1K关注 0票数 0

我有一个outlook插件,允许用户将电子邮件保存到数据库中。当用户保存电子邮件时,我修改电子邮件主题,以便将其标识为已保存。

保存电子邮件可以通过两种方式进行。通过工具栏上的一个按钮,用户可以保存他们想要的任何电子邮件,也可以通过一个提示,当一个新的电子邮件被放入“发送的邮件”文件夹中。这两种方法都使用相同的形式保存电子邮件!

好了,现在来谈谈问题..。

在保存电子邮件的过程中,我使用mailItem.SaveAs方法将其放入文件存储中。在此成功完成之后,我想改变outlook中仍然存在的电子邮件的主题,说它已经成功保存了。为此,我更改myItem.Subject,然后使用mailItem.Save方法保存更改。

当电子邮件没有通过提示方法保存时,上面的功能非常好。因此,当用户在发送电子邮件后尝试保存电子邮件时,mailItem.Save方法不起作用。

如果我把myItem.Save()行放在myItem.SaveAs()行之前,我已经缩小了它的范围,但是很明显,如果我这样做了,我不能保证电子邮件实际上是正确保存的。

那么,有谁知道mailItem.Save方法在mailItem.SaveAs方法被调用之后不工作的原因吗?

事先感谢您对可能出现的问题提出任何建议。

编辑:代码

代码语言:javascript
复制
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函数..。

代码语言:javascript
复制
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() + " :: ");
    }
}
EN

回答 1

Stack Overflow用户

发布于 2012-11-20 09:01:57

如果这仍然与您相关:您可以使用一个自定义的列,您可以在该列中写入保存是否成功。

示例代码:

代码语言:javascript
复制
 mail.UserProperties.Add("Profiled", Outlook.OlUserPropertyType.olText, true);
 mail.UserProperties["Profiled"].Value = "Yes";
 mail.Save();

唯一的缺点是必须将字段添加到Outlook中显示的列中。(也许可以通过编程来实现)

关于您的方法不起作用的原因:我可以想象,Outlook不喜欢在发送邮件后更改邮件主题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1864008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档