Outlook - 查找邮件已发送至

时间:2015-05-31 22:01:53

标签: c# outlook add-in

我的客户有问题,这意味着我遇到了问题:

如果电子邮件收件人在Outlook中配置了多封电子邮件 - 我有一个雅虎电子邮件帐户和来自不同Exchange服务器的2个Exchange帐户。

在ProcessItemAdd事件处理程序(Add-In Express)中处理传入的电子邮件时 我尝试发现此电子邮件发送到的3个帐号:

所以我的第一种方法是:

                         Outlook.MailItem re = ((Outlook._MailItem)mail).Reply();
                        if (re != null)
                        {
                            var thisEmail = "";
                            Outlook.Account account = re.SendUsingAccount;
                            if (account != null)
                            {
                                thisEmail = account.SmtpAddress;
                                Log.VerboseFormat("thisEmail = {0}", thisEmail);

                                Marshal.ReleaseComObject(re);
                                re = null;

                                Marshal.ReleaseComObject(account);
                                account = null;

它在我的机器上使用Outlook 2010 32bit完美运行,但在Outlook 2010的客户端计算机上无效,返回空字符串。 所以我的第二个(备份方法)是:

                                if (String.IsNullOrEmpty(thisEmail))
                                {
                                    // This requires that in the case of Exchange account it should be cached Exchange account.
                                    thisEmail = (string)mail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8580001F");
                                    Log.VerboseFormat("Got receiver account email address from schema: {0}", thisEmail);
                                }
                            }
                        }

同样,这在我的机器上完美运行但在客户端机器上不起作用,返回空字符串。 我确实在我的客户端验证发件人是具有缓存的Exchange帐户...

顺便说一句,当发件人是非Exchange并且收件人是Exchange帐户时,它在客户端计算机上运行良好...

是否有任何其他方法可靠地确定电子邮件发送到哪个帐户(如果是多个收件人)。

以下是我最新的代码,适用于2010/2013但不适用于2007.后一个帐户为空,x-header返回空:

                            Outlook.MailItem re = ((Outlook._MailItem)mail).Reply();
                        if (re != null)
                        {
                            var thisEmail = "";
                            Outlook.Account account = re.SendUsingAccount;
                            if (account != null)
                            {
                                thisEmail = account.SmtpAddress;
                                Log.VerboseFormat("thisEmail = {0}", thisEmail);

                                Marshal.ReleaseComObject(re);
                                re = null;

                                Marshal.ReleaseComObject(account);
                                account = null;
                                if (String.IsNullOrEmpty(thisEmail))
                                {
                                    string x_headers = (string)mail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F");
                                    thisEmail = FindTargetEmail(x_headers);
                                    Log.Verbose("Extracted target email address from MIME x-headers: {0}", thisEmail);

                                } 
                            }
                            else 
                            {
                                Log.Verbose("account is null...");

                                if (String.IsNullOrEmpty(thisEmail))
                                {
                                    string x_headers = (string)mail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F");
                                    thisEmail = FindTargetEmail(x_headers);
                                    Log.Verbose("Extracted target email address from MIME x-headers: {0}", thisEmail);
                                }

                            } 

编辑: 好吧,还没有结束。虽然它在所有机器上运行良好,但我有客户端在2010年报告问题。这是一个非常奇怪的问题。 用户user1@domain1.com(缓存的Exchange帐户)向用户user2@domain2.com(gmail帐户)发送电子邮件。当我使用上面的代码解析接收方的MIME头以获取此电子邮件发送到的地址时...我回到user1@domain1.com而不是user2@domain2.com !!这怎么可能?我知道我正在解析标题的正确部分(是吗?)并且在我的机器上它返回正确的结果...为什么它不会发生在我的客户端的某些机器上?

专家,任何想法?

1 个答案:

答案 0 :(得分:0)

通常,Outlook配置文件中的Store代表一个单独的帐户。您可以在每个商店中处理Items类的ItemAdd事件(ADX术语中的ProcessItemAdd),以标识电子邮件发送到的帐户。