当电子邮件类型为 EX for Outlook 时,如何获取发件人的电子邮件?

How to get sender's email when Email type is EX for Outlook?

我正在尝试通过 C# 代码获取 Outlook 邮箱的发件人电子邮件地址。我试过 this link 但我得到 "Object reference not set to an instance of an object" 用于 senderEmail = objAddressentry.GetExchangeUser().PrimarySmtpAddress 行。我的代码如下:

Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
Outlook.MailItem olMail = new Outlook.MailItem();

if (olMail.SenderEmailType == "EX")
{

   var objReply = olMail.Reply();
   var objRecipient = objReply.Recipients[1]; 

   var strEntryId = objRecipient.EntryID;

   var objAddressentry = oNS.GetAddressEntryFromID(strEntryId);
   string senderEmail = objAddressentry.GetExchangeUser().PrimarySmtpAddress;
}

如有任何帮助,我将不胜感激。

如果它有任何帮助,我遇到了 this link,但我的 AddressEntryUserTypeolExchangeDistributionListAddressEntry,这就是为什么控件不进入 If 分支的原因。

如前所述,我的代码是正确的。当 SenderAddressEntryUserTypeolExchangeDistributionListAddressEntry 类型时,它会抛出 objAddressentry.GetExchangeUser() 的 "Object reference not set to an instance of an object" 异常。修改后的代码如下:

if (olMail.SenderEmailType == "EX")
   {

         var objReply = olMail.Reply();
         Outlook.Recipient objRecipient = objReply.Recipients[1]; 
         string strEntryId = objRecipient.EntryID;
         Outlook.AddressEntry objAddressentry = oNS.GetAddressEntryFromID(strEntryId);

         if (objAddressentry != null)
         {

             Outlook.ExchangeUser eu = objAddressentry.GetExchangeUser();
             if (eu != null)
                senderEmail = eu.PrimarySmtpAddress; 

          }

 }