1

我正在開發一個使用加載項表達式的Outlook加載項。當我收到發件箱郵件項目時,我可以在x500格式的「mailItem.SenderEmailAddress」下看到我的電子郵件地址。有沒有辦法將其轉換爲SMTP電子郵件地址。轉換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; 

我曾嘗試和成功: - 我知道如何轉換X500型使用Outlook.Recipient對象爲SMTP。在那裏,我可以獲取收件人的「addressEntry」並獲取ExhangeUser,然後進入ExhangeUser的「PrimarySmtpAddress」。但我不確定如何處理SenderEmailAddress並將其轉換爲SMTP。

一些有趣的實驗: - 由於「發件人」的屬性在當前的MailItem對象中可用,我設法使用反射來得到「發件人」的財產。但是當我運行下面的代碼時,「propertyInfo」對象值變爲null。我不明白爲什麼。

下面是代碼

//... 
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; 
      } 
     } 

請諮詢我。謝謝。

+0

由於您將問題標記爲「Outlook-Redemption」,您是否在尋找Redemption解決方案? –

+0

@DmitryStreblechenko,是的,我是。因爲我總是樂於兌現。但是你對「PR_SENDER_ENTRYID」的評論完全解決了這個問題。爲此非常感謝。 –

+0

@DmitryStreblechenko:如果您有興趣請提交此:http://stackoverflow.com/documentation/outlook-addin/commit –

回答

1

閱讀從「德米特里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);     
      } 
     } 
1

您可以使用Sender屬性這樣獲得發件人的AddressEntry

Outlook.AddressEntry senderAddressEntry = mailItem.Sender; 
+0

謝謝。我也想過這個。但是「發件人」的屬性在郵件中沒有。 –

+0

你是什麼意思?它是否返回null例如?此郵件是否來自Exchange郵箱? –

+0

MailItem.Sender屬性是在Outlook 2010中添加的?你使用舊版本的Outlook還是舊的互操作DLL? –

相關問題