VSTO Outlook 网上资料不是很全,由于业务的需要,使用Outlook比较多,之前是使用VBA,但发现VBA在运行时,Outlook被占用无法工作。

进而开始学习VSTO。资料很少自己慢慢摸索,有同样的困扰者可以一起交流。

Outlook VSTO 获取当前选择邮件的详细信息

//引用
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.Data.SqlClient;     
//声明全局变量
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;
            
try { if (this.Application.ActiveExplorer().Selection.Count > 0) { Object selObject = this.Application.ActiveExplorer().Selection[1]; if (selObject is Outlook.MailItem) { Outlook.MailItem mailItem = (selObject as Outlook.MailItem); /* 在这就可以获取活动邮件的各种信息了
               如主题:
mailItem.subject
               发件人地址:mailItem.SenderEmailAddress
                                  

              */ } } }
catch{ } }

没有获取收件人地址的方法,我们就写一个

VSTO 获取收件人的详细邮箱地址,这是返回使用你特殊字符分隔收件人和抄送人,可以写成返回一个数组

public static string GetSMTPAddressForRecipients(Outlook.MailItem mail)
        {
            string ReEmail = "";
            string CC = "";
            const string PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
            Outlook.Recipients recips = mail.Recipients;
            foreach (Outlook.Recipient recip in recips)
            {   
                Outlook.PropertyAccessor pa = recip.PropertyAccessor;
                if (pa.Parent.Type() == 1)
                {
                    ReEmail = ReEmail + pa.GetProperty(PR_SMTP_ADDRESS).ToString()+ "; ";
                }
                else
                {
                    CC = CC + pa.GetProperty(PR_SMTP_ADDRESS).ToString()+"; ";
                }

            }
            return ReEmail+"#"+CC;
        }