【问题标题】:Outlook MailItem Save/SaveAsOutlook MailItem 保存/另存为
【发布时间】:2010-12-24 06:07:29
【问题描述】:

我有一个 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() + " :: ");
    }
}

【问题讨论】:

  • 您能否详细解释一下您的“快速工作流程”。你是如何连接到发送等的,因为有很多方法可以做到这一点。
  • “提示”我的意思是当 addItem 事件在已发送的项目文件夹上触发时,它会询问用户是否要保存电子邮件。因此,电子邮件在通过发件箱并进入已发送项目后触发事件。如果用户说“是”,他们想保存电子邮件,则主表单打开,其中包含对首先触发事件的电子邮件的引用。
  • 好的,那么您的主表单中如何引用电子邮件?是您的“主要”表单以任何方式更改电子邮件,因此需要保存。您可以检查 isSaved 属性。发布代码可能是一个想法。因为这听起来像是一个订单的事情,或者你以某种方式弄脏了电子邮件。
  • 嘿,感谢您的回复,很抱歉这么久才回复您。我已经编辑了上面的问题以包含代码。 markAsProfiled 函数是更改电子邮件主题然后保存它的功能,如前所述,它会在确认电子邮件已被“分析”后执行此操作。如果我将该函数移动到 myIem.SaveAs 之前运行它工作正常!

标签: c# vb.net outlook outlook-addin mailitem


【解决方案1】:

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

示例代码:

 mail.UserProperties.Add("Profiled", Outlook.OlUserPropertyType.olText, true);
 mail.UserProperties["Profiled"].Value = "Yes";
 mail.Save();

唯一的缺点是您必须将该字段添加到 Outlook 中显示的列中。 (也许可以通过编程方式完成)

关于为什么您的方法不起作用:我可以想象 Outlook 不喜欢您在发送电子邮件后更改主题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2013-12-23
    • 2020-06-08
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多