//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

 

posted on 2007-06-07 19:09  袁礼定  阅读(824)  评论(0编辑  收藏  举报