为VSTO Outlook加载项修改MailItem.Body?

时间:2016-02-29 14:23:01

标签: c# vsto outlook-addin outlook-2013 visual-studio-2015

我们一直试图编辑电子邮件的html正文以获得回复和支持。我们有一个自定义表单,我们希望将标题附加到html电子邮件正文,具体取决于它是否是新的电子邮件回复或foward。如何让新机构覆盖回复和发送电子邮件正文。这个主题运作得很好。

namespace OutlookAddIn
{
    public partial class ThisAddIn
    {
        private Outlook.Inspectors inspectors;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            inspectors = this.Application.Inspectors;
            inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Note: Outlook no longer raises this event. If you have code that 
            //    must run when Outlook shuts down, see http://go.microsoft.com/fwlink/?LinkId=506785
        }

        void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
            if (mailItem != null)
            {
                if (mailItem.EntryID == null)
                {
                    ClassificationForm formMain = new ClassificationForm();
                    formMain.ShowDialog();
                    mailItem.HTMLBody = formMain.GetHTMLBody();
                    mailItem.Subject = formMain.GetSubject();
                }
            }
        }

        #region VSTO generated code

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

        #endregion
    }
}

还尝试了这个,它确实取代了正文的html电子邮件,但确实打开了两个版本的电子邮件,并且ForwardingMessage.Display(true);不能完全显示电子邮件的编辑版本。

Outlook.MailItem ForwardingMessage = (Outlook.MailItem)this.Application.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.MailItem SelectedMessage = (Outlook.MailItem)this.Application.ActiveExplorer().Selection[1];
if (SelectedMessage != null)
{
    if (ForwardingMessage != null)
    {
        ClassificationForm formMain = new ClassificationForm();
        formMain.ShowDialog();
        SelectedMessage.HTMLBody = formMain.GetHTMLBody();
        SelectedMessage.Subject = formMain.GetSubject();
     }
}

1 个答案:

答案 0 :(得分:0)

我们解决了这个问题,方法是将一个表单区域添加到修改数据的电子邮件中,并使用简单的onsent事件将表单区域中的数据附加到实际的电子邮件中。