选择电子邮件后,如何存储/显示电子邮件正文? (展望2013)

时间:2015-01-12 16:48:02

标签: c# email outlook

收到电子邮件时,我不想存储/显示电子邮件的正文。

我想要的是选择我的电子邮件并单击允许我存储/显示正文的选项。

到目前为止,我的代码在选中时会显示电子邮件的正文(两次?)。

这是代码..

namespace MailForwarder
{
public partial class ThisAddIn
{

    Outlook.Explorer currentExplorer = null;

    private void ThisAddIn_Startup
        (object sender, System.EventArgs e)
    {
        currentExplorer = this.Application.ActiveExplorer();
        currentExplorer.SelectionChange += new Outlook
            .ExplorerEvents_10_SelectionChangeEventHandler
            (CurrentExplorer_Event);
    }

    private void CurrentExplorer_Event()
    {
        Outlook.MAPIFolder selectedFolder =
            this.Application.ActiveExplorer().CurrentFolder;
        String expMessage = "Your current folder is "
            + selectedFolder.Name + ".\n";
        try
        {
            if (this.Application.ActiveExplorer().Selection.Count > 0)
            {
                Object selObject = this.Application.ActiveExplorer().Selection[1];
                if (selObject is Outlook.MailItem)
                {

                    Outlook.Explorer explorer = this.Application.ActiveExplorer();
                    Outlook.Selection selection = explorer.Selection;

                    if (selection.Count > 0)   // Check that selection is not empty.
                    {
                        object selectedItem = selection[1];   // Index is one-based.
                        Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;

                        if (mailItem != null)    // Check that selected item is a message.
                        {
                            String htmlBody = mailItem.HTMLBody;
                            String Body = mailItem.Body;
                            MessageBox.Show(Body);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            expMessage = ex.Message;
        }
    }
 private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }
 #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// From    Subject Received    Size    Categories  
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }
    #endregion
}
}

1 个答案:

答案 0 :(得分:0)

首先,不要在单行代码中使用多个点。使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Outlook对象。然后在Visual Basic中将变量设置为Nothing(C#中为null)以释放对该对象的引用。有关详细信息,请参阅Systematically Releasing Objects

您似乎需要处理Explorer类的SelectionChange 事件并获取所选电子邮件的列表。当用户以编程方式选择不同的或附加的Microsoft Outlook项目或通过与用户界面交互时,将触发该事件。当用户(通过编程方式或通过用户界面)单击或切换到包含项目的其他文件夹时,也会触发它,因为Outlook会自动选择该文件夹中的第一个项目。

在Outlook中启用“阅读”窗格时,会触发两次事件。如果将其关闭,事件将被触发一次。

enter image description here