Get Sender Email Address from Outlook using CDO in C#

//Step 1: Add reference of  "Microsoft CDO 1.21 Library" and "Outlook"

private object oMissing = System.Reflection.Missing.Value;

public string GetFromAddress ( Outlook.MailItem msg )
  {
   string strReturn = string.Empty;

   MAPI.Session oSession = new MAPI.Session ( );
   oSession.Logon ( oMissing, oMissing, false, false, null, oMissing, oMissing );
   MAPI.Message oMessage = ( MAPI.Message ) oSession.GetMessage ( msg.EntryID,oMissing );

   MAPI.Fields fields = ( MAPI.Fields ) oMessage.Fields;
   MAPI.Field oFiled = ( MAPI.Field ) fields.get_Item ( MAPI.CdoPropTags.CdoPR_SENDER_EMAIL_ADDRESS, oMissing );

   strReturn = oFiled.Value.ToString ( );

   oSession.Logoff ( );

   return strReturn;  
  }


You can also use VBA as following:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Desc: Get the from address from of the specified MailItem
'Input:
'   objMsg: The object class can be Outlook.MailItem or MAPI.Message
'Output:
'   byte array
'Remarks:
'   Since there is no function to get from address in outlook, so we use this function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function GetFromAddress(objMsg)
   
    ' start CDO session
    Set objSession = CreateObject("MAPI.Session")
    objSession.Logon "", "", False, False
   
    ' pass message to CDO
    strEntryID = objMsg.EntryID
    strStoreID = objMsg.Parent.StoreID
    Set objCDOMSG = objSession.GetMessage(strEntryID, strStoreID)
   
    ' get sender address
    On Error Resume Next
    strAddress = objCDOMSG.Sender.Address
    If Err = &H80070005 Then
        'handle possible security patch error
        MsgBox "The Outlook E-mail and CDO Security Patches are " & _
            "apparently installed on this machine. " & _
            "You must response Yes to the prompt about " & _
            "accessing e-mail addresses if you want to " & _
            "get the From address.", vbExclamation, _
            "GetFromAddress"
    End If
   
    GetFromAddress = strAddress
   
    Set objCDOMSG = Nothing
    objSession.Logoff
    Set objSession = Nothing
End Function

 

转载于:https://www.cnblogs.com/yuanliding_1/archive/2007/06/07/775670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值