简体   繁体   English

Outlook MailItem另存为

[英]Outlook MailItem Save/SaveAs

I have an outlook add-in that allows the user to save an email into a database. 我有一个Outlook加载项,允许用户将电子邮件保存到数据库中。 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. 在保存电子邮件的过程中,我使用mailItem.SaveAs方法将其放入文件存储中。 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. 成功完成此操作后,我想更改Outlook中仍然存在的电子邮件主题,以表示已成功保存。 I do this by changing myItem.Subject and then using the mailItem.Save method to save the change. 我可以通过更改myItem.Subject ,然后使用mailItem.Save方法来保存更改来实现。

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. 因此,当用户在发送电子邮件后尝试保存电子邮件时, mailItem.Save方法不起作用。

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. 我已经把范围缩小到实际工作,如果我把myItem.Save()行前myItem.SaveAs()线,但很明显,如果我这样做,我不能保证电子邮件实际上是保存正常。

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? 那么,有人知道在mailItem.Save方法之后, mailItem.SaveAs方法将不起作用的mailItem.SaveAs吗?

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 ... 和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() + " :: ");
    }
}

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: 示例代码:

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

The only disadvantage is that you've to add the field to the displayed columns in Outlook. 唯一的缺点是您必须将字段添加到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. 关于您的方法为什么行不通的原因:我可以想象当您在发送电子邮件后更改电子邮件的主题时,Outlook不喜欢它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM