【问题标题】:Convert x500 email address into a smtp address将 x500 电子邮件地址转换为 smtp 地址
【发布时间】:2015-11-20 13:24:09
【问题描述】:

我正在使用插件 express 开发 Outlook 插件。当我收到发件箱邮件时,我可以在“mailItem.SenderEmailAddress”下看到我的电子邮件地址,格式为 x500。有没有办法将其转换为 SMTP 电子邮件地址。

这是我的代码

        Outlook.Explorer expl = null;
        Outlook.Selection selection = null;
        Outlook.MailItem mailItem = null;
        Outlook.Recipient recipient = null;
        string recipientsEmail = "";

        try
        {
            expl = Globals.ObjOutlook.ActiveExplorer() as Outlook.Explorer; ;
            if (expl != null)
            {
                selection = expl.Selection;
                if (selection.Count == 1)
                {
                    if (selection[1] is Outlook.MailItem)
                    {
                        mailItem = (selection[1] as Outlook.MailItem);
                        if (mailItem != null)
                        {
                            string senderEmailAddress = mailItem.SenderEmailAddress;

我的尝试和成功:- 我知道如何使用 Outlook.Recipient 对象将 x500 类型转换为 SMTP。在那里我可以获取收件人的“addressEntry”并获取 ExhangeUser,然后转到 ExhangeUser 的“PrimarySmtpAddress”。但我不确定如何处理 SenderEmailAddress 并将其转换为 SMTP。

一些有趣的实验 :- 由于“Sender”的属性在当前的 mailitem 对象中不可用,我设法使用反射来获取“Sender”的属性。但是当我运行以下代码时,“propertyInfo”对象值变为空。我不明白为什么。

这里是代码

//...
if (mailItem != null)
{
      var mailItem2 = GetNewObject(mailItem, "Sender", intArray);   
//...


public static object GetNewObject(Outlook.MailItem o, string popertyName, object[] parameters)
        {
            try
            {               
                PropertyInfo propertyInfo = o.GetType().GetProperty(popertyName); // This object is getting null
                return propertyInfo.GetType().GetProperty(popertyName).GetValue(o,parameters) as Outlook.MailItem;

            }
            catch (MissingMethodException ex)
            {
                // discard or do something
                Debug.DebugMessage(2, "AddinModule : Error in GetNewObject() : " + ex.Message);
                return null;
            }
        }

请给我建议。谢谢。

【问题讨论】:

  • 由于您将问题标记为“Outlook-Redemption”,您是否正在寻找 Redemption 解决方案?
  • @DmitryStreblechenko,是的,我是。因为我总是乐于接受救赎。但是您对“PR_SENDER_ENTRYID”的评论完全解决了任何问题。非常感谢。
  • @DmitryStreblechenko:如果您对此感兴趣,请提交:stackoverflow.com/documentation/outlook-addin/commit

标签: c# reflection exchange-server outlook-addin outlook-redemption


【解决方案1】:

您可以使用Sender 属性获取发件人的AddressEntry,如下所示:

Outlook.AddressEntry senderAddressEntry = mailItem.Sender;

【讨论】:

  • 谢谢。我也想过这个。但是邮件中没有“Sender”的属性。
  • 什么意思?例如,它是否返回 null?此消息是否来自 Exchange 邮箱?
  • MailItem.Sender 属性已添加到 Outlook 2010 中?您使用的是旧版本的 Outlook 还是只是旧的互操作 dll?
  • @DmitryStreblechenko,我也需要支持旧版本的 Outlook。所以,我不得不选择outlook的最低版本为2007。我尝试反射来获取“发件人”的属性。我将更新问题以获取更多信息。感谢您的考虑
  • @Yacoub Massad,Visual Studio 智能感知不会将“发件人”识别为 mailitem 的属性。原因是我使用的是旧版本的程序集。不幸的是我无法改变它。我正在尝试使用反射来获取“发件人”的属性。
【解决方案2】:

在阅读了“Dmitry Streblechenko”的评论后,我能够为自己开发一个完全可行的解决方案。这是代码

        private void adxRibBtnAddEmailAddress_OnClick(object sender, IRibbonControl control, bool pressed)
        {
            Outlook.MailItem mailItem = null;
            Outlook.Recipient recipient = null;
            string recipientsEmail = "";
            string sendersEmail = "";

            try
            {               
                mailItem = OutlookHelper.GetSelectedMailItem();
                if (mailItem != null)
                {
                    if (mailItem.SenderEmailType == "EX")
                        sendersEmail = GetSenderEmailAddress(mailItem);
                    else
                        sendersEmail = mailItem.SenderEmailAddress;

                    if (!string.IsNullOrEmpty(sendersEmail))
                    {
                        if (sendersEmail == Globals.OLCurrentUserEmail) // Sent mail
                        {
                            recipient = mailItem.Recipients[1];
                            if (recipient != null)
                            {
                                recipientsEmail = OutlookHelper.GetAddress(ref recipient);
                                if (!string.IsNullOrEmpty(recipientsEmail))
                                {
                                    // Do Something
                                }
                            }
                        }
                        else // inbox mail
                        {
                            // Do Something
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.DebugMessage(2, "AddinModule : Error in adxRibBtnsddemailaddress_OnClick() : " + ex.Message);
            }
            finally
            {
                if (recipient != null) Marshal.ReleaseComObject(recipient);
                if (mailItem != null) Marshal.ReleaseComObject(mailItem);
                Cursor.Current = Cursors.Default;
            }
        }

        private string GetSenderEmailAddress(Outlook.MailItem oM)
        {
            Outlook.PropertyAccessor oPA = null;
            Outlook.AddressEntry oSender = null;
            Outlook.ExchangeUser oExUser = null;

            string SenderID;
            string senderEmailAddress;

            try
            {
                // Create an instance of PropertyAccessor 
                oPA = oM.PropertyAccessor;
                // Obtain PidTagSenderEntryId and convert to string 
                SenderID = oPA.BinaryToString(oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C190102"));
                // Obtain AddressEntry Object of the sender 
                oSender = Globals.ObjNS.GetAddressEntryFromID(SenderID);

                oExUser = oSender.GetExchangeUser();
                senderEmailAddress = oExUser.PrimarySmtpAddress;

                return senderEmailAddress;
            }
            catch (Exception ex)
            {
                Debug.DebugMessage(2, "AddinModule : Error in adxRibBtnAddGenInteraction_OnClick() : " + ex.Message);
                return null;
            }
            finally
            {
                if (oExUser != null) Marshal.ReleaseComObject(oExUser);
                if (oSender != null) Marshal.ReleaseComObject(oSender);
                if (oPA != null) Marshal.ReleaseComObject(oPA);                
            }
        }

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 2013-02-04
    • 2011-03-11
    • 2013-01-24
    • 2011-03-12
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多