将当前查看的Outlook电子邮件正文存储为字符

时间:2018-02-27 15:26:26

标签: c# outlook ms-office vsto

我正在创建一个VSTO加载项来捕获用户当前选择的电子邮件(他们正在阅读的那个),并将所选文本作为字符串发送到python脚本进行处理。

我不确定如何获取当前查看的电子邮件正文并将其存储到单个字符串中。我使用mailItem.Body运行解决方案,将文本添加到新创建的电子邮件中,我无法找到从用户收件箱中查看的电子邮件中获取正文的方法。

我在想这样的事情可能有用:

public void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
    Microsoft.Office.Interop.Outlook.MailItem mailItem = 
    Inspector.CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;
    string test = mailItem.Body; //store email body as string
    MessageBox.Show(test); //verify the string was properly stored
 }

但是,我相信上面的代码只有在用户想要当前正在编写的电子邮件中的文本时才有效?我可以用什么来从电子邮件的正文中获取文本?

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码获取所选的电子邮件正文

public partial class ThisAddIn
{
   private Outlook.Explorer currentExplorer = null;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        currentExplorer = this.Application.ActiveExplorer();
        currentExplorer.SelectionChange += ExplorerSelectionChange;
    }
    private void ExplorerSelectionChange()
    {
        if (this.Application.ActiveExplorer().Selection.Count > 0)
        {
            Object selItem = this.Application.ActiveExplorer().Selection[1];
            if (selItem is Outlook.MailItem)
            {
                Outlook.MailItem mailItem = (selItem as Outlook.MailItem);
                string bodyText= mailItem.Body; //GET PlainTExt
                 string bodyHTML=mailItem.HTMLBody; //Get HTMLFormat
             }
         }
     }
}