我怎样才能在Silverlight 4应用程序Outlook 2003中的数据 [英] How can I get Outlook 2003 data in Silverlight 4 application

查看:220
本文介绍了我怎样才能在Silverlight 4应用程序Outlook 2003中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能使用自动化Outlook 2003与Silverlight 4的? ?或者,也许有在Silverlight应用程序使用Outlook 2003 MAPI一些不同的方式。

Is it possible to use Automation for Outlook 2003 with Silverlight 4? Or maybe there are some different way to use Outlook 2003 MAPI in Silverlight application?

我使用Silverlight 4中,我试图与Outlook以这种方式互动:

I'm using Silverlight 4 and I'm trying interact with Outlook in this way:

dynamic outlook = AutomationFactory.GetObject("Outlook.Application"); 



对于Outlook 2007/2010一切工作正常。

For Outlook 2007/2010 all works fine.

但是当我尝试使用动态实例(例如outlook.Session)的任何字段从Outlook 2003中,我已经得到NotSupportedException异常。这只是Outlook 2003中的问题。

But when I try use any field of dynamic instance (for example outlook.Session) from Outlook 2003 then I've get NotSupportedException. It's only Outlook 2003 problem.

我发现文章的 http://msdn.microsoft.com/en-us/library/aa159619%28office.11​​%29.aspx 但它是无用的Silverlight应用程序(不可能得到参考办公室集或COM直接)。

I found article http://msdn.microsoft.com/en-us/library/aa159619%28office.11%29.aspx but it's useless for Silverlight application (impossible to get reference to office assembly or COM directly).

Type.GetTypeFromProgID 也没用太,Silverlight不支持

Type.GetTypeFromProgID is useless too, Silverlight doesn't support it.

推荐答案

我终于找到了答案。
大部分操作可以使用标准Outlook 2003对象模型来执行。所有这些行动在此Microsoft文章描述。在文章和Silverlight代码示例之间
主要区别 - 我们不能参考的Interop的Outlook组件,所以我们需要使用动态。
所以这是很容易得到联系人列表或所有联系人的所有电子邮件收件箱(见文章)。最困难的部分是获得创建用户帐户列表。 Outlook 2003的对象模型提供了可能获得只有一个(默认)帐户:

I’ve finally found an answer. Most of actions can be performed using standard Outlook 2003 object model. All these actions described in this Microsoft article. Main difference between examples in article and Silverlight code – we can’t refer Interop Outlook assembly, so we need to use dynamics. So it’s pretty easy to get all contacts from contact list or all inbox emails (see article). Most difficult part is obtaining list of created user’s accounts. Outlook 2003 object model provide possibility to obtain only one (default) account:

dynamic outlook = AutomationFactory.CreateObject("Outlook.Application");
var ns = outlook.GetNamespace("MAPI");
var defaultAccount = ns.CurrentUser;



但它仍然不适合我。这是非常可悲的,但在Outlook 2003对象模型没有Session.Accounts财产。所以,我发现只有一个取巧的办法来获得帐户列表。

But it’s still doesn’t suitable for me. It’s very sad, but there is no Session.Accounts property in Outlook 2003 object model. So I’ve found only one tricky way to obtain list of accounts.

public class Ol11ImportStrategy 
    {
        const string registryPath = @"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676\{0}\{1}";
        const string constAccountName = "Account Name";
        const string constEmail = "Email";
        const string constSMTPServer = "SMTP Server";
        const string constName = "Display Name";
        const string constIMAPServer = "IMAP Server";
        const string constPOP3Server = "POP3 Server";
        const string constValueClsid = "clsid";
        const string constContentsAccountClsid_POP3 = "{ED475411-B0D6-11D2-8C3B-00104B2A6676}";
        const string constContentsAccountClsid_IMAP = "{ED475412-B0D6-11D2-8C3B-00104B2A6676}";

        public IEnumerable<AccountEntity> GetAccountsInFriendlyFormat()
        {
            List<AccountEntity> accounts = new List<AccountEntity>();

            using (dynamic WShell = AutomationFactory.CreateObject("WScript.Shell"))
            {
                for (int i = 1; i < 1000; i++)
                {
                    try
                    {
                        string classId = WShell.RegRead(String.Format(registryPath, i.ToString().PadLeft(8, '0'), constValueClsid));

                        if (StringComparer.InvariantCultureIgnoreCase.Compare(classId, constContentsAccountClsid_POP3) == 0)
                        {
                            accounts.Add(new AccountEntity
                            {
                                FriendlyName = GetRegisterElementValue(WShell, i.ToString(), constAccountName),
                                IncomingMailServer = GetRegisterElementValue(WShell, i.ToString(), constPOP3Server),
                                Email = GetRegisterElementValue(WShell, i.ToString(), constEmail),
                                UserName = GetRegisterElementValue(WShell, i.ToString(), constName)
                            });
                            continue;
                        }

                        if (StringComparer.InvariantCultureIgnoreCase.Compare(classId, constContentsAccountClsid_IMAP) == 0)
                        {
                            accounts.Add(new AccountEntity
                            {
                                FriendlyName = GetRegisterElementValue(WShell, i.ToString(), constAccountName),
                                IncomingMailServer = GetRegisterElementValue(WShell, i.ToString(), constIMAPServer),
                                Email = GetRegisterElementValue(WShell, i.ToString(), constEmail),
                                UserName = GetRegisterElementValue(WShell, i.ToString(), constName)
                            });
                            continue;
                        }

                        //it isn't POP3 either IMAP
                    }
                    catch (FileNotFoundException e)
                    {
                        //classId isn't found - we can break iterations because we already iterate through all elements
                        break;
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Unknown exception");
                    }
                }
            }

            return accounts;
        }

        private string GetRegisterElementValue(object scriptShell, string elementNumber, string elementName)
        {
            dynamic shell = scriptShell;
            string currentElement = elementNumber.PadLeft(8, '0');

            object[] currentElementData = shell.RegRead(String.Format(registryPath, currentElement, elementName));

            byte[] dataBytes = currentElementData.Cast<byte>().ToArray();
            return Encoding.Unicode.GetString(dataBytes, 0, dataBytes.Count()).Trim('\0');
        }
    }

public class AccountEntity
{
    public string FriendlyName { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string AccountType { get; set; }
    public string IncomingMailServer { get; set; }
}



主要伎俩是使用AutomationFactory.CreateObject(WScript.Shell的)。现在,可以使用RegRead方法从注册表中直接拿到帐户信息。顺便说一句这个代码工作良好,即使对于Outlook 2007/2010。至于我,如果帐户应被悄悄收集是最可取的方法(也没有必要数据采集之前推出的Outlook)。

Main trick is in use of AutomationFactory.CreateObject("WScript.Shell"). Now it’s possible to get account information directly from registry using RegRead method. By the way this code works well even for Outlook 2007/2010. And as for me it’s most preferable way if accounts should be collected silently (there is no need to launch Outlook before data collecting).

这篇关于我怎样才能在Silverlight 4应用程序Outlook 2003中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆