Office 2003 Outlook plug-in

Ez a mintaprogram egy web alapú iktatóprogramnak küld a levelekről adatokat.

namespace AnfrageToolOutlookAddIn
{
    using System;
    using Extensibility;
    using Microsoft.Office.Core;
    using System.Runtime.InteropServices;
 
    using Microsoft.Office.Interop.Outlook;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    using System.Collections.Specialized;
    using System.DirectoryServices;
    using MAPI;
 
    [GuidAttribute("1G3450FG-VFG5-5315-GHRE-FE20BGHJ24GD"), ProgId("IncomingAddIn.Connect")]
    public class Connect : Object, Extensibility.IDTExtensibility2
    {
        private Microsoft.Office.Interop.Outlook.Application applicationObject;
        private object addInInstance;
        private CredentialCache ccache;
        private WebClient client;
        private UriBuilder urib;
        private object oMissing = System.Reflection.Missing.Value;
        private const uint CdoPR_EMS_AB_PROXY_ADDRESSES = 0x800F101E;
 
 
        private CommandBarButton btnImportEMail;
 
        public Connect()
        {
        }
 
        public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
        {
            applicationObject = (Microsoft.Office.Interop.Outlook.Application)application;
            addInInstance = addInInst;
            ccache = new CredentialCache();
            if (connectMode != ext_ConnectMode.ext_cm_Startup)
            {
                OnStartupComplete(ref custom);
            }
        }
 
        public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
        {
            if (disconnectMode !=
                ext_DisconnectMode.ext_dm_HostShutdown)
            {
                OnBeginShutdown(ref custom);
            }
            applicationObject = null;
        }
 
        public void OnAddInsUpdate(ref System.Array custom)
        {
        }
 
        public void OnStartupComplete(ref System.Array custom)
        {
            CommandBars commandBars =
                applicationObject.ActiveExplorer().CommandBars;
            try
            {
                btnImportEMail = (CommandBarButton)
                    commandBars["Standard"].Controls["Incoming"];
            }
            catch
            {
                btnImportEMail = (CommandBarButton)
                    commandBars["Standard"].Controls.Add(1,
                    System.Reflection.Missing.Value,
                    System.Reflection.Missing.Value,
                    System.Reflection.Missing.Value,
                    System.Reflection.Missing.Value);
                btnImportEMail.Caption = "Incoming";
                btnImportEMail.Style = MsoButtonStyle.msoButtonCaption;
            }
            btnImportEMail.Tag = "import to incoming";
            btnImportEMail.OnAction = "!<IncomingAddIn.Connect>";
            btnImportEMail.Visible = true;
            btnImportEMail.Click += new
                _CommandBarButtonEvents_ClickEventHandler(
                btImportEMail_Click);
            client = new WebClient();
            urib = new UriBuilder("http", "infokristaly.hu", 80, "/teszt/index.php");
            client.Credentials = ccache;
            #endregion
        }
 
        private void btImportEMail_Click(CommandBarButton Ctrl,
            ref bool CancelDefault)
        {
            NameSpace outlookNS = applicationObject.GetNamespace("MAPI");
 
            MAPIFolder currentFolder = applicationObject.ActiveExplorer().CurrentFolder;
            Selection items = applicationObject.ActiveExplorer().Selection;
 
            foreach (Object item in items)
            {
                if (item is MailItem)
                {
                    NameValueCollection pairs = new NameValueCollection();
                    MailItem mailItem = (MailItem)item;
 
                    pairs.Add("Title", mailItem.Subject);
                    string senderSMTPAddress = "";
                    if (mailItem.SenderEmailType == "SMTP")
                        senderSMTPAddress=mailItem.SenderEmailAddress;
                    else
                        try
                        {
                            senderSMTPAddress = AdsGetObject(mailItem,currentFolder);
                        }
                        catch (System.Exception ex) {
                            MessageBox.Show(ex.ToString(), "Exception");
                        }
                    pairs.Add("ContactEmail", senderSMTPAddress);
                    if (ccache.GetCredential(urib.Uri, "Basic") == null)
                    {
                        Login loginForm = new Login();
                        if (loginForm.ShowDialog().Equals(DialogResult.OK))
                        {
                            ccache.Add(urib.Uri, "Basic", new NetworkCredential(loginForm.edtUser.Text, loginForm.edtPasswd.Text));
                        }
                    }
                    try
                    {
                        client.UploadValues(urib.Uri, "POST", pairs);
                        mailItem.UserProperties.Add("Incoming", OlUserPropertyType.olText, true, 1);
                        string stateInfo = "Mail from "+((MailItem)item).SenderName+" sent to incoming with subject: " + ((MailItem)item).Subject;
                        MessageBox.Show(stateInfo, "State Info");
                    }
                    catch (WebException ex)
                    {
                        MessageBox.Show(ex.ToString(),"Exception");
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "Exception");
                    }
                }
            }
 
        }
 
        private string AdsGetObject(MailItem mailItem, MAPIFolder currentFolder)
        {
            string result = "";
            MAPI.Session oSession = new MAPI.Session();
            oSession.Logon(oMissing, oMissing, false, false, null, oMissing, oMissing);
            MAPI.Message oMessage = (MAPI.Message) oSession.GetMessage(mailItem.EntryID, currentFolder.StoreID);
            MAPI.AddressEntry address = (MAPI.AddressEntry)oMessage.Sender;
            MAPI.Fields fields = (MAPI.Fields)address.Fields;
            MAPI.Field oFiled = (MAPI.Field)fields.get_Item(CdoPR_EMS_AB_PROXY_ADDRESSES, oMissing);
            foreach (Object addr in ((Object[])oFiled.Value))
            {
                if (addr.ToString().StartsWith("SMTP"))
                {
                    result = addr.ToString().Substring("SMTP:".Length);
                    break;
                }
            }
            oSession.Logoff();
            return result;
        }
 
        public void OnBeginShutdown(ref System.Array custom)
        {
            CommandBars commandBars = applicationObject.ActiveExplorer().CommandBars;
            try
            {
                commandBars["Standard"].Controls["Incoming"].Delete(System.Reflection.Missing.Value);
            }
            catch (System.Exception ex)
            { MessageBox.Show(ex.Message); }
        }
 
    }
}

Az AdsGetObject az Outlook2003 hiányosságát pótló függvény. Exchange szerveren X400 formátumban tárolt e-mail címeket cseréli SMTP címekre.