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

时间:2022-01-05 07:23:31

I don't want to store/display the body of an email when I receive it.

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

What I want is to select my email and click an option that allows me to store/display the body.

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

So far my code displays the body of the email(twice?) when it is selected.

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

Here is the code..

这是代码..

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 个解决方案

#1


0  

First of all, don't use multiple dots in the single line of code. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. See Systematically Releasing Objects for more information.

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

It seems you need to handle the SelectionChange event of the Explorer class and get the list of selected emails. The event is fired when the user selects a different or additional Microsoft Outlook item programmatically or by interacting with the user interface. It is also fired when the user (either programmatically or via the user interface) clicks or switches to a different folder that contains items, because Outlook automatically selects the first item in that folder.

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

The event is fired twice when the Reading pane is enabled in Outlook. If you turn it off, the event will be fired once.

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

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

#1


0  

First of all, don't use multiple dots in the single line of code. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. See Systematically Releasing Objects for more information.

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

It seems you need to handle the SelectionChange event of the Explorer class and get the list of selected emails. The event is fired when the user selects a different or additional Microsoft Outlook item programmatically or by interacting with the user interface. It is also fired when the user (either programmatically or via the user interface) clicks or switches to a different folder that contains items, because Outlook automatically selects the first item in that folder.

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

The event is fired twice when the Reading pane is enabled in Outlook. If you turn it off, the event will be fired once.

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

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